//util.h #pragma once #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <mutex> #include <sstream> #include <string> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> class util { public: std::string get_time_now(); char *get_uuid(); util(); ~util(); util(const util &ul) = delete; util &operator=(util &&ul) = delete; char *uuid_value = (char *)malloc(40); std::once_flag of; std::map<int, std::string> _once_flag_map; static void call_once_func(util &ul, const int &len); static void fill_once_flag_map(util &ul, const int &len); void mt_call_once_func(util &ul, const int &x, const int &y, const int &z); }; //model/util.cpp #include "model/util.h" void util::mt_call_once_func(util &ul, const int &x, const int &y, const int &z) { std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ", start in " << std::string(__FUNCTION__) << std::endl; std::thread t1(call_once_func,std::ref(ul),std::cref(x)); std::thread t2(call_once_func,std::ref(ul),std::cref(y)); std::thread t3(call_once_func,std::ref(ul),std::cref(z)); t1.join(); t2.join(); t3.join(); std::cout << "Size=" << _once_flag_map.size() << std::endl; auto itr = _once_flag_map.begin(); while (itr != _once_flag_map.end()) { std::cout << "Key=" << itr->first << ",value=" << itr->second << std::endl; itr++; } std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ", finish in " << std::string(__FUNCTION__) << std::endl; } void util::call_once_func(util &ul, const int &len) { std::call_once(ul.of, fill_once_flag_map,std::ref(ul),std::cref(len)); } void util::fill_once_flag_map(util &ul, const int &len) { std::cout << ul.get_time_now() << ",thread id:" << std::this_thread::get_id() << ", start in " << std::string(__FUNCTION__) << std::endl; static int num = 0; for (int i = 0; i < len; i++) { ul._once_flag_map.insert(std::pair<int, std::string>(++num, ul.get_uuid())); } std::cout << ul.get_time_now() << ",thread id:" << std::this_thread::get_id() << ", finish in " << std::string(__FUNCTION__) << std::endl; } char *util::get_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } util::util() { std::cout << get_time_now() << " in constructor!" << std::endl; } util::~util() { std::cout << get_time_now() << " in decontructor!" << 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); 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()); struct tm tm_info = *localtime(&raw_time); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000); return ss.str(); } //main.cpp #include "model/util.h" void mt_call_once_func_demo(const int &x, const int &y, const int &z) { util ul; ul.mt_call_once_func(std::ref(ul),std::cref(x),std::cref(y),std::cref(z)); } int main(int args,char **argv) { mt_call_once_func_demo(atoi(argv[1]),atoi(argv[2]),atoi(argv[3])); }
Compile
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread
Run
./h1 10 10 10
原文链接: https://www.cnblogs.com/Fred1987/p/17249997.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401105
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!