The callback of the timer inside a thread is not called

Hi,
I am trying to make the attached code run but no luck.
I use the compiler, gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.
I tried all combinations. The timer callback, handle_timeout is never
called.
The weird thing is that the similar code in windows using msvc works fine.
Could anybody help me out to figure out the issue in the code below?
Thanks a lot in advance,
mustafa
============================================================
#include

int main(int argc, char* argv[]) { boost::thread thrd_io(boost::bind(&boost::asio::io_service::run, &io_service));
The above will probably exit immediately, as it doesn't have work.
boost::thread workerThread(workerFunc); while ( true ) { try { io_service.run();
This one might also exit before another thread gives any work to the io_service. In short, try to create io_service::work before you call io_service::run() and see if it helps.

it worked! Thank you so much Igor. I changed it this way: int main(int argc, char* argv[]) { boost::asio::io_service::work work(io_service); boost::thread thrd_io(boost::bind(&boost::asio::io_service::run, &io_service)); boost::thread workerThread(workerFunc); ... } Can I ask why we needed io_service::work? in windows, it did not require it. Thanks, mustafa below is the output after i used it. $./timer main: startup main: after thrd_io main: after workerThread Worker: running Starting synchronous wait Finished synchronous wait Starting asynchronous wait timer is set to expire at '1336684315' Finished asynchronous wait handle_timeout Worker: finished -- View this message in context: http://boost.2283326.n4.nabble.com/The-callback-of-the-timer-inside-a-thread... Sent from the Boost - Users mailing list archive at Nabble.com.

it worked! Thank you so much Igor. I changed it this way: int main(int argc, char* argv[]) { boost::asio::io_service::work work(io_service); boost::thread thrd_io(boost::bind(&boost::asio::io_service::run, &io_service)); boost::thread workerThread(workerFunc); ... }
Can I ask why we needed io_service::work? in windows, it did not require it.
It's not a matter of platform, it's just a timing. You had a race condition, which produced different results under different circumstances.
participants (2)
-
Igor R
-
mozdemir