首页 > 其他分享 >cpp condition_variable wait_for unique_mutex,chrono::seconds

cpp condition_variable wait_for unique_mutex,chrono::seconds

时间:2023-06-25 15:13:53浏览次数:48  
标签:std now uuid chrono seconds time variable include

#include <chrono>
#include <condition_variable>
#include <ctime>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <thread>
#include <uuid/uuid.h>
#include <vector>

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()
{
    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;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto 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::atomic<bool> _is_write_finished(false);
std::condition_variable _cv;
std::mutex _mtx;
void cv_atomic_write_file(const std::string &file_name)
{
    std::fstream w_file(file_name, std::ios::app);
    if (!w_file.is_open())
    {
        std::cout << "Create or open " << file_name << " failed!!!" << std::endl;
        return;
    }
    static std::uint64_t num = 0;
    int loops = 0;
    std::stringstream ss;

    while (!_is_write_finished.load(std::memory_order_seq_cst))
    {
        for (int i = 0; i < 100000; i++)
        {
            ss << ++num << "," << get_uuid_value() << std::endl;
        }
        w_file << ss.str();
        ss = std::stringstream();
        std::cout<<std::boolalpha<<"is_write_finished:"<<_is_write_finished<<","<< get_time_now() << ",loops:" << ++loops << ",num:" << num << std::endl;
    }
    w_file.close();
    std::cout << get_time_now() << ",finish id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
}

void cv_wait_for(const std::string&file_name,const int &wait_seconds)
{
    std::thread t1(cv_atomic_write_file,std::cref(file_name)); 
    std::unique_lock<std::mutex> _lk(_mtx);    
    _cv.wait_for(_lk,std::chrono::seconds(wait_seconds));
    _is_write_finished.store(true, std::memory_order_seq_cst);
    std::cout<<std::boolalpha<<"is_write_finished:"<<_is_write_finished<<std::endl; 
    t1.detach();
    std::cout << get_time_now() << ",finish id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
}

int main(int args, char **argv)
{
    // write_file_for_loops(argv[1],atoi(argv[2]));
    // future_get_for_len(atoi(argv[1]));
    // promise_set_value_future_get(atoi(argv[1]));
    // atomic_write_file_store(atoi(argv[1]), argv[2]);
    cv_wait_for(argv[1],atoi(argv[2]));
    std::cout << get_time_now() << ",id:" << std::this_thread::get_id() << " in " << __FUNCTION__ << std::endl;
}

 

Compile

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

Run

./h1 log2.txt 100

 

标签:std,now,uuid,chrono,seconds,time,variable,include
From: https://www.cnblogs.com/Fred1987/p/17502936.html

相关文章

  • Asynchronous & Synchronous Reset
    第1章复位的目的复位信号在数字电路里面的重要性仅次于时钟信号。对一个芯片来说,复位的主要目的是使芯片电路进入一个已知的,确定的状态。主要是触发器进入确定的状态。在一般情况下,芯片中的每个触发器都应该是可复位的。在某些情况下,当在高速应用程序中使用流水线触发器(移位寄......
  • C++ 计时方法 std::chrono
    计时的作用:测试某一段代码的运行时间,时间越短,则性能相对越高。C++11标准的”最佳计时方法“的代码:1#include<chrono>2usingnamespacestd;3usingnamespacechrono;45autostart=system_clock::now();6//dosomething...7autoend=system_clock::no......
  • C++ 计时器:chrono库介绍
    C++11有了chrono库,可以在不同系统中很容易的实现定时功能。要使用chrono库,需要#include,其所有实现均在std::chrononamespace下。注意标准库里面的每个命名空间代表了一个独立的概念。chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock一、时......
  • Proj. CAR Paper Reading: Augmenting Decompiler Output with Learned Variable Name
    Abstract背景:decompilers难以恢复注释、variablenames,customvariabletypes本文:工具:DIRTY((DecompIledvariableReTYper)方法:postprocessesdecompiledfiles基于:Transformer训练数据:Github效果:实验:outperformspriorworkapproachesbyasizablemargin......
  • std::thread 三:条件变量(condition_variable())
     condition_variable 、  wait  、  notify_one  、  notify_all *:notify_one:通知(唤醒)一个线程*:notify_all:通知(唤醒)多个线程 #include<iostream>#include<thread>#include<mutex>#include<list>usingnamespacestd;classA{public:......
  • 解决MySQL8.0报错:Unknown system variable 'validate_password_policy'
    解决MySQL8.0报错:Unknownsystemvariable'validate_password_policy'解决MySQL8.0报错:Unknownsystemvariable'validate_password_policy'一、问题描述1.通过yum安装好mysql8.0,通过生成的临时密码登录后操作mysql时会报如下错误:mysql>showdatabases;ERROR1820......
  • Rust 声明式宏中的 Metavariables 有哪些
    Metavariables官方文档确实写得很好,但是缺少一些风味,容易催眠......
  • C++的多线程编程(练习一下condition_variable)
        嗯,高考结束了,那就编写一个阅卷和查成绩的多线程吧。一个线程老师阅卷,其他三个线程查成绩。代码如下:    1#include<iostream>2#include<thread>3#include<mutex>4#include<condition_variable>5#include<chrono>6#include<futu......
  • [ABC166F] Three Variables Game
    ThreeVariablesGameの传送门Solution首先,我们每次操作只会修改两个数。所以考虑dfs枚举操作的顺序,但是这让时间复杂度变为\(O(2^n)\),不能接受。但是,我们可以判断当a<0||b<0||c<0时,就退出,这样可以减少绝大部分状态。边界条件:当枚玩\(n\)个后,输出用\(ans......
  • [从jQuery看JavaScript]-变量与作用域链(Variable and Scope Chain)
    jQuery片段: 1.var2.//Willspeedupreferencestowindow,andallowsmungingitsname.3.window=this,4.//Willspeedupreferencestoundefined,andallowsmungingitsname.5.undefined,6.//MapoverjQueryinc......