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.