Created
November 25, 2011 15:38
-
-
Save snaewe/1393807 to your computer and use it in GitHub Desktop.
run many jobs in a boost::asio::io_service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fixed example from: http://thisthread.blogspot.com/2011/04/multithreading-with-asio.html | |
void run (int tNumber) // 1. | |
{ | |
boost::asio::io_service svc; // 2. | |
boost::thread_group threads; | |
{ | |
std::auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(svc)); //3. | |
for (int i = 0; i < tNumber; ++i) // 4. | |
threads.create_thread (boost::bind (&boost::asio::io_service::run, &svc)); | |
svc.post(std::bind(jobOne, 2)); // 5. | |
svc.post(std::bind(jobOne, 1)); | |
svc.post(std::bind(jobTwo, 500)); | |
//svc.stop (); // 6. | |
} | |
threads.join_all (); // 7. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment