cpp condition_variable wait_until unique_mutex time_out
#include <chrono> #include <condition_variable> #include <ctime> #include <fstream> #include <future> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <sstream> #include <thread> #include <uuid/uuid.h> #include <vector> std::string get_time_now() { 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); std::stringstream ss; 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::put_time(&tm_info, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << std::to_string(mills.count() - seconds.count() * 1000) << std::setw(3) << std::setfill('0') << std::to_string(micros.count() - mills.count() * 1000) << std::setw(3) << std::setfill('0') << std::to_string(nanos.count() - micros.count() * 1000); return ss.str(); } char *uuid_value = (char *)malloc(40); char *get_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } std::condition_variable cv; std::mutex _mtx; bool is_done = false; void condition_variable_wait_for_seconds(const int &wait_seconds) { std::chrono::time_point<std::chrono::high_resolution_clock> time_out = std::chrono::high_resolution_clock::now() + std::chrono::seconds(wait_seconds); std::unique_lock<std::mutex> lk(_mtx); while (1) { if (cv.wait_until(lk, time_out) == std::cv_status::timeout) { is_done = true; break; } } } void log_file_time() { std::fstream w_file("log.txt", std::ios::app); if (!w_file.is_open()) { std::cout << "Create or open log.txt failed!" << std::endl; return; } std::stringstream ss; std::uint64_t num = 0; while (!is_done) { ss << ++num << "," << get_uuid() << std::endl; if (num % 1000000 == 0) { w_file << ss.str(); ss = std::stringstream(); std::cout << num << "," << get_time_now() << "," << get_time_now() << std::endl; } if (is_done) { w_file << ss.str(); ss = std::stringstream(); std::cout << std::boolalpha << num << "," << get_time_now() << "," << get_time_now()<< ",is_done:" << is_done << std::endl; break; } } } void mt_condition_variable(const int &wait_seconds) { std::stringstream ss; ss << get_time_now() << ",start in " << __FUNCTION__ << std::endl; std::thread t1(condition_variable_wait_for_seconds, std::cref(wait_seconds)); std::thread t2(log_file_time); t1.join(); t2.join(); ss << get_time_now() << ",finish in " << __FUNCTION__ << std::endl; std::cout << ss.str(); } int main(int args, char **argv) { mt_condition_variable(atoi(argv[1])); }
Compile
g++ -g -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 1000
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2022-04-19 Compute the classic Fibonacci running times quantitatively via the third variable
2022-04-19 C++ numeric types max values via numeric_limits<T>::max()
2022-04-19 Original Fibonacci and optimized Fibonacci functions and use the third variable to log the recursion times quantitatively
2020-04-19 dynamic intercept and interpret all method calls via implementation IDynamicMetadataObjectProvider
2020-04-19 get caller member name,caller file path,caller line number via attributes
2020-04-19 try catch exception when