Managing io_service in another thread, weird problem with exceptions
I have been struggling over this problem for a wee while in my application and so I have tried to reproduce the problem in a simple code snippet.
I am using Boost 1.44 and mingw32-g++ version 4.4.1. on windows XP
My expected result for the following program is:
>messageservice.exe
[debug]BEFORE EXCEPTION ****
[error]EXCEPTION HANDLED ****
[debug]AFTER TRY/CATCH
>
But in reality it sometimes does this, sometimes crashes verbosely "unhandled win32 exception..." (with Just In Time debugger) , and sometimes it crashes silently
So I am at a loss as to why it does this.
If I remove the try...catch block the program runs just fine.
Here is the code:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#define L_(lvl) std::cout<<"开发者_高级运维\n["<<#lvl<<"]"
void nothingfun() { } /* an empty job */
void threadfun() {
boost::asio::io_service myIoService; /* thread's own io_service object */
for(int i=0;i<10000;i++) {
myIoService.post(¬hingfun);
}
}
int main()
{
boost::thread t(&threadfun);
L_(debug)<<"BEFORE EXCEPTION ****";
try{
throw "aaah!";
} catch(...) {
L_(error)<<"EXCEPTION HANDLED ****";
}
L_(debug)<<"AFTER TRY/CATCH";
t.join();
return 0;
}
Is it illegal to manage an io_service instance in a non-main thread? or have I done something blindingly obviously wrong!?
Appreciate any help!
Thanks
Thanks to Matt Gruenke for the solution (http://news.gmane.org/gmane.comp.lib.boost.asio.user)
Apparently having thread safe exceptions is an optional extra in mingw. I needed to specify the -mthreads option when linking
I guess im naive in thinking that this should be an always-on core guarantee!
精彩评论