std::package_task bind.lambda,thread,future
#include <atomic> #include <chrono> #include <cmath> #include <condition_variable> #include <cstddef> #include <forward_list> #include <fstream> #include <functional> #include <future> #include <iomanip> #include <iostream> #include <latch> #include <list> #include <map> #include <memory> #include <mutex> #include <optional> #include <queue> #include <random> #include <stack> #include <string> #include <thread> #include <utility> #include <uuid/uuid.h> #include <vector> char *uuid_value = (char *)malloc(40); char *get_uuid_value() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } std::string get_time_now(bool is_exact = false) { std::stringstream ss; std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); if (is_exact) { std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); ss << "_" << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000) << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000) << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000); } return ss.str(); } std::string get_uuid_for_len(const int &len) { std::stringstream ss; for (int i = 0; i < len; i++) { ss << i + 1 << "," << get_uuid_value() << std::endl; } return ss.str(); } void package_task_lambda(const int &len) { std::packaged_task<std::string(const int &)> task([](const int &len) { std::stringstream ss; for(int i=0;i<len;i++) { ss<<i+1<<","<<get_uuid_value()<<std::endl; } std::cout << get_time_now() << ",thread id: " << std::this_thread::get_id() << ",finished in " << __FUNCTION__ << std::endl; return ss.str(); }); std::future<std::string> fut = task.get_future(); task(std::cref(len)); std::cout << "Task lambda:" << fut.get() << std::endl; std::cout << get_time_now() << ",thread id: " << std::this_thread::get_id() << ",finished in " << __FUNCTION__ << std::endl; } void package_task_thread_move(const int &len) { std::packaged_task<std::string(const int &len)> task(get_uuid_for_len); std::future<std::string> fut = task.get_future(); std::thread t1(std::move(task), std::cref(len)); t1.join(); std::cout << fut.get() << std::endl; std::cout << get_time_now() << ",thread id: " << std::this_thread::get_id() << ",finished in " << __FUNCTION__ << std::endl; } void packaged_task_bind(const int &len) { std::packaged_task<std::string()> task(std::bind(get_uuid_for_len, std::cref(len))); std::future<std::string> fut = task.get_future(); task(); std::cout << fut.get() << std::endl; std::cout << get_time_now() << ",thread id: " << std::this_thread::get_id() << ",finished in " << __FUNCTION__ << std::endl; } int main(int agrs, char **argv) { packaged_task_bind(atoi(argv[1])); std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",finished in " << __FUNCTION__ << std::endl; }
Compile
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 10
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2022-06-14 Linux C++ mysql create table and insert data into table,mysqlconnector set global net_read_timeout=3600