简体   繁体   中英

Thread pool Implementation using Boost::thread class

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM