4

For the sake of learning combinatorics of boost::thread I'm implementing a simple barrier (BR) for threads which lock a common mutex (M). However, as far as I get it when going to BR.wait() the locks on the mutex are not released, so in order for all threads to arrive at BR a manual release of the lock on M needs to be performed. So I have the following code:

boost::barrier BR(3);
boost::mutex M;

void THfoo(int m){
    cout<<"TH"<<m<<" started and attempts locking M\n";
    boost::lock_guard<boost::mutex> ownlock(M);

    cout<<"TH"<<m<<" locked mutex\n";
    Wait_(15); //simple wait for few milliseconds

    M.unlock(); //probably bad idea
    //boost::lock_guard<boost::mutex> ~ownlock(M);
    // this TH needs to unlock the mutex before going to barrier BR

    cout<<"TH"<<m<<" unlocked mutex\n";
    cout<<"TH"<<m<<" going to BR\n";
    BR.wait();
    cout<<"TH"<<m<<" let loose from BR\n";
}

int main()  
{  
    boost::thread TH1(THfoo,1);
    boost::thread TH2(THfoo,2);
    boost::thread TH3(THfoo,3);

    TH2.join(); //but TH2 might end before TH1, and so destroy BR and M
    cout<<"exiting main TH \n";

    return 0;  
}

Whereas M.unlock() is clearly a bad solution (not using the lock); so how to (simply) release the lock? Also: how do I (properly) wait in main() for all threads to finish? (TH2.join() is bad, `cause TH2 may finish first...);

Please do not suggest go-arounds, e.g. with conditional variables, which I can also use, but it must be possible to do it straightforwardly without them.

5 Answers 5

9

In addition to scoping the boost::lock_guard in a block, you can also use a boost::unique_lock which can be unlock()'ed explicitly:

boost::unique_lock<boost::mutex> ownlock(M);

cout<<"TH"<<m<<" locked mutex\n";
Wait_(15); //simple wait for few milliseconds

ownlock.unlock();

This is useful if you need to release the mutex before reacquiring it later.

As for the joining, simply call join() on all thread handles in turn.

0
4

Something like:

void THfoo(int m){
  // use a scope here, this means that the lock_guard will be destroyed (and therefore mutex unlocked on exiting this scope
  {
    cout<<"TH"<<m<<" started and attempts locking M\n";
    boost::lock_guard<boost::mutex> ownlock(M);

    cout<<"TH"<<m<<" locked mutex\n";
    Wait_(15); //simple wait for few milliseconds

  }
  // This is outside of the lock
  cout<<"TH"<<m<<" unlocked mutex\n";
  cout<<"TH"<<m<<" going to BR\n";
  BR.wait();
  cout<<"TH"<<m<<" let loose from BR\n";
}

As for the waiting, simply call join on all the thread handles (if they've completed already, the function will return immediately)

TH1.join();
TH2.join();
TH3.join();
0
1

Let it go out of scope:

void THfoo(int m){
    cout<<"TH"<<m<<" started and attempts locking M\n";
    {
       boost::lock_guard<boost::mutex> ownlock(M);

       cout<<"TH"<<m<<" locked mutex\n";
       Wait_(15); //simple wait for few milliseconds
    }

    // this TH needs to unlock the mutex before going to barrier BR

    cout<<"TH"<<m<<" unlocked mutex\n";
    cout<<"TH"<<m<<" going to BR\n";
    BR.wait();
    cout<<"TH"<<m<<" let loose from BR\n";
}
1

If you use boost::mutex::scoped_lock rather than boost::lock_guard, that has an unlock() method. If you call that then the lock will not attempt to re-unlock in its destructor. I find the code thus more to my taste than placing the lock into its own block.

2
  • To add a little: If you call unlock() on the underlying mutex like you do in your question, unlock() will be called again when the lock_guard goes out of scope. If you use scoped_lock (or similar) and call unlock() on that, though, it won't attempt to unlock a second time when it goes out of scope.
    – RobH
    Commented Mar 6, 2012 at 10:44
  • I find the block (and accompanying indentation) a lot clearer than interleaving function calls Commented Mar 6, 2012 at 12:16
1
cout<<"TH"<<m<<" started and attempts locking M\n";
{
    boost::lock_guard<boost::mutex> ownlock(M);

    cout<<"TH"<<m<<" locked mutex\n";
    Wait_(15); //simple wait for few milliseconds

} //boost::lock_guard<boost::mutex> ~ownlock(M);
// this TH needs to unlock the mutex before going to barrier BR

cout<<"TH"<<m<<" unlocked mutex\n";

As long as you join all the threads, the only issue when TH2 finishes first is that TH1 has to finish running before TH2 can be "reaped" by join, and any leftover resources like the return value freed. Not really worth worrying about for 3 threads. If that memory use was a problem then you could use timed_join to repeatedly try all threads in turn.

You could also do what you don't want - have the main thread wait on a condition variable, and each thread when it finishes stores a value somewhere to say that it is finished, and signals the condition variable so the main thread can join it. You have to be absolutely sure that the thread is going to signal, otherwise you could end up waiting for it forever. So be careful if you ever cancel threads.

2
  • I'm rather worried about the M and BR, which would not be accessible to the rest of THs after main() exits. So, the issue is how to do it correctly, so that it never runs the risk of crashing -- and I guess your "timed_join" on all threads is the answer, right?
    – P Marecki
    Commented Mar 6, 2012 at 10:48
  • @PMarecki: Unless you can think of a particular reason not to, just do TH1.join(); TH2.join(); TH3.join();. Then main will not return while any of the threads is running (although it could throw). You should plan to either join or detach every thread you start. The destructor of boost::thread will in any case detach them, which I think means that they should be terminated on exit before globals are destroyed. But if you're seeing otherwise, perhaps I'm wrong. Commented Mar 6, 2012 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.