http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5
4. Serializing our workload with strand
使用strand将任务排序
即使在多线程情况下,我们也希望任务能按照post的次序执行
// 1111.cpp : 定义控制台应用程序的入口点。
//
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>
boost::mutex global_stream_lock;
void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Start" << std::endl;
global_stream_lock.unlock();
io_service->run();
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Finish" << std::endl;
global_stream_lock.unlock();
}
void PrintNum(int x)
{
std::cout << "[" << boost::this_thread::get_id()
<< "] x: " << x << std::endl;
}
int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::asio::io_service::strand strand(*io_service);
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] The program will exit when all work has finished." << std::endl;
global_stream_lock.unlock();
boost::thread_group worker_threads;
for (int x = 0; x < 2; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
}
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
/*
strand.post( boost::bind( &PrintNum, 1 ) );
strand.post( boost::bind( &PrintNum, 2 ) );
strand.post( boost::bind( &PrintNum, 3 ) );
strand.post( boost::bind( &PrintNum, 4 ) );
strand.post( boost::bind( &PrintNum, 5 ) );
*/
io_service->post(boost::bind(&PrintNum, 1));
io_service->post(boost::bind(&PrintNum, 2));
io_service->post(boost::bind(&PrintNum, 3));
io_service->post(boost::bind(&PrintNum, 4));
io_service->post(boost::bind(&PrintNum, 5));
work.reset();
worker_threads.join_all();
return 0;
}
代码中开启了两个线程运行io_service的run函数
未使用strand来post任务时候 显示是不确定的
而使用了strand后 显示是按照输入的次序依次显示、
1 // deleteMe.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <boost/asio.hpp> 6 #include <boost/shared_ptr.hpp> 7 #include <boost/thread.hpp> 8 #include <boost/thread/mutex.hpp> 9 #include <boost/bind.hpp> 10 #include <iostream> 11 12 13 boost::mutex global_stream_lock; 14 15 void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service) 16 { 17 global_stream_lock.lock(); 18 std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl; 19 global_stream_lock.unlock(); 20 21 io_service->run(); 22 23 global_stream_lock.lock(); 24 std::cout << "[" << boost::this_thread::get_id() 25 << "] Thread Finish" << std::endl; 26 global_stream_lock.unlock(); 27 } 28 29 void PrintNum(int x) 30 { 31 std::cout << "[" << boost::this_thread::get_id() 32 << "] x: " << x << std::endl; 33 } 34 35 int main(int argc, char * argv[]) 36 { 37 boost::shared_ptr< boost::asio::io_service > io_service( 38 new boost::asio::io_service 39 ); 40 boost::shared_ptr< boost::asio::io_service::work > work( 41 new boost::asio::io_service::work(*io_service) 42 ); 43 boost::asio::io_service::strand strand(*io_service); 44 45 global_stream_lock.lock(); 46 std::cout << "[" << boost::this_thread::get_id() 47 << "] The program will exit when all work has finished." << std::endl; 48 global_stream_lock.unlock(); 49 50 boost::thread_group worker_threads; 51 for (int x = 0; x < 4; ++x) 52 { 53 worker_threads.create_thread(boost::bind(&WorkerThread, io_service)); 54 } 55 56 boost::this_thread::sleep(boost::posix_time::milliseconds(100)); 57 io_service->post(strand.wrap(boost::bind(&PrintNum, 1))); 58 io_service->post(strand.wrap(boost::bind(&PrintNum, 2))); 59 60 boost::this_thread::sleep(boost::posix_time::milliseconds(100)); 61 io_service->post(strand.wrap(boost::bind(&PrintNum, 3))); 62 io_service->post(strand.wrap(boost::bind(&PrintNum, 4))); 63 64 boost::this_thread::sleep(boost::posix_time::milliseconds(100)); 65 io_service->post(strand.wrap(boost::bind(&PrintNum, 5))); 66 io_service->post(strand.wrap(boost::bind(&PrintNum, 6))); 67 68 work.reset(); 69 70 worker_threads.join_all(); 71 72 return 0; 73 }