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

晋城建设路网站光谷中心城建设投资有限公司网站

晋城建设路网站,光谷中心城建设投资有限公司网站,wordpress 产品多图,企业官网建设为什么写这个 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.w-s-a.com/news/151111/

相关文章:

  • 烟台高端网站开发wordpress 设置权限
  • 中小企业网站制作流程网站开发和设计人员的岗位要求
  • 公司网站建设多少费用河北城乡建设官网站
  • 国科联创网站建设广告传媒公司招聘信息
  • 网站后台文章删了 怎么前台还有一级做爰片软件网站
  • 辽宁省建设注册中心网站wordpress 博客插件
  • 做电商看的网站有哪些网站建设需求策划书
  • 关于网站建设交易流程的描述一句话哪些网站用户体验好
  • 男女做暖暖的网站大全深圳平台网站建设外包
  • 凯里展示型网站设计抖音代运营收费详细价格
  • 外包网站会自己做原型吗网站制作怎样盈利
  • 为什么在百度搜不到我的网站电商网站开发过程
  • 什么是网站反链网页设计页面链接
  • 佛山企业网站制作韩国seocaso
  • 微信公司网站vue做社区网站
  • 蒙阴网站优化五核网站建设
  • 企业微商城网站建设wordpress新闻是哪个表
  • 重庆网站开发培训机构电商网站创办过程
  • 企业建网站得多少钱长沙财优化公司
  • 网站开发api平台扒完网站代码之后怎么做模板
  • PHP网站建设选择哪家好动画设计师月薪多少
  • 网站如何做市场推广网站开发主要步骤
  • 浏览器正能量网站网页文章导入wordpress
  • 江西中国建设银行网站首页永久免费自助建网站
  • 创建自己网站的步骤吸引人的微信软文
  • 网站建设与网页设计论述题软件开发公司在哪里
  • 二级网站建设方案模板亚马逊网站建设案例
  • 网站开发兼职团队门户网站如何制作
  • 高州市网站建设开发区招聘信息
  • 上海专业网站制作设计公司企业邮箱怎样注册