schneiderit
asked on
lock on bool when access via multiple threads
Guys,
I have two threads.
Currently I check on a bool to determine whether one of the threads should stop work. The bool is set from the other thread. Do I need to lock around the bool? I presumed I would, but collegues believe I may not have to? Another collegue mentioned setting the bool to volatile, so it's not cached? I appreciate if anyone could suggest the correct action.
Thanks.
- Lee.
I have two threads.
Currently I check on a bool to determine whether one of the threads should stop work. The bool is set from the other thread. Do I need to lock around the bool? I presumed I would, but collegues believe I may not have to? Another collegue mentioned setting the bool to volatile, so it's not cached? I appreciate if anyone could suggest the correct action.
Thanks.
- Lee.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that's great, thankyou.
You only need to lock if both threads were updating the bool. If only one is writing and the other is only reading, the lock is not needed.