Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

std::string

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just told that std::string causes a problem in a multithreaded application and that when i assing a string I should change it to a c style string instead. can someone explain this?

Share this post


Link to post
Share on other sites
Advertisement
This does not make any sense. Neither std::string nor char* is adapted to multithreaded environments in itself, you will need some sort of synchronization or exclusion strategy for either to work anyway.

Share this post


Link to post
Share on other sites
Some implementations of std::string use Copy-on-write semantics, which can cause problems in multithreaded environments. IIRC, though, these are actually pretty rare, for exactly this reason.

Share this post


Link to post
Share on other sites
Unless your compiler's standard library documentation tells you not to, std::string is fine as long as you obey normal reader/writer exclusions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
IIRC, though, these are actually pretty rare, for exactly this reason.


For a given value of "rare" anyways. IIRC, MSVC 6's std::string implementation uses COW, as do older versions of Borland C++.

Share this post


Link to post
Share on other sites
Bah, those don't count as C++ compilers anyways ;-).

That said, the proper alternative to std::string in such a situation would be your own string class (or downloading a replacement standard library - doesn't STLport work on VS6?). While std::string + multithreading = problems in a few ancient compilers I wouldn't be caught dead using anyways, "c style" char*s = problems anywhere, and everywhere if used directly.

Share this post


Link to post
Share on other sites
What you should use is a suitable synchronization primitive to serialize usage of the resource, instead of blindly hoping things will work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zao
What you should use is a suitable synchronization primitive to serialize usage of the resource, instead of blindly hoping things will work.


Which would be much better advice if you mentioned how to properly synchronize multiple instances of a string object if they used a COW implementation. Entangled state makes this a less straightforward operation than you imply.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!