#model/util.h #pragma once #include <chrono> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <mutex> #include <sstream> #include <stdio.h> #include <time.h> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> class util { public: char *uuid_value; char *get_uuid(); std::string get_str_uuid(); std::string get_time_now(); util(); ~util(); util(util &ul) = delete; util operator=(util &ul) = delete; }; //model/util.cpp #include "model/util.h" char *util::get_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } util::util() { uuid_value = (char *)malloc(40); std::cout << "In constructor and now is " << get_time_now() << std::endl; } util::~util() { std::cout << "In deconstructor and now is " << get_time_now() << std::endl; } std::string util::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::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); std::uint64_t mills_count = mills.count() - seconds.count() * 1000; std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << std::to_string(mills_count); return ss.str(); } std::string util::get_str_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return std::string(uuid_value); } //main.cpp #include "model/util.h" std::map<int, std::string> _mutex_map; std::mutex _lock_unlock_mtx; void fill_mutext_map(util &ul, const int &len) { _lock_unlock_mtx.lock(); std::cout << "std::this_thread::get_id()=" << std::this_thread::get_id() << "," << ul.get_time_now() << " started in file:" << std::string(__FILE__) << ",function:" << std::string(__FUNCTION__) + ",line:" << std::to_string(__LINE__) << std::endl; static int num = 0; for (int i = 0; i < len; i++) { _mutex_map.insert(std::pair<int, std::string>(++num, ul.get_str_uuid())); } std::cout << num << ",std::this_thread::get_id()=" << std::this_thread::get_id() << "," << ul.get_time_now() << " finished in file:" << std::string(__FILE__) << ",function:" << std::string(__FUNCTION__) + ",line:" << std::to_string(__LINE__) << std::endl; _lock_unlock_mtx.unlock(); } void mutex_multi_threads(const int &x, const int &y) { util ul; std::thread t1(fill_mutext_map, std::ref(ul), std::cref(x)); std::thread t2(fill_mutext_map, std::ref(ul), std::cref(y)); t1.join(); t2.join(); std::cout << "_mutex_map.size()=" << _mutex_map.size() << std::endl; auto itr = _mutex_map.begin(); while (itr != _mutex_map.end()) { std::cout << itr->first << "," << itr->second << std::endl; itr = std::next(itr, 1000000); } std::cout << ul.get_time_now() << " finished in file:" << std::string(__FILE__) << ",function:" << std::string(__FUNCTION__) + ",line:" << std::to_string(__LINE__) << std::endl; } int main(int argc, char **argv) { mutex_multi_threads(atoi(argv[1]), atoi(argv[2])); }
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread
标签:std,now,uuid,lock,util,unlock,mutex,time,include From: https://www.cnblogs.com/Fred1987/p/17228563.html