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 6187691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:06:09+00:00 2026-05-24T02:06:09+00:00

I am using boost::thread, and I meet some problems. The thing is, are there

  • 0

I am using boost::thread, and I meet some problems.

The thing is, are there any ways I can join a thread before the last join finish?
for example,

int id=1;
void temp()
{
    int theardID = id++;
    for(int i=0;i<3;i++)
    {
        cout<<theardID << " : "<<i<<endl;
        boost::this_thread::sleep(boost::posix_time::millisec(100));
    }
}
int main(void)
{
    boost::thread thrd1(temp);

    thrd1.join();

    boost::thread thrd2(temp);
    boost::thread thrd3(temp);    
    thrd2.join();
    thrd3.join();   
    return 0;
}

In this simple example, the order of output may be:

1:0
1:1
1:2
2:0
3:0
3:1
2:1
2:2
3:2

As the above example, we can see find out that thrd2 and thrd3 start to run after thrd1 finish.

Are there any ways to let thrd2 and thrd3 run before thrd1 finish?

  • 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-24T02:06:10+00:00Added an answer on May 24, 2026 at 2:06 am

    You can use Boost.Thread’s condition variables to synchronize on a condition more complex than what join can provide. Here’s a example based on yours:

    #include <iostream>
    #include <boost/thread.hpp>
    #include <boost/thread/locks.hpp>
    #include <boost/thread/mutex.hpp>
    #include <boost/thread/condition_variable.hpp>
    
    boost::mutex mutex;
    boost::condition_variable cond;
    
    // These three variables protected by mutex
    bool finishedFlag = false;
    int finishedID = 0;
    int finishedCount = 0;
    
    int id=1;
    void temp()
    {
        int threadID = id++;
        for(int i=0;i<3;i++)
        {
            std::cout << threadID << " : " << i << std::endl;
            boost::this_thread::sleep(boost::posix_time::millisec(100));
        }
    
        {
            boost::lock_guard<boost::mutex> lock(mutex);
            finishedFlag = true;
            finishedID = threadID;
            ++finishedCount;
        }
        cond.notify_one();
    }
    
    int main(void)
    {
        boost::thread thrd1(temp);
        boost::this_thread::sleep(boost::posix_time::millisec(300));
        boost::thread thrd2(temp);
        boost::thread thrd3(temp);
    
        boost::unique_lock<boost::mutex> lock(mutex);
        while (finishedCount < 3)
        {
            while (finishedFlag != true)
            {
                // mutex is released while we wait for cond to be signalled.
                cond.wait(lock);
    
                // mutex is reacquired as soon as we finish waiting.
            }
            finishedFlag = false;
    
            if (finishedID == 1)
            {
                // Do something special about thrd1 finishing
                std::cout << "thrd1 finished" << std::endl;
            }
        };
    
        // All 3 threads finished at this point.
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using boost::any to store pointers and was wondering if there was a
Without using boost::thread and boost::bind directly, is there a way to implement the equivalent
I am using boost::thread, how can I tell if a thread is still running
So I have done some research, and have found you can create a boost::thread
I started using boost::thread recently (WinXP, VS10, BoostPro) and found that mutex can be
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I am using wxwidgets together with boost::thread. The Thread is a worker thread which
I am using boost::thread to process messages in a queue. When a first message
I'm writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;

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.