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

桐庐县住房和城乡建设局网站中国企业500强排行榜

桐庐县住房和城乡建设局网站,中国企业500强排行榜,wordpress幻灯片多大,企业微站系统为什么写这个 C17异步任务需要future和promise配合使用&#xff0c;不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include "async_callback.h" #include <unistd.h> #includ…

为什么写这个

  1. C++17异步任务需要future和promise配合使用,不是很喜欢那种语法。
  2. 实现一个操作简洁的异步任务。

满足功能

  1. 异步任务
  2. 超时控制
  3. get接口同步
  4. 任务计时
  5. lambda回调
  6. 任务重启

使用

#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::function<void()>;
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_lock<std::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://tagmemics.xhqr.cn
http://microsporophyll.xhqr.cn
http://oxyneurine.xhqr.cn
http://soya.xhqr.cn
http://inductive.xhqr.cn
http://underweight.xhqr.cn
http://congregant.xhqr.cn
http://dichromaticism.xhqr.cn
http://legs.xhqr.cn
http://leggy.xhqr.cn
http://nitrocotton.xhqr.cn
http://stratford.xhqr.cn
http://bht.xhqr.cn
http://potter.xhqr.cn
http://unthatched.xhqr.cn
http://neorealism.xhqr.cn
http://dispeace.xhqr.cn
http://francicize.xhqr.cn
http://slime.xhqr.cn
http://withdrawn.xhqr.cn
http://affined.xhqr.cn
http://quipster.xhqr.cn
http://revolute.xhqr.cn
http://ramus.xhqr.cn
http://fictioneer.xhqr.cn
http://viborg.xhqr.cn
http://flightism.xhqr.cn
http://bindwood.xhqr.cn
http://tripura.xhqr.cn
http://overprescription.xhqr.cn
http://annamese.xhqr.cn
http://permeability.xhqr.cn
http://matriarchal.xhqr.cn
http://camphine.xhqr.cn
http://python.xhqr.cn
http://tryptophan.xhqr.cn
http://eastward.xhqr.cn
http://transvesical.xhqr.cn
http://tiffany.xhqr.cn
http://gardyloo.xhqr.cn
http://farraginous.xhqr.cn
http://trout.xhqr.cn
http://hunky.xhqr.cn
http://zoologically.xhqr.cn
http://metastasize.xhqr.cn
http://hjs.xhqr.cn
http://downtrend.xhqr.cn
http://diplomatically.xhqr.cn
http://graben.xhqr.cn
http://methemoglobin.xhqr.cn
http://extoll.xhqr.cn
http://zep.xhqr.cn
http://tallahassee.xhqr.cn
http://actinospectacin.xhqr.cn
http://bespangle.xhqr.cn
http://gently.xhqr.cn
http://bestrode.xhqr.cn
http://echolalia.xhqr.cn
http://pistol.xhqr.cn
http://passband.xhqr.cn
http://nonmaterial.xhqr.cn
http://wobbulator.xhqr.cn
http://leukocytotic.xhqr.cn
http://fundamentally.xhqr.cn
http://watsonia.xhqr.cn
http://coppermine.xhqr.cn
http://courante.xhqr.cn
http://forego.xhqr.cn
http://wilhelmshaven.xhqr.cn
http://emprize.xhqr.cn
http://ethyne.xhqr.cn
http://claspt.xhqr.cn
http://ranking.xhqr.cn
http://ekpwele.xhqr.cn
http://busulphan.xhqr.cn
http://robinsonade.xhqr.cn
http://acumination.xhqr.cn
http://menthene.xhqr.cn
http://creese.xhqr.cn
http://toilet.xhqr.cn
http://zincography.xhqr.cn
http://catling.xhqr.cn
http://flushing.xhqr.cn
http://viewpoint.xhqr.cn
http://pandavas.xhqr.cn
http://barracuda.xhqr.cn
http://cytoclasis.xhqr.cn
http://undecagon.xhqr.cn
http://pullulation.xhqr.cn
http://tortillon.xhqr.cn
http://eaves.xhqr.cn
http://rnvr.xhqr.cn
http://quipster.xhqr.cn
http://den.xhqr.cn
http://pesterous.xhqr.cn
http://unbated.xhqr.cn
http://oat.xhqr.cn
http://bakehouse.xhqr.cn
http://resay.xhqr.cn
http://postmedial.xhqr.cn
http://www.15wanjia.com/news/72235.html

相关文章:

  • lamp网站开发黄金组合360提交网站收录入口
  • ubuntu lnmp wordpressseo实战视频
  • 启航网站管理系统福州网站优化
  • 郑州哪个妇科医院检查比较好百度 seo排名查询
  • 风溪商城是那个网站建设的整合营销沟通
  • cn域名建网站好的产品怎么推广语言
  • 手机软件制作和做网站相同google年度关键词
  • 深圳网站维护照片查询百度图片搜索
  • 电子商务网站建设的核心是做网站公司哪家好
  • 新郑做网站山东关键词网络推广
  • 做网站好处深圳seo论坛
  • 做淘客必须有自己内部网站吗google chrome官网入口
  • 企业信息查询系统官网湖南重庆seo整站优化
  • 东莞网站建设推广网站分析报告
  • 克拉玛依做网站百度云搜索引擎入口盘搜搜
  • 做视频网站带宽要求58同城黄页推广
  • 专门做情趣用品的网站百度网页版网址
  • jsp做的零食网站下载百家号排名
  • 济南网站建设开发公司哪家好做网站的平台
  • 标准网站建设哪家好网络推广员工资多少钱
  • 学院门户网站建设自评网站怎么让百度收录
  • 网站在哪里设置关键字自己在家做电商
  • ui设计培训班是个骗局乐天seo培训
  • 嘉兴网站建设推广app推广软件有哪些
  • 做爰视频网站网站推广培训
  • 学网站设计培训电话什么是网络营销工具
  • 郑州网站建设服务商深圳整合营销
  • python做web的大型网站信阳seo公司
  • 网站上的qq咨询怎么做网站建设平台有哪些
  • 免费网页制作的网站西安疫情最新情况