#include <algorithm> #include <atomic> #include <chrono> #include <condition_variable> #include <cstdint> #include <execution> #include <fstream> #include <future> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <mutex> #include <random> #include <sstream> #include <thread> #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() { 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); auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << "_" << 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::mutex _mtx; std::condition_variable _cv; bool _is_cv_ok=false; void action_unique_lock_wait() { static int num=0; while(!_is_cv_ok) { std::cout<<get_time_now()<<","<<++num<<","<<get_uuid_value()<<std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(100)); } std::cout<<get_time_now()<<",id:"<<std::this_thread::get_id()<<",in:"<<__FUNCTION__<<std::endl; } void cv_wait_for(const int&wait_seconds) { std::unique_lock<std::mutex> _lk(_mtx); std::cout<<std::boolalpha<<get_time_now()<<",_is_cv_ok:"<<_is_cv_ok<<",in:"<<__LINE__<<std::endl; std::thread t1(action_unique_lock_wait); _cv.wait_for(_lk,std::chrono::seconds(wait_seconds)); _is_cv_ok=true; _lk.unlock(); std::cout<<std::boolalpha<<get_time_now()<<",_is_cv_ok:"<<_is_cv_ok<<",in:"<<__LINE__<<std::endl; _cv.notify_all(); t1.join(); std::cout<<get_time_now()<<",id:"<<std::this_thread::get_id()<<",in:"<<__FUNCTION__<<std::endl; } int main(int args, char **argv) { cv_wait_for(atoi(argv[1])); std::cout<<get_time_now()<<",id:"<<std::this_thread::get_id()<<",in "<<__FUNCTION__<<std::endl; }
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
./h1 2
标签:std,now,uuid,chrono,lock,notify,time,duration,include From: https://www.cnblogs.com/Fred1987/p/17514701.html