首页 > 其他分享 >thread promise get_future(),get(), promise set_value()

thread promise get_future(),get(), promise set_value()

时间:2023-04-03 22:33:05浏览次数:32  
标签:std now set chrono get promise time include

#include <chrono>
#include <ctime>
#include <future>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <unistd.h>
#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;
}

void fill_promise(std::promise<std::string> *promise_obj,const int&len)
{
    std::stringstream ss;
    for(int i=0;i<len;i++)
    {
        ss<<i+1<<","<<get_uuid()<<std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    std::cout<<get_time_now()<<",in "<<__FUNCTION__<<std::endl;
    promise_obj->set_value(ss.str());
}

void thread_promise_get(const int &len)
{
    std::promise<std::string> promise_obj;
    std::thread t1(fill_promise,&promise_obj,std::cref(len));
    std::cout<<promise_obj.get_future().get()<<std::endl;
    t1.join();
}

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

 

Compile

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

 

标签:std,now,set,chrono,get,promise,time,include
From: https://www.cnblogs.com/Fred1987/p/17284712.html

相关文章

  • AirCassette音乐应用:复古情愫与现代社交元素的完美融合
    随着iPod和iPhone等现代设备的涌现,音乐已变得无处不在。在享受数字音乐带来的轻松体验时,是否也会偶尔怀念那个老式随身听和磁带的年代?AirCassette就是这样一款融合了怀旧情感和现代社交元素的iOS音乐应用。通过这款时尚的应用,用户可在播放数字音乐时体验磁带带来的视觉享受,同时......
  • (4.3)数组、对象及类数组对象,set的用法,正则表达式的常用方法,蓝桥杯备赛-(生成数组、数
    1.1数组、对象及类数组对象1.数组:​ 数组是有序的数据集合,其中的索引值从0开始递增,并且数组有length属性,可以获取数组内的元素个数,其中的值可以是任何的数组类型。2.对象:​ 对象是无序的是由一个或多个键值对组成的数据集合,对象没有length属性。3.伪数组(类数组对象):​ ......
  • 启动redis时,告警日志中出现“The TCP backlog setting of 511……”以及“overcommit_
    问题描述:启动redis时,告警日志中出现“TheTCPbacklogsettingof511……”以及“overcommit_memoryissetto0…..”警告,如下所示:系统:rhel7.9数据库:redis6.2.61、异常重现[[email protected]]#redis-serverredis6379.conf[root@leo-redis626-aredis-6.......
  • Java 获取当前或调用者类名和方法名(Thread.currentThread().getStackTrace()、new Thr
    Java获取当前或调用者类名和方法名(Thread.currentThread().getStackTrace()、newThrowable().getStackTrace())原文链接:https://blog.csdn.net/inthat/article/details/111885544文章目录一、Java获取当前类名和方法名Thread.currentThread().getStackTrace()1.关于Thr......
  • C# Nuget版本号排序
    Nuget包版本号和我们软件应用版本号一样,不过因为稳定性等的考虑,组件版本有更高的要求。预发布版本使用频率更高版本号介绍,详见我朋友胡承老司机的博客:Nuget包的版本规范(qq.com)比如1.0.1-alpha.2,表示1.0.1有个开发联调版本alpha,alpha版本下面有构建号次数2。也有开发在构建号......
  • 给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的
    /***给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。**你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。**你可以按......
  • 加载spring配置的两个方法AnnotationConfigApplicationContext()和getRootConfigClass
    在Spring中,AnnotationConfigApplicationContext类和AbstractAnnotationConfigDispatcherServletInitializer类中的getRootConfigClasses()方法都是用来加载Spring配置类,并创建Spring容器的。因此,它们的作用是相似的,都是用来配置Spring容器的。但是,它们的使用场景和......
  • redis使用setnx+lua实现分布式锁
    在Redis中,使用SETEX命令(对应RedisTemplate的setIfAbsent方法)可以实现一个最简易的分布锁。SETEX命令当key不存在的话,才会设置key的值,如果可以已经存在,就不做任何操作。为了避免锁无法被释放,就给这个key(也就是锁)设置一个过期时间。为了保证解锁操作的原子性,使用Lua脚本进行释放锁......
  • StreamSets单机版安装文档
    StreamSets单机版安装文档解压安装包到opt目录tar-zxvfstreamsets-datacollector-common-3.22.3.tgz-C/opt修改为sdc配置为表单认证cd/opt/streamsets-datacollector-3.22.3/vietc/sdc.propertieshttp.authentication=form后台启动nohup./bin/streamsets......
  • es(Elasticsearch)查询报错: Set fielddata=true on [level] in order to load fieldd
     Invocationofinitmethodfailed;nestedexceptionisElasticsearchStatusException[Elasticsearchexception[type=search_phase_execution_exception,reason=allshardsfailed]];nested:ElasticsearchException[Elasticsearchexception[type=illegal_argument_......