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

  • Home
  • SEARCH
  • 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 7925499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:19:06+00:00 2026-06-03T18:19:06+00:00

I’m working on learning to use boost threads. I’m trying to make a simple

  • 0

I’m working on learning to use boost threads. I’m trying to make a simple program where I feed numbers to a queue that is available on both threads and output it on the worker thread.

I made it so that the worker thread shuts down if I feed it a 1.

The problem is when I call join, my main thread just sits there and waits for the worker thread to be finished. I do not want this. I want main to keep executing in parallel to the worker thread.

template<typename Data>
class concurrent_queue
{
private:
    std::queue<Data> the_queue;
    mutable boost::mutex the_mutex;
    boost::condition_variable the_condition_variable;
public:
    void push(Data const& data)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        the_queue.push(data);
        lock.unlock();
        the_condition_variable.notify_one();
    }

    bool empty() const
    {
        boost::mutex::scoped_lock lock(the_mutex);
        return the_queue.empty();
    }

    bool try_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        if(the_queue.empty())
        {
            return false;
        }

        popped_value=the_queue.front();
        the_queue.pop();
        return true;
    }

    void wait_and_pop(Data& popped_value)
    {
        boost::mutex::scoped_lock lock(the_mutex);
        while(the_queue.empty())
        {
            the_condition_variable.wait(lock);
        }

        popped_value=the_queue.front();
        the_queue.pop();
    }

};

void workerFunc(concurrent_queue<int>* q )  
{  
    while(true)
    {
        while(!q->empty())
        {
            int p = -1;
            q->wait_and_pop(p);
            std::cout << p;

            if(p == 1)
            {
                return;
            }
        }
    }
}  

int main(int argc, char* argv[])  
{  
    concurrent_queue<int> m_q;
    std::cout << "main: startup" << std::endl;  

    boost::thread workerThread(workerFunc,&m_q);  

    std::cout << "main: waiting for thread" << std::endl;  

    m_q.push(6);
    m_q.push(11);
    workerThread.join();
    m_q.push(99); //will not reach here
    std::cout << "main: done" << std::endl;  

    return 0;  
} 

Thanks

I want the thread to be active and running and only process numbers when it has some that could be given at any time by the main thread.

  • 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-06-03T18:19:07+00:00Added an answer on June 3, 2026 at 6:19 pm

    The join() function waits for the attached thread to finish, as it should. There is no reason for your main thread to call join() where it does, if your intent is to keep that thread running for the life of your program (or until you push a 1 into your queue).

    If you don’t join() that thread at all, then the thread will become detached when the workerThread object goes out of scope. This is almost certainly not what you want. Typically a multithreaded program will join() all threads prior to returning from main. In your case, you need to specifically signal your thread (by pushing a 1 onto your queue) prior to calling join() to get your program to exit cleanly.

    Here’s an example:

    int main(int argc, char* argv[])  
    {  
        concurrent_queue<int> m_q;
        std::cout << "main: startup" << std::endl;  
    
        boost::thread workerThread(workerFunc,&m_q);  
    
        std::cout << "main: waiting for thread" << std::endl;  
    
        m_q.push(6);
        m_q.push(11);
    
        // not ready for this yet...
        // workerThread.join();
    
        m_q.push(99);
    
        boost::this_thread::sleep(1000);
    
        m_q.push(50);
        m_q.push(30);
    
        std::cout << "main: done" << std::endl;  
    
        m_q.push(1);
        workerThread.join();
    
        return 0;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has

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.