#include <chrono> #include <condition_variable> #include <ctime> #include <fstream> #include <future> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <random> #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; } bool is_finish=false; std::string get_str_len_is_finish() { static int num=0; std::stringstream ss; while(!is_finish) { for(int i=0;i<100000;i++) { ss<<++num<<","<<get_uuid()<<std::endl; if(is_finish) { break; } } if(is_finish) { break; } } return ss.str(); } void get_str_len_is_finish_detach(const int&sleep_seconds) { std::future<std::string> fut=std::async(get_str_len_is_finish); std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds)); is_finish=true; std::cout<<fut.get()<<std::endl; std::cout<<get_time_now()<<",finish in "<<__FUNCTION__<<std::endl; } int main(int args, char **argv) { get_str_len_is_finish_detach(atoi(argv[1])); }
Compile
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 10
标签:std,now,third,chrono,get,sleep,time,variable,include From: https://www.cnblogs.com/Fred1987/p/17364404.html