asio::steady_timer无法与“use_awaitable”完成处理程序一起使用。
创始人
2024-11-11 09:30:25
0

使用底层的操作系统定时器(例如Windows API的SetTimer函数)代替asio::steady_timer,然后使用co_await在协程中等待操作完成。以下是示例代码:

#include #include #include #include

using namespace std; using namespace std::experimental;

class TimerAwaitable { public: TimerAwaitable() { timer_id_ = SetTimer(NULL, 0, 1000, &TimerAwaitable::timer_callback_); // 如果使用boost.asio,可以将SetTimer换成asio::steady_timer // timer_.expires_after(chrono::seconds(1)); // timer_.async_wait(TimerAwaitable::timer_callback_); } ~TimerAwaitable() { KillTimer(NULL, timer_id_); // 如果使用boost.asio,可以使用timer_.cancel() } bool await_ready() const { return false; } void await_suspend(coroutine_handle<> hdl) const { coro_handle_ = hdl; } void await_resume() const {

}
static VOID CALLBACK timer_callback_(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
    coro_handle_.resume();
}

private: UINT_PTR timer_id_; static coroutine_handle<> coro_handle_; };

coroutine_handle<> TimerAwaitable::coro_handle_ = nullptr;

int main() { auto timer_awaitable = TimerAwaitable(); for (int i = 0; i < 5; ++i) { co_await timer_awaitable; cout << "tick" << endl; } return 0; }

相关内容

热门资讯

9分钟讲义!wepoker一直... 9分钟讲义!wepoker一直输的号能继续打吗(透视)其实存在有辅助插件(哔哩哔哩)1、玩家可以在w...
第一分钟大纲!wepoker辅... 第一分钟大纲!wepoker辅助器安装包定制(透视)竟然真的是有辅助透视(哔哩哔哩)1、wepoke...
第三分钟机巧!德州局怎么透视(... 第三分钟机巧!德州局怎么透视(透视)本来是真的有辅助插件(哔哩哔哩)1.德州局怎么透视 选牌创建新账...
第二分钟方针!pokernow... 第二分钟方针!pokernow辅助工具(透视)真是真的有辅助神器(哔哩哔哩)所有人都在同一条线上,像...
第1分钟窍要!we-poker... 第1分钟窍要!we-poker是什么软件(透视)真是有辅助神器(哔哩哔哩)1、全新机制【we-pok...
第1分钟阶段!wepoker科... 第1分钟阶段!wepoker科技辅助器(透视)一直有辅助安装(哔哩哔哩)1、超多福利:超高返利,海量...
3分钟总结!hhpoker可以... 3分钟总结!hhpoker可以控制牌吗(透视)本来是真的有辅助工具(哔哩哔哩)1、任何hhpoker...
四分钟项目!wpk模拟器(透视... 四分钟项目!wpk模拟器(透视)切实是真的有辅助插件(哔哩哔哩)亲,关键说明,wpk模拟器透视脚本安...
十分钟举措!aa poker辅... 十分钟举措!aa poker辅助(透视)都是是有辅助插件(哔哩哔哩)1、aa poker辅助公共底牌...
第2分钟演示!hhpoker底... 第2分钟演示!hhpoker底牌透视脚本(透视)好像真的有辅助开挂(哔哩哔哩)1、每一步都需要思考,...