Juha Nieminen <nos...@thanks.invalid> wrote in
news:n0pvll$93o$
2...@adenine.netfront.net:
Yes, better safe than sorry. I'm not actually sure what kind of thread
safety the OP meant. Maybe he was just concerned if one can call new in
two different threads at the same time. This is of cource guaranteed,
otherwise we would need to put a mutex lock around each string+=char
operation.
For allocators, the next level of thread safety means that the memory
allocated in one thread can be released in another. Without this, any
inter-thread communication would become extremely fragile and cumbersome.
One could not legally call e.g. string.c_str() in a wrong thread. So
there are good reasons why the default allocator must provide such thread
safety.
However, there is no inherent reason that thread-safety must mean
slowness. For example, there are general allocator libraries like Intel
TBB with drastically better performance than the default MSVC one, at
least in massive multithreading regime and at least some years ago (have
not bothered to check recently).
Of course, dynamically allocating something will always be slower than
not allocating. Here C++ has an edge over many competitors as it can
easily place complex objects on stack, in a buffer of std::vector, etc.
Cheers
Paavo