//model/util.h #pragma once #include <algorithm> #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <sstream> #include <thread> #include <uuid/uuid.h> #include <vector> class util { public: util(); ~util(); util(util &ul) = delete; util operator=(util &ul) = delete; char *uuid_value; char *get_uuid(); std::string get_uuid_str(); std::string get_time_now(); void print_log(const std::string &msg); std::mutex _mtx; std::map<int, std::string> _mtx_map; static void fill_mtx_map(util &ul, const int &len); void mt_thread_mutext(util &ul, const int &x, const int &y, const int &z); std::mutex _guard_mtx; std::map<int, std::string> _guard_mtx_map; static void fill_mtx_guard_map(util &ul, const int &len); void mt_thread_mtx_guard(util &ul, const int &x, const int &y, const int &z); }; //model/util.cpp #include "model/util.h" void util::fill_mtx_guard_map(util &ul, const int &len) { std::lock_guard<std::mutex> guard(ul._guard_mtx); ul.print_log("Started in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); std::cout << "std::this_thread::get_id()= " << std::this_thread::get_id() << std::endl; static int num = 0; for (int i = 0; i < len; i++) { ul._guard_mtx_map.insert(std::pair<int, std::string>(++num, ul.get_uuid())); } ul.print_log("Finished in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); } void util::mt_thread_mtx_guard(util &ul, const int &x, const int &y, const int &z) { print_log("Started in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); std::cout << "std::this_thread::get_id()= " << std::this_thread::get_id() << std::endl; std::thread t1(fill_mtx_guard_map, std::ref(ul), std::cref(x)); std::thread t2(fill_mtx_guard_map, std::ref(ul), std::cref(y)); std::thread t3(fill_mtx_guard_map, std::ref(ul), std::cref(z)); t1.join(); t2.join(); t3.join(); std::cout << "_guard_mtx_map's size =" << _guard_mtx_map.size() << std::endl; auto itr = _guard_mtx_map.begin(); while (itr != _guard_mtx_map.end()) { std::cout << "Key=" << itr->first << "," << itr->second << std::endl; itr = std::next(itr, 5000000); } print_log("Finished in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); } void util::mt_thread_mutext(util &ul, const int &x, const int &y, const int &z) { print_log("Started in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); std::thread t1(fill_mtx_map, std::ref(ul), std::cref(x)); std::thread t2(fill_mtx_map, std::ref(ul), std::cref(y)); std::thread t3(fill_mtx_map, std::ref(ul), std::cref(z)); t1.join(); t2.join(); t3.join(); std::cout << "Map size= " << _mtx_map.size() << std::endl; auto itr = _mtx_map.begin(); while (itr != _mtx_map.end()) { std::cout << "Key=" << itr->first << "," << itr->second << std::endl; itr = std::next(itr, 5000000); } print_log("Finished in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); } void util::fill_mtx_map(util &ul, const int &len) { ul._mtx.lock(); ul.print_log("Started in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); static int num = 0; try { for (int i = 0; i < len; i++) { ul._mtx_map.insert(std::pair<int, std::string>(++num, ul.get_uuid())); } } catch (const std::exception &e) { ul._mtx.unlock(); std::cerr << e.what() << '\n'; } ul.print_log("Finished in " + std::string(__FILE__) + "," + std::string(__FUNCTION__) + "," + std::to_string(__LINE__)); ul._mtx.unlock(); } void util::print_log(const std::string &msg) { std::cout << get_time_now() << "," << msg << 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::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(); } util::util() { uuid_value = (char *)malloc(40); std::cout << get_time_now() << " in constructor!" << std::endl; } util::~util() { std::cout << get_time_now() << " in deconstructor!" << std::endl; } char *util::get_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } std::string util::get_uuid_str() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); std::string uuid_str(uuid_value); return uuid_str; } //main.cpp #include "model/util.h" void mt_thread_mutex_lock_unlock(const int &x, const int &y, const int &z) { util ul; ul.print_log("Started in "+std::string(__FILE__)+","+std::string(__FUNCTION__)+","+std::to_string(__LINE__)); ul.mt_thread_mutext(std::ref(ul),std::cref(x),std::cref(y),std::cref(z)); ul.print_log("Finished in "+std::string(__FILE__)+","+std::string(__FUNCTION__)+","+std::to_string(__LINE__)); } void mt_thread_mutex_lock_guard(const int &x,const int &y,const int&z) { util ul; ul.print_log("Started in "+std::string(__FILE__)+","+std::string(__FUNCTION__)+","+std::to_string(__LINE__)); ul.mt_thread_mtx_guard(std::ref(ul),std::cref(x),std::cref(y),std::cref(z)); ul.print_log("Finished in "+std::string(__FILE__)+","+std::string(__FUNCTION__)+","+std::to_string(__LINE__)); } int main(int args,char **argv) { try { mt_thread_mutex_lock_guard(std::atoi(argv[1]),std::atoi(argv[2]),std::atoi(argv[3])); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } }
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread
标签:std,__,const,lock,util,ul,unlock,mutex,include From: https://www.cnblogs.com/Fred1987/p/17231640.html