#include <atomic> #include <chrono> #include <cmath> #include <condition_variable> #include <ctime> #include <fstream> #include <functional> #include <future> #include <iomanip> #include <iostream> #include <list> #include <map> #include <mutex> #include <queue> #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; } std::atomic<bool> _is_write_finish(false); void write_into_file(const std::string &file_name, const int &len) { 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; } std::stringstream ss; int loops = len / 1000000; int remainder = len % 1000000; static int num = 0; if (loops > 0) { for (int i = 0; i < loops; i++) { for (int j = 0; j < 1000000; j++) { ss << ++num << "," << get_uuid() << std::endl; } w_file << ss.str(); std::cout<<num<<std::endl; ss=std::stringstream(); } } if(remainder>0) { for(int i=0;i<remainder;i++) { ss<<++num<<","<<get_uuid()<<std::endl; } w_file<<ss.str(); ss=std::stringstream(); } w_file.close(); _is_write_finish=true; } void print_file(const std::string&file_name) { while(!_is_write_finish.load()) { } std::fstream read_file(file_name,std::ios::in); if(!read_file.is_open()) { std::cout<<"Open "<<file_name<<" failed!"<<std::endl; return; } std::string line; while(getline(read_file,line)) { std::cout<<line<<std::endl; } read_file.close(); std::cout<<get_time_now()<<",finishi in "<<__FUNCTION__<<std::endl; } void atomic_write_read_sync(const std::string &file_name, const int &len) { std::thread t1(write_into_file,std::cref(file_name), std::cref(len)); std::thread t2(print_file,std::cref(file_name)); t1.join(); t2.join(); std::cout<<get_time_now()<<",finish in "<<__FUNCTION__<<std::endl; } int main(int args, char **argv) { atomic_write_read_sync(argv[1],atoi(argv[2])); }
Compile
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 log.txt 11111111
The key to synchronize multiple threads in before-and-write relationships located at std::atomic<bool> the third variable,with false default value,when before thread finished set the atomic variable to true
While in the after thread,in while loop it run nothing because the load() is false,until the atomic variable is true,it run the following code sequentially.
标签:std,multi,via,chrono,now,time,include,atomic From: https://www.cnblogs.com/Fred1987/p/17366526.html