Created
February 15, 2012 03:05
-
-
Save DarrenCook/1832811 to your computer and use it in GitHub Desktop.
Use two io_services, with strand for timer, but not for PrintNum
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
| #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(); | |
| while( true ) | |
| { | |
| try | |
| { | |
| boost::system::error_code ec; | |
| io_service->run( ec ); | |
| if( ec ) | |
| { | |
| global_stream_lock.lock(); | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] Error: " << ec << std::endl; | |
| global_stream_lock.unlock(); | |
| } | |
| break; | |
| } | |
| catch( std::exception & ex ) | |
| { | |
| global_stream_lock.lock(); | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] Exception: " << ex.what() << std::endl; | |
| global_stream_lock.unlock(); | |
| } | |
| } | |
| global_stream_lock.lock(); | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] Thread Finish" << std::endl; | |
| global_stream_lock.unlock(); | |
| } | |
| void TimerHandler( | |
| const boost::system::error_code & error, | |
| boost::shared_ptr< boost::asio::deadline_timer > timer, | |
| boost::shared_ptr< boost::asio::io_service::strand > strand | |
| ) | |
| { | |
| if( error ) | |
| { | |
| global_stream_lock.lock(); | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] Error: " << error << std::endl; | |
| global_stream_lock.unlock(); | |
| } | |
| else | |
| { | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] TimerHandler " << std::endl; | |
| timer->expires_from_now( boost::posix_time::seconds( 1 ) ); | |
| timer->async_wait( | |
| strand->wrap( boost::bind( &TimerHandler, _1, timer, strand ) ) | |
| ); | |
| } | |
| } | |
| void PrintNum( int x ) | |
| { | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] x: " << x << std::endl; | |
| boost::this_thread::sleep( boost::posix_time::milliseconds( 1500 ) ); | |
| } | |
| 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 > io_service2( | |
| new boost::asio::io_service | |
| ); | |
| boost::shared_ptr< boost::asio::io_service::work > work( | |
| new boost::asio::io_service::work( *io_service ) | |
| ); | |
| boost::shared_ptr< boost::asio::io_service::work > work2( | |
| new boost::asio::io_service::work( *io_service2 ) | |
| ); | |
| boost::shared_ptr< boost::asio::io_service::strand > strand2( | |
| new boost::asio::io_service::strand( *io_service2 ) | |
| ); | |
| global_stream_lock.lock(); | |
| std::cout << "[" << boost::this_thread::get_id() | |
| << "] Press [return] to exit." << 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, x==0? io_service2 : io_service ) ); | |
| } | |
| boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) ); //Give threads time to start up | |
| 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 ) ); | |
| boost::shared_ptr< boost::asio::deadline_timer > timer( | |
| new boost::asio::deadline_timer( *io_service2 ) | |
| ); | |
| timer->expires_from_now( boost::posix_time::milliseconds( 1000 ) ); | |
| timer->async_wait( | |
| strand2->wrap( boost::bind( &TimerHandler, _1, timer, strand2 ) ) | |
| ); | |
| std::cin.get(); | |
| io_service->stop(); | |
| io_service2->stop(); | |
| worker_threads.join_all(); | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
[0x18b74b0] Press [return] to exit.
[0x18b7620] Thread Start
[0x18b7900] Thread Start
[0x18b7900] x: 1
[0x18b7620] TimerHandler
[0x18b7900] x: 2
[0x18b7620] TimerHandler
[0x18b7900] x: 3
[0x18b7620] TimerHandler
[0x18b7620] TimerHandler
[0x18b7900] x: 4
[0x18b7620] TimerHandler
[0x18b7900] x: 5
[0x18b7620] TimerHandler
[0x18b7620] TimerHandler
[0x18b7900] Thread Finish
[0x18b7620] Thread Finish