Multithread sync
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.
correct - read access never hurt anyone :). it's only when one or more threads attempts to write that you get problems.
Quote:Original post by Dave
What language is this?
Isnt this a common problem to all languages?
Quote:Original post by The C modest godQuote: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,
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement