cpp once_flag,call_once in mutex
//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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2021-03-24 WPF implement SelectedCommand in MVVM via Interaction.Triggers
2020-03-24 C# HttpClient PostAsync with parameters