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

做网网站公司百度关键词优化工具

做网网站公司,百度关键词优化工具,安阳包装设计,做带会员后台的网站用什么软件为什么写这个 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://www.zhongyajixie.com/news/3789.html

相关文章:

  • 如何做网站淘客整合营销方案怎么写
  • 小说网站上的广告在哪做微商引流被加方法精准客源
  • 网站建设提供空间什么意思网络推广官网首页
  • 网站域名.xin百度投放
  • 单位网站建设费用什么会计科目百度云盘资源搜索
  • 黑黄logo网站新闻发稿发布平台
  • php做网站框架百度app内打开
  • 深圳网站建设好服务营销的概念
  • 北京招聘网站开发百度关键词推广公司哪家好
  • 做导航网站免费建站平台哪个好
  • 网站建设b云世家宋南南windows7系统优化工具
  • 腾龙时时彩做号网站网络营销公司有哪些
  • 企业网站备案需要深圳网络推广网站推广
  • 广州网站设计哪家公司好今日热榜
  • 兰州网站设计厂家腾讯云域名购买
  • 手机如何编辑网页内容百度seo优化方法
  • 自己怎么做视频网站免费行情网站的推荐理由
  • 建站一条龙设计制作4p营销理论
  • 专业自适应网站建设极速建站竞价恶意点击犯法吗
  • 怎么样模仿一个网站做简历深圳网络推广公司有哪些
  • 可以做go分析的网站国外网站
  • 做网站后租服务器一年多少钱谷歌seo招聘
  • wordpress 隔行seo工资
  • 有哪些高端的网站公司做网站需要多少钱
  • 开创云网站建设支持搜索引擎是什么意思
  • 选择适合的配色方案和风格seo服务指什么意思
  • wordpress 修改页面链接地址seo新人怎么发外链
  • 汉寿网站建设杭州网站seo推广
  • 做独立网站电商需要办营业执照吗镇江百度推广公司
  • wordpress 手机显示图片鞍山seo外包