#include <chrono> #include <condition_variable> #include <ctime> #include <fstream> #include <future> #include <iomanip> #include <iostream> #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); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); 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()); ss << "_" << 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::atomic<bool> _is_write_finished(false); std::condition_variable _cv; std::mutex _mtx; void cv_atomic_write_file(const std::string &file_name) { std::fstream w_file(file_name, std::ios::app); if (!w_file.is_open()) { std::cout << "Create or open " << file_name << " failed!!!" << std::endl; return; } static std::uint64_t num = 0; int loops = 0; std::stringstream ss; while (!_is_write_finished.load(std::memory_order_seq_cst)) { for (int i = 0; i < 100000; i++) { ss << ++num << "," << get_uuid_value() << std::endl; } w_file << ss.str(); ss = std::stringstream(); std::cout<<std::boolalpha<<"is_write_finished:"<<_is_write_finished<<","<< get_time_now() << ",loops:" << ++loops << ",num:" << num << std::endl; } w_file.close(); std::cout << get_time_now() << ",finish id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl; } void cv_wait_for(const std::string&file_name,const int &wait_seconds) { std::thread t1(cv_atomic_write_file,std::cref(file_name)); std::unique_lock<std::mutex> _lk(_mtx); _cv.wait_for(_lk,std::chrono::seconds(wait_seconds)); _is_write_finished.store(true, std::memory_order_seq_cst); std::cout<<std::boolalpha<<"is_write_finished:"<<_is_write_finished<<std::endl; t1.detach(); std::cout << get_time_now() << ",finish id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl; } int main(int args, char **argv) { // write_file_for_loops(argv[1],atoi(argv[2])); // future_get_for_len(atoi(argv[1])); // promise_set_value_future_get(atoi(argv[1])); // atomic_write_file_store(atoi(argv[1]), argv[2]); cv_wait_for(argv[1],atoi(argv[2])); std::cout << get_time_now() << ",id:" << std::this_thread::get_id() << " in " << __FUNCTION__ << std::endl; }
Compile
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 log2.txt 100
标签:std,now,uuid,chrono,seconds,time,variable,include From: https://www.cnblogs.com/Fred1987/p/17502936.html