首页 > 其他分享 >cpp condition_variable wait_until unique_mutex time_out

cpp condition_variable wait_until unique_mutex time_out

时间:2023-04-19 22:27:05浏览次数:41  
标签:std now chrono duration mutex time variable include

#include <chrono>
#include <condition_variable>
#include <ctime>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#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::condition_variable cv;
std::mutex _mtx;
bool is_done = false;
void condition_variable_wait_for_seconds(const int &wait_seconds)
{    
    std::chrono::time_point<std::chrono::high_resolution_clock> time_out = std::chrono::high_resolution_clock::now() + std::chrono::seconds(wait_seconds);
    std::unique_lock<std::mutex> lk(_mtx);
    while (1)
    {
        if (cv.wait_until(lk, time_out) == std::cv_status::timeout)
        {
            is_done = true;
            break;
        }
    } 
}

void log_file_time()
{     
    std::fstream w_file("log.txt", std::ios::app);
    if (!w_file.is_open())
    {
        std::cout << "Create or open log.txt failed!" << std::endl;
        return;
    }
    std::stringstream ss;
    std::uint64_t num = 0;
    while (!is_done)
    {
        ss << ++num << "," << get_uuid() << std::endl;
        if (num % 1000000 == 0)
        {
            w_file << ss.str();
            ss = std::stringstream();
            std::cout << num << "," << get_time_now() << "," << get_time_now() << std::endl;
        }
        if (is_done)
        {
            w_file << ss.str();
            ss = std::stringstream();
            std::cout << std::boolalpha << num << "," << get_time_now() << "," << get_time_now()<< ",is_done:" << is_done << std::endl;
            break;
        }
    } 
}

void mt_condition_variable(const int &wait_seconds)
{
    std::stringstream ss;
    ss << get_time_now() << ",start in " << __FUNCTION__ << std::endl;
    std::thread t1(condition_variable_wait_for_seconds, std::cref(wait_seconds));
    std::thread t2(log_file_time);
    t1.join();
    t2.join();
    ss << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
    std::cout << ss.str();
}

int main(int args, char **argv)
{
    mt_condition_variable(atoi(argv[1]));
}

Compile

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

Run

./h1 1000

 

标签:std,now,chrono,duration,mutex,time,variable,include
From: https://www.cnblogs.com/Fred1987/p/17334837.html

相关文章

  • timeout news
    ......
  • MySQL 时间类型 date、datetime 和 timestamp 的用法与区别
    时间范围datetime和timestamp区别时间范围不一样,TIMESTAMP要小很多,且最大范围为2038-01-1903:14:07.999999,到期也不远了。datetime与时区无关、timestamp与时区有关。对于timestamp,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储。查询时,将其又转......
  • time_t now=time(NULL); std::cout<<ctime(&another_time);tm* ltm = localtime(&now)
    #include<iostream>#include<iomanip>#include<ctime>#include<windows.h>intmain(){time_tnow=time(NULL);tm*ltm=localtime(&now);std::cout<<ctime(&now);std::cout<<"Year:"......
  • Python小练习:解决strftime()中国时区乱码问题
    Python小练习:解决strftime()中国时区乱码问题作者:凯鲁嘎吉-博客园 http://www.cnblogs.com/kailugaji/1.mytest.py1#-*-coding:utf-8-*-2#Author:凯鲁嘎吉CoralGajic3#https://www.cnblogs.com/kailugaji/4#Python小练习:解决strftime()中国时区乱码问......
  • 浏览器报错net::err_connection_timed_out 怎么解决
    误代码“err_connection_timed_out”的解决方案2017-04-2011:02曾经试过用浏览器访问一个或几个特定网站,出现“err_connection_timed_out”错误,而其他网站加载良好没有任何问题或错误。上网查过发现这是一个常见的互联网错误。发生此DNS错误是因为访问或浏览该网站时,Internet......
  • Qt多线程之QMutex
    QMutex同一个QMutexlock()时,其他的lock()操作要等待locked的地方unlock()(可以保证顺序执行)测试代码:#include<iostream>#include<QThread>#include<QMutex>usingnamespacestd;classmyThread1:publicQThread{public:myThread1(int&num,QMutex&......
  • go语言sync.Mutex
    go语言sync.mutex数据结构typeMutexstruct{ stateint32 semauint32}Mutex包含了两个字段,分别是state、sema,state表示了当前锁的状态,sema是用于控制锁的一个信号量。这是一个零值可用的结构体,零值表示未加锁state字段的最低三位表示三种状态,分别是mutexLockedmute......
  • [Maven] The JAVA_HOME environment variable is not definded...... 配置错误日志
    ...因为是第一次配置Maven,所以出现了很多错误所以列举一二。出现找不到Java_Home环境变量的时候,如图。这个时候可能是,你的Java_Home路径配置错误,在Java_Home路径中,将\bin删掉,在path后面跟\bin,如%java_home%\bin,而不是直接在java_home中直接写死。......
  • HttpRuntime.Cache[key] as List<string>竟然会引用转值改变Cache的值
    HttpRuntime.Cache[key]asList<string>竟然会引用转值改变Cache的值此时HttpRuntime.Cache[key]的值会发生改变 ......
  • Again Prime? No Time. UVA - 10780
    给定m,n,求最大的k使得m^k∣n! 分解质因数   #include<iostream>#include<cstring>#include<sstream>usingnamespacestd;constintN=1e4+20;constintinf=1e9;intn,m,a[N],b[N];intprime[N],tot,vis[N];voidinit(inttop){ for(......