#include <chrono> #include <ctime> #include <future> #include <iomanip> #include <iostream> #include <sstream> #include <string> #include <unistd.h> #include <uuid/uuid.h> 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::string get_num_and_uuid(const int&len) { std::stringstream ss; for(int i=0;i<len;i++) { ss<<i+1<<","<<get_uuid()<<std::endl; sleep(1); } return ss.str(); } void async_future_demo(const int &len) { std::cout<<get_time_now()<<",start in "<<__FUNCTION__<<std::endl; std::future<std::string> fut=std::async(get_num_and_uuid,std::cref(len)); std::cout<<get_time_now()<<" wait for the result!"<<std::endl; std::cout<<get_time_now()<<",the result is "<<fut.get()<<std::endl; std::cout<<get_time_now()<<",end in "<<__FUNCTION__<<std::endl; } int main(int args,char **argv) { async_future_demo(atoi(argv[1])); }
Compile
g++ -g -std=c++2a -I. *.cpp -o h1 -luuid
标签:std,now,chrono,c++,future,time,duration,async,include From: https://www.cnblogs.com/Fred1987/p/17281833.html