Sign in to follow this  

Multithread sync

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

This topic is 4304 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this