《C++Boost库asio》第四篇 io_server在多线程下如何使用
io_server在多线程下如何使用:因为有可能最终逻辑是,ioserver只处理网路部分的信息,外带time,把接收玩家信息和发送放到一
个额外线程里,主循环跑的是主逻辑。也有可能在翻过来让iosever在主线程,逻辑在分线程,2钟方
法都可以
int main() {
boost::asio::io_service io;
printer p (io);//一定要等所有事件定义好了才能开线程
std::thread t([&io]{io.run();});//逻辑有可能在分线程跑
io.run();//逻辑也有可能在主线程做处理
t.join();
return;
}
多线程如何同步回调:
boost::asio::io_service::strand strand_;
timer1_.async_wait(strand_.wrap((const auto&){this->print1();}));
timer2_.async_wait(strand_.wrap((const auto&){this->print2();}));
strand_.wrap这个可以保证在多线程中执行的顺序一定是按照你定义的顺序来的,如果1没有执行,2
就不会先执行
注意:
1.一定要等所有事件定义好了才能开线程
2.io.run一旦结束,就进入到stop状态,其他线程下的io.run也跟着改变了,如果要再次启动,一定
要调用
if (io.stopped()) {
io.reset();
}
例子:
#include <iostream>
#include <boost/asio.hpp>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer {
public:
printer(boost:;asio::io_server &io) : strand_(io), timer1_
(io,boost::posix_timer::seconds(1)), timer2_(io,boost::posix_time::seconds(1)),count_(0)
{
timer1_.async_wait(strand_.wrap((const auto&){this->print1();}));
timer2_.async_wait(strand_.wrap((const auto&){this->print2();}));
}
~printer() { std::cout << "Final count is " << count_<< std::endl; }
void print1() {
if (count_< 10){
std::cout << "Timer 1: " << count_ << std:: endl;
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap((const auto&){this->print1();}));
}
void print2() {
if (count_< 10){
std::cout << "Timer 2: " << count_ << std:: endl;
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap((const auto&){this->print2;}));
}
}
private:
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer1_;
boost:;asio:;deadline_timer timer2_;
int count_;
};
int main() {
boost:;asio::io_service io;
printer p(io);
std::thread t([&io]{io.run();});
io.run();
t.join();
return 0;
}
io_server在多线程下如何使用
同步回调
#include <iostream>
#include <boost/asio.hpp>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost:; asio::io_server &io) :
strand_(io),
timer1_(io,boost::posix_timer::seconds(1)),
timer2_(io,boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(strand_.wrap((const auto&)
{
this->print1();
}));
timer2_.async_wait(strand_.wrap((const auto&)
{
this->print2();
}));
}
~printer()
{
std::cout << "Final count is " << count_<< std::endl;
}
void print1()
{
if (count_< 10)
{
std::cout << "Timer 1: " << count_ << std:: endl;
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap((const auto&)
{
this->print1();
}));
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << std:: endl;
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap((const auto&)
{
this->print2;
}));
}
}
private:
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer1_;
boost:; asio:; deadline_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_service io;
//一定要等所有事件定义好了才能开线程
printer p (io);
std::thread t([&io] {io.run();}); //逻辑有可能在分线程跑
io.run();//逻辑也有可能在主线程做处理
t.join();// 断开;
return;
}
注意:
1.一定要等所有事件定义好了才能开线程
2.io.run一旦结束,就进入到stop状态,其他线程下的io.run也跟着改变了,如果要再次启动,一定要调用
if (io.stopped()) {
io.reset();
}
页:
[1]