boost::thread and boost::asio::io_service questions
I am trying to understand both of these items and how they are executing in
our code, so I made a small demo to debug through.
1) Why can't I pass boost::io_service by reference in the call, boost::thread
workerThread(DoWork, 1, ioService); with a function signature of void DoWork
(const unsigned int threadNum, boost::asio::io_service * ioService) ? I
changed it to a pointer and it works, but I don't know why I can't do it by
reference.
2) If boost::thread is not copyable, how can I store a collection of "thread
handles" or boost::thread objects like the variable workerThread? I can't use
the std collections I would usually use...
3) When stepping through the code, the debugger jumps between the main thread,
the worker thread, and a call stack in the depths of boost code where one item
points to the line where the call to ioService->run() is. The first two make
sense. I don't understand the latter.
The call stack looks very much like one we get in production code and is all I
have to go on in trying to debug a problem. The challenge is trying to
determing who created the thread and find that line in source, because the
production code is keeps creating threads until the program crashes.
#include <iostream>
#include
On Fri, 10 Jun 2011 22:27:49 +0200, Christopher Pisz
1) Why can't I pass boost::io_service by reference in the call, boost::thread workerThread(DoWork, 1, ioService); with a function signature of void DoWork (const unsigned int threadNum, boost::asio::io_service * ioService) ? I changed it to a pointer and it works, but I don't know why I can't do it by reference.
Use boost::ref: boost::thread workerThread(DoWork, 1, boost::ref(ioService));
2) If boost::thread is not copyable, how can I store a collection of "thread handles" or boost::thread objects like the variable workerThread? I can't use the std collections I would usually use...
You can create boost::thread objects with new and put the pointers in boost::shared_ptrs or a pointer container like boost::ptr_vector. HTH, Boris
[...]
participants (2)
-
Boris Schaeling
-
Christopher Pisz