Boost.Fiber是否会自动产生网络请求(如果我理解正确的话,它们会在I/O期间产生CPU ),例如网络上的数据库调用?我想用它来设置阻塞的数据库调用,在其中我插入了大量的信息,并且在接收到这些信息时需要插入大量的小记录。我的另一个选择是libdill/libmill进行微线程处理。
总的想法是:
纤维的工作方式和我想象的一样吗?
这是使用RapidJSON和boost.fiber的代码的简化基本结构,但我从网络(Websocket)获得消息:
inline
void doDatabaseCall( Document &msg ) {
//Get connection & do query with libpqxx
}
Document msg;
msg.Parse( "{\"myVar\": \"test\"}" );
Document msg2;
msg2.Parse( "{\"myVar\": \"test2\"}" );
boost::fibers::fiber f1( doDatabaseCall, msg);
boost::fibers::fiber f2( doDatabaseCall, msg2);
f1.join();
f2.join();发布于 2018-03-30 23:17:34
发布于 2018-04-04 06:11:01
boost.fiber确实与boost.asio集成-您需要安装一个特殊的调度程序 -示例在boost.fiber /子目录示例中提供)。
int main( int argc, char* argv[]) {
try {
std::shared_ptr< boost::asio::io_service > io_svc = std::make_shared< boost::asio::io_service >();
boost::fibers::use_scheduling_algorithm< boost::fibers::asio::round_robin >( io_svc);
// server
tcp::acceptor a( * io_svc, tcp::endpoint( tcp::v4(), 9999) );
boost::fibers::fiber( server, io_svc, std::ref( a) ).detach();
// client
const unsigned iterations = 2;
const unsigned clients = 3;
boost::fibers::barrier b( clients);
for ( unsigned i = 0; i < clients; ++i) {
boost::fibers::fiber(
client, io_svc, std::ref( a), std::ref( b), iterations).detach();
}
io_svc->run();
std::cout << "done." << std::endl;
return EXIT_SUCCESS;
} catch ( std::exception const& e) {
print("Exception: ", e.what(), "\n");
}
return 0;
}https://stackoverflow.com/questions/49578241
复制相似问题