hi
Is it necessary to protect a single variable with mutex when only a single variable is getting changed.
hi
Is it necessary to protect a single variable with mutex when only a single variable is getting changed.
Yes, a statement such as
can in the object code expand to several instructions. Pseudocode:
Qt Code:
register = foo; register += 1; foo = register;To copy to clipboard, switch view to plain text mode
If another thread is also executing the same instructions, it may have been interrupted right after the first pseudo-instruction above. The variable will then be incremented only once even when two threads tried to increment it...
babu198649 (9th December 2008)
Thanks
Does changing a boolean value also takes more than one instruction.
Not if you're simply assigning true or false to it.
But bools rarely come alone. They might, say, indicate that some data structure is ready for use (which should then also be protected by the mutex).
If another thread needs to QWaitCondition for the bool to have a particular value, it will need a mutex argument.
Bookmarks