cpp multi thread std::lock_guard,mutex()-其他
cpp multi thread std::lock_guard,mutex()
#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::mutex _mtx;
std::map<int, std::string> _mtx_map;
void fill_mtx_map(const int &len)
{
std::lock_guard<std::mutex> _lock_guard(_mtx);
static int num = 0;
for (int i = 0; i < len; i++)
{
std::string temp_str = get_uuid();
_mtx_map.insert(std::pair<int, std::string>(++num, temp_str));
}
}
void mt_fill_mtx_map(const int &x, const int &y, const int &z, const int &w)
{
std::thread t1(fill_mtx_map, std::cref(x));
std::thread t2(fill_mtx_map, std::cref(y));
std::thread t3(fill_mtx_map, std::cref(z));
std::thread t4(fill_mtx_map, std::cref(w));
t1.join();
t2.join();
t3.join();
t4.join();
std::cout << "Size =" << _mtx_map.size() << std::endl;
int idx = 0;
auto itr = _mtx_map.begin();
while (itr != _mtx_map.end())
{
while (++idx % 100000 == 0)
{
std::cout<<"Idx:"<<idx<<"," << "key:" << itr->first << ",value:" << itr->second << std::endl;
}
++itr;
}
std::cout << get_time_now() << ", finish in lock_guard mutex of multi threads" << std::endl;
}
int main(int args, char **argv)
{
mt_fill_mtx_map(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
}
Comile
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 100000000 10000000 100000000 10000000
————————
#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::mutex _mtx;
std::map<int, std::string> _mtx_map;
void fill_mtx_map(const int &len)
{
std::lock_guard<std::mutex> _lock_guard(_mtx);
static int num = 0;
for (int i = 0; i < len; i++)
{
std::string temp_str = get_uuid();
_mtx_map.insert(std::pair<int, std::string>(++num, temp_str));
}
}
void mt_fill_mtx_map(const int &x, const int &y, const int &z, const int &w)
{
std::thread t1(fill_mtx_map, std::cref(x));
std::thread t2(fill_mtx_map, std::cref(y));
std::thread t3(fill_mtx_map, std::cref(z));
std::thread t4(fill_mtx_map, std::cref(w));
t1.join();
t2.join();
t3.join();
t4.join();
std::cout << "Size =" << _mtx_map.size() << std::endl;
int idx = 0;
auto itr = _mtx_map.begin();
while (itr != _mtx_map.end())
{
while (++idx % 100000 == 0)
{
std::cout<<"Idx:"<<idx<<"," << "key:" << itr->first << ",value:" << itr->second << std::endl;
}
++itr;
}
std::cout << get_time_now() << ", finish in lock_guard mutex of multi threads" << std::endl;
}
int main(int args, char **argv)
{
mt_fill_mtx_map(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
}
Comile
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 100000000 10000000 100000000 10000000