Reputation: 677
I found that accessing tcp::socket
from a std::thread will cause program terminated.
Here's the sample program from boost.
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/echo/blocking_tcp_echo_server.cpp
Compile it: g++ blocking_tcp_echo_server.cpp -std=c++11 -lboost_system -lboost_thread -pthread
So far, everthing works fine.
But if you replace the boost::thread with std::thread (and #include ), the program will crash(terminated) when it access sock member ( socket::read_some()
).
Error message: terminate called without an active exception.
Any idea?
Upvotes: 2
Views: 932
Reputation: 3982
That's the difference between boost::thread
and std::thread
. I have seen the code, and you can fix it to work with std::thread
, just like this:
void server(boost::asio::io_service& io_service, short port) {
// ...
std::thread t(boost::bind(session, sock));
t.detach();
}
It seems you must detach or join the thread when you use std::thread
.
Code:
#include <iostream>
#include <thread>
int main(void) {
std::thread t([](){std::cout << "will throw exception" << std::endl;});
// t.detach();
return 0;
}
It will throw exception if not detach or not join or not link pthread
Upvotes: 1