Question

I want to implement thread pool using boost::thread class.

I am able to create the threads using below line.

boost::thread Consumer_1(consume); 

where consumer_1 is thread and consume is the function bound to it.

Above statement starts the thread as soon as it gets executed.

Now I just want to create the thread and do the binding run time.

I have not yet discovered the boost method to delay this binding.

Can anyone help on this?

Was it helpful?

Solution

The binding can't be done later. For principal reasons—a thread of execution has to be executing something.

What you need to do is create a function, that will take jobs, represented as boost::function, from a queue and execute them. Than run this function in one or more threads.

I am not sure there is a thread-safe queue, but you can always use a regular std::deque with boost::condition_variable for waking up the threads and boost::mutex for locking the deque.

You might want to look at Boost.Asio too. See also here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top