Creating a work_queue class

Let's call the functional object that takes no arguments (a task, in short).

typedef boost::function<void()> task_t;

And now, imagine a situation where we have threads that post tasks and threads that execute posted tasks. We need to design a class that can be safely used by both types of thread. This class must have methods for getting a task (or blocking and waiting for a task until it is posted by another thread), checking and getting a task if we have one (returning an empty task if no tasks remain), and a method to post tasks.

Getting ready

Make sure that you feel comfortable with boost::thread or std::thread and know some basics of mutexes.

How to do it...

The classes that we are going to implement will be close in ...

Get Boost C++ Application Development Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.