首页 > 其他分享 >cpp shared_future

cpp shared_future

时间:2023-04-05 22:23:35浏览次数:36  
标签:std include chrono duration future time cpp shared now

#include <chrono>
#include <ctime>
#include <future>
#include <iomainp>
#include <iostream>
#include <sstream>
#include <uuid/uuid.h>

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::string future_str(const int&len)
{
    std::stringstream ss;
    for(int i=0;i<len;i++)
    {
        ss<<i+1<<","<<get_uuid()<<std::endl;
    }
    return ss.str();
}

void std_shared_future(const int&len)
{
    std::future<std::string> fut=std::async(future_str,std::cref(len));
    std::shared_future<std::string> shared_fut(std::move(fut));
    std::cout<<shared_fut.get()<<std::endl;
    std::cout<<get_time_now()<<",finished in "<<__FUNCTION__<<std::endl;
}

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

 

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

 

标签:std,include,chrono,duration,future,time,cpp,shared,now
From: https://www.cnblogs.com/Fred1987/p/17291115.html

相关文章

  • cpp: sort Algorithmic
      //TenSortAlgorithms.h:此文件包含"TenSortAlgotrthms"类。十个常用排序算法C++14//2023年4月5日涂聚文GeovinDuedit.#ifndefTENSORTALGORITHMS_H#defineTENSORTALGORITHMS_H#include<vector>//#includedirective#include<string>#include&l......
  • cpp generate random array then sort by quick sort
    #include<chrono>#include<ctime>#include<iomainp>#include<iostream>#include<random>#include<sstream>std::stringget_time_now(){std::chrono::time_point<std::chrono::high_resolution_clock>now=st......
  • shared_ptr 自定义 deleter 删除器
    make_shared不支持自定义删除器shared_ptr<Bar>sp2(sp1,deleter1);无法指定自定义删除器?《C++Primer5th》P413错误?sp1不能是智能指针,可以是内置指针reset参数为内置指针,可以重新指定删除器测试代码classBar{public:Bar(intii):i(ii){cout<<......
  • yaml-cpp YAML格式处理库的介绍和使用(面向业务编程-文件格式处理)
    yaml-cppYAML格式处理库的介绍和使用(面向业务编程-文件格式处理)YAML格式介绍YAML的格式介绍,有关ini、json和xml或许很多人已经很了解了,但是关于YAML,还有许多人不了解。YAML被设计成更适合人类阅读(我想正因为如此,所以相对来说更灵活,就导致到使用的时候很多人会觉得它看起来并不......
  • JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)
    JsonCppJSON格式处理库的介绍和使用(面向业务编程-文件格式处理)介绍JSON是一种轻量级的数据交换格式,它是一种键值对的集合。它的值可以是数字、字符串、布尔值、序列。想知道更多有关JSON格式的介绍,可以到JSON的官网json.org学习JsonCpp是一个c++库,允许对JSON值进行操作,包括......
  • cpp: create class
    PigInfo.h#ifndefPIGINFO_H#definePIGINFO_H#include<iostream>#include<string.h>#include<math.h>usingnamespacestd;/*实体类https://learn.microsoft.com/zh-cn/cpp/standard-library/cpp-standard-library-header-files?view=msvc-170......
  • 记几次 [线上环境] Dubbo 线程池占满原因分析(第二次:CompletableFuture)
    转载:https://blog.csdn.net/wsmalltiger/article/details/124236189文章目录[线上环境]Dubbo线程池占满原因排查系列前言一、问题分析1、分析日志2、定位原因二、解决方案三、总结前言  某天早上9点左右收到线上故障报警,超过3个商家反馈“无法正常进入功能页面,点击相关操作提......
  • Chisel3 使用 DPI-C,发现在 Chisel 环境下 printf 没问题,但是 set_pc 死活传不到 cpp
    大概率是因为你使用了SignExt之类的封装这类封装只会把”值“传给DPI-C,而不会把线连给DPIC,即,传过去的是调用set_pc时的值,而不是引用这样会造成CPP获取不了相应线路的指针 如下图     这些也是错的......
  • 线程池之FutureTask、ThreadPoolExecutor源码分析
    前言在我们日常工作中,我们经常会用到多线程的来处理一些异步任务,提高系统吞吐量,但是线程毕竟是一种昂贵的系统的资源,我们不应该频繁的去申请销毁。在java的projectloom还未正式release的时候,我们常用池化的方式来使用线程。所以了解线程池的设计思想能够有助于我们更好的使用它,......
  • c++ std::package_task,task.get_future()
    #include<iostream>#include<future>#include<thread>intcountdown(intfrom,intto){for(inti=from;i!=to;--i){std::cout<<i<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));......