ASIO Tutorial 5 - Synchronising handlers in a multithreaded programs - Doesn't use both threads for processing

Using the code from http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tuttimer5.... with a few extra cout's for extra info. If you print out the thread IDs of at the start of print1 and print2 you'll see that both methods are actually using the same thread. ie two threads gets created but only one does any work. I'm printing the thread ID's via std::cout << "print1 " << boost::this_thread::get_id() << std::endl; std::cout << "print2 " << boost::this_thread::get_id() << std::endl; at the top of print 1 and print 2. I first noticed this when I was single line stepping through the code trying to work out how the second thread could possibly be doing anywork since t.join() is called after io.run(). Also if I comment out io.run() the program still works the same as before, except its the second thread doing all the processing. Compiler MSVC 9.0, happens on both debug and release.

AMDG mark wrote:
Using the code from http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tuttimer5.... with a few extra cout's for extra info.
If you print out the thread IDs of at the start of print1 and print2 you'll see that both methods are actually using the same thread. ie two threads gets created but only one does any work.
I'm printing the thread ID's via std::cout << "print1 " << boost::this_thread::get_id() << std::endl; std::cout << "print2 " << boost::this_thread::get_id() << std::endl;
at the top of print 1 and print 2.
I first noticed this when I was single line stepping through the code trying to work out how the second thread could possibly be doing anywork since t.join() is called after io.run(). Also if I comment out io.run() the program still works the same as before, except its the second thread doing all the processing.
Compiler MSVC 9.0, happens on both debug and release.
Sure. For such a tiny program there is no guarantee that the work will be split evenly between threads. If there were enough work to keep both the threads busy then they would both be used, but in this program, both threads spend all their time waiting for the timer. In Christ, Steven Watanabe
participants (2)
-
mark
-
Steven Watanabe