[Boost] ASIO explanation of boost Library 1 -- difference between strand and io_service

Source: Internet
Author: User
Namespace {// strand provides serial execution to ensure thread security. The post or dispatch method will not be executed concurrently. // io_service cannot guarantee thread security. Boost: ASIO: io_service m_service; Boost: ASIO: strand m_strand (m_service); Boost: mutex m_mutex; void print (int id) {// boost: mutex: scoped_lock lock (m_mutex); static int COUNT = 0; print_debug ("ID:" <boost: lexical_cast <STD :: string> (ID); print_debug ("count:" <boost: lexical_cast <STD: String> (++ count);} void iorun1 () {While (true) {m_service.run () ;}} void iorun2 () {While (true) {m_service.run () ;}} void strand_print1 () {// print_debug ("Enter print1"); m_strand.dispatch (boost: BIND (print, 1); // print_debug ("Exit print1");} void strand_print2 () {// print_debug ("Enter print2"); m_strand.post (boost: BIND (print, 2); // print_debug ("Exit print2");} void strand_print3 () {// print_debug ("Enter print3"); m_strand.post (boost: BIND (print, 3); // print_debug ("Exit print3");} void strand_print4 () {// print_debug ("Enter print4"); m_strand.post (boost: BIND (print, 4); // print_debug ("Exit print4 ");} // After m_strand is replaced with m_service, void service_print1 () {// print_debug ("Enter print1"); m_service.dispatch (boost: BIND (print, 1 )); // print_debug ("Exit print1");} void service_print2 () {// print_debug ("Enter print2"); m_service.post (boost: BIND (print, 2 )); // print_debug ("Exit print2");} void service_print3 () {// print_debug ("Enter print3"); m_service.post (boost: BIND (print, 3 )); // print_debug ("Exit print3");} void service_print4 () {// print_debug ("Enter print4"); m_service.post (boost: BIND (print, 4 )); // print_debug ("Exit print4") ;}} void test_strand () {boost: thread ios1 (iorun1); Boost: thread ios2 (iorun2); Boost :: thread T1 (strand_print1); Boost: thread T2 (strand_print2); Boost: thread T3 (strand_print3); Boost: thread T4 (strand_print4); t1.join (); t2.join (); t3.join (); t4.join (); m_server.run ();} void test_service () {boost: thread ios1 (iorun1); Boost: thread ios2 (iorun2 ); boost: thread T1 (service_print1); Boost: thread T2 (service_print2); Boost: thread T3 (service_print3); Boost: thread T4 (service_print4); t1.join (); t2.join (); t3.join (); t4.join (); m_service.run ();}

Execution result of test_strand:

 
17:25:34 626 [8228] Debug-ID: 42013-01-05 17:25:34 631 [8228] Debug-count: 12013-01-05 17:25:34 634 [5692] Debug-ID: 12013-01-05 17:25:34 637 [5692] Debug-count: 22013-01-05 17:25:34 640 [5692] Debug-ID: 22013-01-05 17:25:34 642 [5692] Debug-count: 32013-01-05 17:25:34 646 [5692] Debug-ID: 32013-01-05 17:25:34 649 [5692] Debug-count: 4

Execution result of test_ioserivice:

 
17:26:28 071 [3236] Debug-ID: 12013-01-05 17:26:28 071 [5768] Debug-ID: 22013-01-05 17:26:28 071 [5108] Debug-ID: 32013-01-05 17:26:28 076 [3236] Debug-count: 12013-01-05 17:26:28 079 [5768] Debug-count: 22013-01-05 17:26:28 083 [5108] Debug-count: 32013-01-05 17:26:28 087 [3236] Debug-ID: 42013-01-05 17:26:28 099 [3236] Debug-count: 4

From the results, we can see that the print functions in test_strand are executed in pairs, and there is no thread security for the print functions in test_ioservice.
To ensure synchronization of test_ioservice, you must add mutexCodeThe commented sentence.

Note that the log thread number shows that the true execution of print () is the main thread, ios1, ios2, while the T1, T2, T3, and T4 threads only add tasks to the ioservice queue.

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.