首页 > 其他分享 >cpp multi thread std::lock_guard,mutex

cpp multi thread std::lock_guard,mutex

时间:2023-05-01 16:13:34浏览次数:40  
标签:std multi now thread chrono mtx time include

#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

 

标签:std,multi,now,thread,chrono,mtx,time,include
From: https://www.cnblogs.com/Fred1987/p/17366618.html

相关文章

  • RTThread的初始化宏(备忘录)
    RTThread的初始化流程方便后续查找.一.初始化接口初始化顺序接口描述1INIT_BOARD_EXPORT(fn)硬件的初始化,此时调度器还未启动2INIT_PREV_EXPORT(fn)主要是用于纯软件的初始化、没有太多依赖的函数3INIT_DEVICE_EXPORT(fn)外设驱动初始化相关,比如网卡设备......
  • cpp multi thread sync via std::atomic<bool>
    #include<atomic>#include<chrono>#include<cmath>#include<condition_variable>#include<ctime>#include<fstream>#include<functional>#include<future>#include<iomanip>#include<iostream&g......
  • c++11:std::forward,完美转发
    目录1、不完美转发2、完美转发2.1、引用折叠2.2、std::forward1、不完美转发所谓完美转发,是指在函数模板中,完全按照模板的参数的类型,将参数传递给函数模板中调用的另一个函数。比如:template<typenameT>voidIamForwording(Tt){IrunCodeActually(t);}上面的例子中,IamF......
  • Python+UDP+Threading
    Python+UDP+Threading近期用pythonsocket使用TCP协议做了一个小型的数据收发服务器,后来由于在实际场景中使用时,出现网络不佳导致出现错误的情况,改成了使用UDP协议重做了一版,总体效果变好了。下面是通用代码,实际使用时在这基础上进行修改即可。#-*-coding:utf-8-*-import......
  • 线程常用方法join 和threadLocal
     从源码中可以得知,如果想要join方法正常生效,调用join方法的线程对象必须已经调用了start()方法并且未进入终止状态。扩展:从join方法的源码来看,join方法的本质调用的是Object中的wait方法实现线程的阻塞,wait方法的实现原理在后续的文章中在说详细阐述。**但是我们需要知道的是......
  • 问题解决:Component name "xxx" should always be multi-word vue/multi-word-compone
    如题,原因是单个单词命名时语法检测无法通过,可以在导出组件时通过name属性给组件名加一个后缀,比如Component。<script>exportdefault{//当组件名为一个单词时,语法检查是无法通过的,可以设置name的值为2个单词来规避检查。name:'HomeComponent'}<......
  • C++-std::this_thread::get_id()-获取线程id
    C++-std::this_thread::get_id()-获取线程idstd::this_thread::get_id()头文件:<thread>函数:std::this_thread::get_id()用例:std::thread::idthread_id=std::this_thread::get_id();std::thread对象的成员函数get_id()头文件:<thread>函数:std::thread::idget_id()用例:......
  • STM32:RTthread_线程
    1微处理器系统    随着产品功能的增多,裸机系统不能够满足产品需求,引入RTOS实时操作系统的多线程管理,可以增加程序的稳定性逻辑性,便于管理;2线程  通常默认一个能独立实现功能的函数,称之为线程;多线程管理的意思就是这个程序可以实现多个功能管理;  2.1线程栈   ......
  • STM32 + RTThread + UGUI
    一、概述开发板:STM32F103C8T6显示器:ST7735SRT-Thread:5.0.0玩过GUI的小伙伴都知道,界面的显示是一个个像素点组合起来的,那么直接构建出来炫酷的GUI还是相对比较困难的,所以我们一般都会使用一些GUI库来实现,比如LVGL、QT、UGUI等,这样对于驱动开发的人员来说就相对比较简......
  • Linux安装Fastdfs
    前言:还是和以前一样,linux安装软件的目录都是data目录 1.进入data目录,创建libfastcommon目录并进入该目录cd/datamkdirlibfastcommoncdlibfastcommon 2.yum安装一下unzipyuminstallzipunzip-y 3.上传"libfastcommon_v1.40.zip"文件到当前目录(/data/li......