std::string copy-on-write implementation thread safety [duplicate]
Possible Duplicates:
C++: std::string in a multi-threaded program Is std::string thead-safe with gcc 4.3?
Hello all,
Suppose that we are passing a reference to object to the thread procedure and later we modify that object. Here we have data race, so to avoid that it's better to pass object by value. Now it looks safe, but is it really safe? What if the object is of type std::string
. There are compilers that implement std::string
with copy-on-write semantics, so the actual data is not copied, instead they both refer to the same data. The real copy is done when modifier function is called on one of the objects. Existing C++ standard allows such implement开发者_如何学Cation, however existing standard doesn't say a word about multithreading, so this implementation is allowed not to be thread safe. Is that right?
What would you say about this kind of solution?
std::string x = "blablabla; std::strinc copy_of_x = x.c_str();
Thanks.
This is implementation-dependent. Check the documentation of your standard libraries: there may be separate implementations for single- and multithreaded applications. Offhand, I can't point to any reasonably modern C++ platform that doesn't have some support for multithreading.
精彩评论