首页 > 编程语言 >c++ condition_variable wait unique_lock,cv.notifyall()

c++ condition_variable wait unique_lock,cv.notifyall()

时间:2023-06-05 18:12:51浏览次数:38  
标签:std uuid chrono lock time c++ notifyall include cv

#include <atomic>
#include <chrono>
#include <cmath>
#include <condition_variable>
#include <cstddef>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <latch>
#include <list>
#include <map>
#include <mutex>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <uuid/uuid.h>
#include <vector>
#include "tree.cpp"

char *uuid_value = (char *)malloc(40);
char *get_uuid_value()
{
    uuid_t new_uuid;
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    return uuid_value;
}

std::string get_time_now(bool is_exact = false)
{
    std::stringstream ss;
    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);
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    if (is_exact)
    {
        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::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
           << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
           << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    }
    return ss.str();
}

std::condition_variable _cv;
std::map<int, std::string> _cv_map;
bool _is_ready = false;
std::mutex _cv_mtx;
void fill_map()
{
    static int num = 0;
    while (!_is_ready)
    {
        _cv_map.insert(std::pair<int, std::string>(++num, get_uuid_value()));
    }
}

void cv_wait_notify(const int &sleep_seconds)
{
    std::unique_lock<std::mutex> _lk(_cv_mtx);
    std::thread t1(fill_map);
    _cv.wait_for(_lk, std::chrono::seconds(sleep_seconds));
    _is_ready = true;
    _cv.notify_all();
    t1.join();
    std::cout << "Map size:" << _cv_map.size() << std::endl;
    std::cout << get_time_now(true) << ",thread id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
}

int main(int args, char **argv)
{
    cv_wait_notify(atoi(argv[1]));
    std::cout << get_time_now(true) << ",thread id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
    std::cout << "Map size:" << _cv_map.size() << std::endl;
}

 

Compile

g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread

Run

./h1 10;

 

标签:std,uuid,chrono,lock,time,c++,notifyall,include,cv
From: https://www.cnblogs.com/Fred1987/p/17458613.html

相关文章

  • C#读写锁ReaderWriterLockSlim的使用
    读写锁的概念很简单,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁。在C#中,推荐使用ReaderWriterLockSlim类来完成读写锁的功能。某些场合下,对一个对象的读取次数远远大于修改次数,如果只是简单的用lock方式加锁,则会影响读取的效率。而如果采用读......
  • C++11中的std::function
    看看这段代码先来看看下面这两行代码:std::function<void(EventKeyboard::KeyCode,Event*)>onKeyPressed;std::function<void(EventKeyboard::KeyCode,Event*)>onKeyReleased;这两行代码是从Cocos2d-x中摘出来的,重点是这两行代码的定义啊。std::function这是什么东西?如果你对上......
  • C、C++、Java等控制汽车- 汽车引擎控制系统
    汽车代码的实现 - 汽车引擎控制系统汽车引擎控制系统是汽车代码中最重要的一部分之一。控制系统的目的是确保引擎在各种负载下运行顺畅,并最大程度地减少排放。控制系统的主要组成部分是ECU(电子控制单元),它是一种具有微处理器的电子设备,可接收传感器信号并控制汽车的各种功能,从燃......
  • 汽车代码的实现 - 用python、c++写的自动驾驶系统
    汽车代码的实现-自动驾驶系统自动驾驶系统是近年来发展迅速的汽车代码领域之一。这个系统旨在使用传感器和计算机,自动化控制车辆行驶,消除驾驶员的错误和危险。实现自动驾驶系统需要使用一些重要的编程语言和技术,如Python、Matlab、深度学习和机器视觉算法。此外,还需要使用各种......
  • C++设计模式:单例模式
    什么是单例模式?单例模式指在整个系统生命周期里,保证一个类只能产生一个实例,确保该类的唯一性。单例模式分类有哪些?单例模式可以分为懒汉式和饿汉式,两者之间的区别在于创建实例的时间不同:懒汉式:指系统运行中,实例并不存在,只有当需要使用该实例时,才会去创建并使用实例。(这种方式要考......
  • C++ 子类调用父类的方法,静态方法的调用
    #include<iostream>classA{public:A();~A();virtualvoidsay(){std::cout<<"HellothisisclassA!\n";}staticvoidtest(){std::cout<<"HellothisisclassAtestfunction..!\n";}private:};......
  • c++ 与c#之间的字符串传递
    1.方法中不要直接返回字符串,防止内存崩溃。c++写法:voidnecall(char*str1,char*outdata){strcpy(outdata,str1);}outdata为导出数c#写法:[DllImport("testdemo")]privatestaticexternvoidnecall(stringa,StringBuilderb);StringBuilderb=new......
  • 利用PImpl在C++14中优雅调用C++17方法
    诉求你的工程由C++14写成,某天你看中了一个功能强大的三方库,一切都好除了该库仅支持C++17编译,对于比较复杂的三方库使用C++14进行重构工作量太大,有没有优雅的办法?实现历史总是惊人的相似,为了解决这一问题前人发明了PImpl编程方法用于隐藏class的实现细节,头文件中仅声明抽象class......
  • C++-语法复习
    记录一些刷算法题中的常用C++语法。STL相关向量Vector头文件:#include<vector>初始化:vector<数据类型>变量名(长度,初始化值)赋值初始化:vector<数据类型>变量名={1,2,3,4,5}可以作为数组数组开头:array.begin()数组结尾:array.end()数组大小:array.size()添加元素到......
  • c++中正确使用round()来四舍五入计算
    说明四舍五入的函数参数可以有多种数据类型。不同的数据类型有不同的结果。当他的参数应该是浮点数的时候,结果才是真正的四舍五入。例子/*g++-g-std=c++17./src/basic_demo.cpp-obasic_demo*/#include<iostream>#include<cmath>voidtest_round(){//参数是......