A thread pool for executing arbitrary tasks

Source: Internet
Author: User

Http://www.think-async.com/Asio/Recipes? Skin = clean. Nat % 2 Casio % 2 cpattern # a_thread_pool_for_executing_arbi

CreateIo_service:

 
ASIO: io_service;

And some work to stop itsRun ()Function from exiting if it has nothing else to do:

 
ASIO: io_service: Work (io_service );

Start some worker threads:

 
Boost: thread_group threads;
For (STD: size_t I = 0; I <my_thread_count; ++ I)
Threads. create_thread (boost: BIND (& ASIO: io_service: Run, & io_service ));

Post the tasks toIo_serviceSo they can be saved med by the worker threads:

Io_service.post (boost: BIND (an_expensive_calculation, 42 ));
Io_service.post (boost: BIND (a_long_running_task, 123 ));

Finally, before the program exits shut downIo_serviceAnd wait for all threads to exit:

 
Io_service.stop ();
Threads. join_all ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.