Reputation: 54103
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.
Upvotes: 2
Views: 2440
Reputation: 19022
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;
}
Upvotes: 2