当前位置: 首页 > news >正文

2022中文无字幕入口网站中国大规模建设合肥

2022中文无字幕入口网站,中国大规模建设合肥,重庆网络推广培训,优质的seo网站排名优化软件为什么写这个 C17异步任务需要future和promise配合使用#xff0c;不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include async_callback.h #include unistd.h #includ…为什么写这个 C17异步任务需要future和promise配合使用不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include async_callback.h #include unistd.h #include iostream using namespace std;int main() {int a 0, b 0, c 0;AsyncTask* task1 new AsyncTask([a](){sleep(2);for (int i 0; i 10000; i) {a;}});AsyncTask* task2 new AsyncTask([b](){sleep(2);for (int i 0; i 10000; i) {b;}});if (!task1-get(1)) {cout task1超时 endl;}if (task2-get()) {cout task2没超时 endl;}task1-restart();task2-restart();task1-get();task2-get();cout a endl;cout b endl;cout task1执行时间: task1-executionTime() endl;cout task2执行时间: task2-executionTime() endl;return 0; }打印结果 task1超时 task2没超时 20000 20000 task1执行时间:2.00009 task2执行时间:2.00009代码 // Simple asynchronous tasks with timeout // Author: Y. F. Zhang // Date: 2023-09-21#ifndef ASYNC_CALLBACK_H #define ASYNC_CALLBACK_H#include mutex #include condition_variable #include thread #include atomic #include functional #include exception #include iostream #include sys/time.h #include time.h class Timer { public:Timer() {offsetTime_ 0;timerState_ TIMERSTOP;}void timerStart() {timerState_ TIMERRUN;timeStamp_ getWallTime();}double elapsedTime() {if (timerState_ TIMERPAUSE) {return offsetTime_;}curTime_ getWallTime() - timeStamp_ offsetTime_;if (timerState_ TIMERSTOP) return 0;return curTime_;}void pauseTimer() {offsetTime_ getWallTime() - timeStamp_; timerState_ TIMERPAUSE;}void stopTimer() {offsetTime_ 0;curTime_ 0;timerState_ TIMERSTOP;}private:double getWallTime() {struct timeval time ;if (gettimeofday(time,NULL)){return 0;}return (double)time.tv_sec (double)time.tv_usec * .000001;}double timeStamp_;double curTime_;double offsetTime_;enum{TIMERSTOP,TIMERRUN,TIMERPAUSE} timerState_; };namespace AsyncTaskException {class GetButTaskStopErr: public std::exception {const char* what() const throw () {return invoke get method but task is stop!;} };class GetExecutionTimeButTaskStopErr: public std::exception {const char* what() const throw () {return invoke executionTime method but task is stop!;} };}using func std::functionvoid(); class AsyncTask { private:enum {TASKRUNNING,TASKSTOP,TASKFINISHED} taskState_;func callback_;std::mutex mtx_;std::condition_variable cond_;std::atomic_bool completedFlag_;std::thread* taskThread_;Timer timer_;void initTask() {timer_.stopTimer();taskState_ TASKSTOP;completedFlag_.store(false);if (taskThread_ ! nullptr) {delete taskThread_;taskThread_ nullptr;}}void runTask() {auto wrapperCallback [this]() {try {timer_.timerStart();callback_();timer_.pauseTimer();completedFlag_.store(true);} catch (std::exception e) {fprintf(stderr, %s, e.what());}taskState_ TASKFINISHED;cond_.notify_one();};taskState_ TASKRUNNING;taskThread_ new std::thread(wrapperCallback);taskThread_-detach();} public:AsyncTask(func callback) {this-callback_ callback;restart();}void restart() {initTask();runTask();}double executionTime() {if (taskState_ TASKSTOP) {throw AsyncTaskException::GetExecutionTimeButTaskStopErr();}return timer_.elapsedTime();}~AsyncTask() {if (taskThread_ ! nullptr) {delete taskThread_;}}bool get(size_t timeoutSec 0) {if (taskState_ TASKSTOP) {throw AsyncTaskException::GetButTaskStopErr();}std::unique_lockstd::mutex lk(mtx_);if (timeoutSec 0) {cond_.wait(lk, [this](){return completedFlag_.load();});} else {return cond_.wait_for(lk, std::chrono::seconds(timeoutSec), [this](){return completedFlag_.load();});}return true;}};#endif
http://www.dnsts.com.cn/news/11626.html

相关文章:

  • 贵州建设厅网站办事大厅深圳住建局官方网站
  • 网站建设的未来海报设计论文
  • 现在建设的网站有什么劣势手机微网站怎么设计方案
  • 网站域名备案需要多长时间one dirve做网站
  • 皮卡剧网站怎样做wordpress 站点身份
  • 网站 关键词汽车之家网页版官网
  • 响应式网站建设好么舆情分析软件
  • 电子商务网站建设有什么认识农村自建房设计图120平方二层
  • 淘宝客网站建站源码禅城网站建设企业
  • 网站开发需要用到哪些设备做网站备案需要多长时间
  • 域名绑定网站需要多久获取排名
  • 天津七七一网站建设有限公司怎么样西安那里做网站
  • 网站项目报价单模板里水网站开发
  • 青梦建站杭州新网站建设方案
  • 怎么做百度网站免费的网站建设的优势是什么意思
  • 徐州网站定制站群wordpress
  • 企业免费建站网站毕设做购物网站系统的原因
  • 58同城建网站怎么做查询域名官网的是那个网站吗
  • 和各大网站做视频的工作能够做网站的资质
  • 湘潭营销型网站建设网页源代码是什么
  • 江苏网站备案要求电商网站开发发展和前景
  • 塘沽企业网站建设网络品牌推广方法
  • 手机网站建设网站报价河北秦皇岛建设局网站
  • 门户网站的细分模式有网站外链
  • 企业网站建设维护合同书建设摩托车官网商城2015
  • ui做交互式网站吗注册城乡规划师有什么用
  • 如何线上推广引流宁波品牌网站推广优化公司
  • 在线看免费网站外贸网站建站m
  • 网站大全2021中软属于国企还是央企
  • 长春网站制作都找源晟27湘潭做网站价格 q磐石网络