参考来源https://www.gamedev.net/blogs/entry/2249317-a-guide-to-getting-started-with-boostasio/
错误的处理
Example 5a
#include <iostream>
#include <asio.hpp>
#include <thread>
#include <vector>
#include<mutex>
#include <functional>
#include <chrono>
std::mutex global_lock;
void workerThread(std::shared_ptr<asio::io_service> io_service ){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" start"<<std::endl;
global_lock.unlock();
try{
io_service->run();
}
catch(std::exception &ex){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" Exception: "<<ex.what()<<std::endl;
global_lock.unlock();
}
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" end"<<std::endl;
global_lock.unlock();
}
void raiseException(std::shared_ptr<asio::io_service> io_service){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" "<<__FUNCTION__<<std::endl;
global_lock.unlock();
io_service->post(std::bind(raiseException,io_service));
throw(std::runtime_error("0ops!"));
}
int main(int argc,char * argv[]){
std::shared_ptr<asio::io_service> io_service(
new asio::io_service);
std::shared_ptr<asio::io_service::work> work(
new asio::io_service::work(*io_service));
// asio::io_service::strand strand(*io_service);
std::cout<<"Press Enter to exit"<<std::endl;
std::vector<std::thread> ntid;
for(int i=0;i<4;i++)
{
ntid.push_back(std::thread(workerThread,io_service));
}
io_service->post(std::bind(raiseException,io_service));
//std::cin.get();
//io_service->stop();
for(auto &iter:ntid){
iter.join();
}
return 0;
}
结果输出:
The reason is because the exception propagated through the run function, so the worker threads exited. Since all worker threads exited, the program is done since join_all returns.
Example 5b
#include <iostream>
#include <asio.hpp>
#include <thread>
#include <vector>
#include<mutex>
#include <functional>
#include <chrono>
std::mutex global_lock;
void workerThread(std::shared_ptr<asio::io_service> io_service ){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" start"<<std::endl;
global_lock.unlock();
asio::error_code ec;
io_service->run(ec);
if(ec){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" Exception: "<<ec<<std::endl;
global_lock.unlock();
}
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" end"<<std::endl;
global_lock.unlock();
}
void raiseException(std::shared_ptr<asio::io_service> io_service){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" "<<__FUNCTION__<<std::endl;
global_lock.unlock();
io_service->post(std::bind(raiseException,io_service));
throw(std::runtime_error("0ops!"));
}
int main(int argc,char * argv[]){
std::shared_ptr<asio::io_service> io_service(
new asio::io_service);
std::shared_ptr<asio::io_service::work> work(
new asio::io_service::work(*io_service));
// asio::io_service::strand strand(*io_service);
std::cout<<"Press Enter to exit"<<std::endl;
std::vector<std::thread> ntid;
for(int i=0;i<4;i++)
{
ntid.push_back(std::thread(workerThread,io_service));
}
io_service->post(std::bind(raiseException,io_service));
//std::cin.get();
//io_service->stop();
for(auto &iter:ntid){
iter.join();
}
return 0;
}
如果传入asio::error_code变量,则无法捕获到异常。程序终止!
Example 5c
综上两种方式,有
#include <iostream>
#include <asio.hpp>
#include <thread>
#include <vector>
#include<mutex>
#include <functional>
#include <chrono>
std::mutex global_lock;
void workerThread(std::shared_ptr<asio::io_service> io_service ){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" start"<<std::endl;
global_lock.unlock();
try{
asio::error_code ec;
io_service->run(ec);
if(ec){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" Error: "<<ec<<std::endl;
global_lock.unlock();
}
}
catch(std::exception &ex){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" Exception: "<<ex.what()<<std::endl;
global_lock.unlock();
}
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" end"<<std::endl;
global_lock.unlock();
}
void raiseException(std::shared_ptr<asio::io_service> io_service){
global_lock.lock();
std::cout<<std::this_thread::get_id()<<" "<<__FUNCTION__<<std::endl;
global_lock.unlock();
io_service->post(std::bind(raiseException,io_service));
throw(std::runtime_error("0ops!"));
}
int main(int argc,char * argv[]){
std::shared_ptr<asio::io_service> io_service(
new asio::io_service);
std::shared_ptr<asio::io_service::work> work(
new asio::io_service::work(*io_service));
// asio::io_service::strand strand(*io_service);
std::cout<<"Press Enter to exit"<<std::endl;
std::vector<std::thread> ntid;
for(int i=0;i<4;i++)
{
ntid.push_back(std::thread(workerThread,io_service));
}
io_service->post(std::bind(raiseException,io_service));
//std::cin.get();
//io_service->stop();
for(auto &iter:ntid){
iter.join();
}
return 0;
}