Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:07:40+00:00 2026-05-31T05:07:40+00:00

For the sake of learning combinatorics of boost::thread I’m implementing a simple barrier (BR)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:07:40+00:00Added an answer on May 31, 2026 at 5:07 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a program to perform Huffman compression/decompression (for the sake of learning/having
For the sake of education, and programming practice, I'd like to write a simple
For the sake of argument, here's a simple person class public class Person :
I am learning multi-threading and for the sake of understanding I have wriiten a
For the sake of learning, I'm trying to chain decorators that are defined by
Well I'm designing a Custom WPF control - fore the sake of learning -
For the sake of learning more about jQuery, I was wondering if anyone could
For the sake of simplicity & learning something new, please don't suggest using two
For sake of a simple example, I'd like to have a list of strings.
For the sake of learning C and understanding the difference between Binary Files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.