#include <chrono> #include <ctime> #include <future> #include <iomainp> #include <iostream> #include <sstream> #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 future_str(const int&len) { std::stringstream ss; for(int i=0;i<len;i++) { ss<<i+1<<","<<get_uuid()<<std::endl; } return ss.str(); } void std_shared_future(const int&len) { std::future<std::string> fut=std::async(future_str,std::cref(len)); std::shared_future<std::string> shared_fut(std::move(fut)); std::cout<<shared_fut.get()<<std::endl; std::cout<<get_time_now()<<",finished in "<<__FUNCTION__<<std::endl; } int main(int args, char **argv) { std_shared_future(atoi(argv[1])); }
g++ -g -std=c++2a -I. *.cpp -o h1 -luuid
标签:std,include,chrono,duration,future,time,cpp,shared,now From: https://www.cnblogs.com/Fred1987/p/17291115.html