惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
博客园_首页
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
G
Google Developers Blog
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements

Jia Yue Hua

完美转发不完美 闲散颂 adapting c++20 ranges algorithms for most metaprogramming Reproducible github Developer Environments 使用mpmcpipeline和jthread实现软流水 在其它线程周期回调函数 for_each cpo和tag_invoke transparent,为关联容器增加查找成员
在当前线程周期回调函数
Jia Yue Hua · 2023-05-21 · via Jia Yue Hua

在当前线程周期回调函数

FunctionScheduler 可在当前线程周期回调函数

#include <folly/experimental/FunctionScheduler.h>
#include <iostream>
#include <gtest/gtest.h>
using namespace folly;
using namespace std;
TEST(repeatfunc,repeatfunc) { 
  FunctionScheduler fs;
  fs.addFunction([]() { cout << "Hello, world!" << endl; }, 1s);
  fs.start();
  this_thread::sleep_for(10s);
  fs.cancelAllFunctions();
}

只需要addFunction时指明要回调的函数和时间间隔。调用start启动,当不再需要回调时调用cancelAllFunctions. 本例中,每隔1s打印一次”Hello, world!”,共打印10次。

可以看到,比使用folly eventbase的asynctimer简单很多。

Posted 2023-05-21