Solved

lock on bool when access via multiple threads

Posted on 2008-10-30
3
342 Views
Last Modified: 2013-12-14
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.

0
Comment
Question by:schneiderit
  • 2
3 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22848298
>>>> Do I need to lock around the bool?
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.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 22848342
Assume the bool is false initially.

Then both threads would see that.

If now the first thread sets the bool to true, the second may still read a false if the read operation was nearly same time (or same time at another core). But any little time later, it correctly would read the true. So, the only reason for a lock could be if the second must recognize the setting to true immediately, e. g. in case the bool is used for synchronization reasons itself. For these cases a unlocked bool is not safe, you would need  amutex, critical section or at least an atomic increment, e. g. by calling InterLockedIncrement on a shared long:

    volatile long  g_stop = 0;
    ...
     
    while (InterLockedIncrement(&g_stop) > 1)
    {
          // coming here a second thread has incremented the g_stop nearly same time
          InterLockedDecrement(&g_stop);
          Sleep(1);
    }
    // coming here it is safe now
    ....
   
    // reset the lock flag
    InterLockedDecrement(&g_stop);



0
 

Author Closing Comment

by:schneiderit
ID: 31511591
that's great, thankyou.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
easiest way to find port number 12 108
VB.Net Reusable Form Layout 4 149
Java eclipse error 2 70
Visual Studio Debugging 3 132
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question