Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Multithread sync

This topic is 4672 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

If I have aglobal variable in a program, and lets say there are 2 cpus in the processor. Then there is no problem for two threads to both read that variable at the same time, right? I only need to worry to get garbage when one reads it while the other writes it. Is that true? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
correct - read access never hurt anyone :). it's only when one or more threads attempts to write that you get problems.

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
Quote:
Original post by Dave
What language is this?

Isnt this a common problem to all languages?


It is - unless you have some built-in mecanism in the langage that lock variables when reading or writing them (but then your question doesn't make sense [smile]).

The main problem is not with reading, it is with writing. But eitherway, you'll have to write the variable at least once before reading it :)

Regards,

Share this post


Link to post
Share on other sites
Any data that is shared between threads should be treated with extreme caution - particularly vectors, or other iterated containers. Such data should be protected by a mutex.

Even searches (ostensibly a read) on iterated containers should be mutexed - should another thread write to the container, potentially the container's internal implementation could be moved, or a bounds iterator could be invalidated.


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!