boost asio timer 定時器
阿新 • • 發佈:2019-01-28
前提條件安裝好boost1.68 其他版本的函式可能有變動
main.cpp
#include <iostream> #include <boost/asio.hpp> int main() { // 每個程序都至少有一個io_context 的物件, 此類提供對I/O功能的訪問 boost::asio::io_context io; /* 宣告一個 boost::asio::steady_timer的物件, I/O功能的類asio類始終將io_server類作為建構函式的第一個引數 第二個引數定時器設定是從現在起5秒後到期*/ boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); /* 將等待定期器到期, 不會繼續往下執行 */ t.wait(); std::cout << "hello world" << std::endl; system("pause"); return 0; }
main.cpp
#include "stdafx.h" #include "SteadyTimer.h" void print(const boost::system::error_code&) { std::cout << "hello world" << std::endl; } int main() { // 每個程序都至少有一個io_context 的物件, 此類提供對I/O功能的訪問 boost::asio::io_context io; /* 宣告一個 boost::asio::steady_timer的物件, I/O功能的類asio類始終將io_server類作為建構函式的第一個引數 第二個引數定時器設定是從現在起5秒後到期*/ boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); /* 非同步等待定時器結束, 當結束時會回撥print函式 */ t.async_wait(print); /* asio庫提供了一種保護, 只能呼叫io_context::run()的執行緒回撥處理程式,除非呼叫run()函式,否則永遠不會呼叫非同步等待完成的回撥 */ io.run(); system("pause"); return 0; }
#include "stdafx.h" #include "SteadyTimer.h" void print(const boost::system::error_code& /*e*/, boost::asio::steady_timer* t, int* count) { if (*count < 5) { std::cout << *count << std::endl; ++(*count); t->expires_at(t->expiry() + boost::asio::chrono::seconds(1)); t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count)); } } int main() { boost::asio::io_context io; int count = 0; boost::asio::steady_timer t(io, boost::asio::chrono::seconds(1)); /* 我們在計時器上啟動一個新的非同步等待。如您所見,boost::bind()函式用於將額外引數與您的回撥處理程式相關聯。 steady_timer::async_wait() 函式需要一個回撥函式與錯誤碼 void(const boost::system::error_code&)。 以及你要傳進去的每個引數 */ t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count)); io.run(); std::cout << "Final count is " << count << std::endl; system("pause"); return 0; }
執行結果
#include "stdafx.h"
#include "SteadyTimer.h"
class printer
{
public:
printer(boost::asio::io_context& io)
: timer_(io, boost::asio::chrono::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this));
}
~printer()
{
std::cout << "Final count is " << count_ << std::endl;
}
void print()
{
if (count_ < 5)
{
std::cout << count_ << std::endl;
++count_;
timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
}
private:
boost::asio::steady_timer timer_;
int count_;
};
int main()
{
boost::asio::io_context io;
printer p(io);
io.run();
return 0;
}
注意boost::bind()函式
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
class printer
{
public:
printer(boost::asio::io_context& io)
: strand_(io),
timer1_(io, boost::asio::chrono::seconds(1)),
timer2_(io, boost::asio::chrono::seconds(1)),
count_(0)
{
timer1_.async_wait(boost::asio::bind_executor(strand_,
boost::bind(&printer::print1, this)));
timer2_.async_wait(boost::asio::bind_executor(strand_,
boost::bind(&printer::print2, this)));
}
~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_.expiry() + boost::asio::chrono::seconds(1));
timer1_.async_wait(boost::asio::bind_executor(strand_,
boost::bind(&printer::print1, this)));
}
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << std::endl;
++count_;
timer2_.expires_at(timer2_.expiry() + boost::asio::chrono::seconds(1));
timer2_.async_wait(boost::asio::bind_executor(strand_,
boost::bind(&printer::print2, this)));
}
}
private:
boost::asio::io_context::strand strand_;
boost::asio::steady_timer timer1_;
boost::asio::steady_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_context io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_context::run, &io));
io.run();
t.join();
return 0;
}