首页 > 其他分享 >cpp future,get,sleep_for,third variable

cpp future,get,sleep_for,third variable

时间:2023-04-29 19:44:05浏览次数:33  
标签:std now third chrono get sleep time variable include

#include <chrono>
#include <condition_variable>
#include <ctime>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <random>
#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;
}

bool is_finish=false;

std::string get_str_len_is_finish()
{ 
    static int num=0;
    std::stringstream ss;
    while(!is_finish)
    {
        for(int i=0;i<100000;i++)
        {
            ss<<++num<<","<<get_uuid()<<std::endl;
            if(is_finish)
            {
                break;
            }
        }
        if(is_finish)
        {
            break;
        }
    }
    return ss.str();
}

void get_str_len_is_finish_detach(const int&sleep_seconds)
{
    std::future<std::string> fut=std::async(get_str_len_is_finish);
    std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds));
    is_finish=true;
    std::cout<<fut.get()<<std::endl;
    std::cout<<get_time_now()<<",finish in "<<__FUNCTION__<<std::endl;
}

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

 

Compile

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

 

Run

./h1 10

 

 

标签:std,now,third,chrono,get,sleep,time,variable,include
From: https://www.cnblogs.com/Fred1987/p/17364404.html

相关文章

  • C++-std::this_thread::get_id()-获取线程id
    C++-std::this_thread::get_id()-获取线程idstd::this_thread::get_id()头文件:<thread>函数:std::this_thread::get_id()用例:std::thread::idthread_id=std::this_thread::get_id();std::thread对象的成员函数get_id()头文件:<thread>函数:std::thread::idget_id()用例:......
  • MFC-GetItemCount获取项目总数
     intnCount=mylist4.GetItemCount();//获取项目总数    ......
  • [ABC142E] Get Everything
    2023-02-18题目题目传送门翻译翻译难度&重要性(1~10):5题目来源AtCoder题目算法状压dp解题思路我们令\(S\)表示当前箱子状态,\(P_i\)表示第\(i\)把钥匙能开的箱子。设\(f_S\)表示开启当前状态箱子的最小花费。能得到转移方程:\(f_{P_i|i}=\min(f_{P_i|i},f_i+......
  • MFC-GetWindowLong获取窗口样式、窗口标识符ID、处理函数
     获取窗口样式LONGStyles=GetWindowLong(hWnd4,GWL_STYLE);//获取窗口风格/*参数1:HWNDhWnd窗口句柄参数2:intnIndex改变窗口上的何种属性窗口属性包括窗口的样式(GWL_STYLE)、扩展样式(GWL_EXSTYLE)、窗口函数、窗......
  • System.getProperty()参数大全
    java.versionJavaRuntimeEnvironmentversionjava.vendorJavaRuntimeEnvironmentvendorjava.vendor.urlJavavendorURLjava.homeJavainstallationdirectoryjava.vm.specification.version......
  • Unity动画系统详解9:Target Matching是什么?
    摘要:在游戏中,经常有这种情况:角色的手或者脚需要在特定时间放在特定的位置。比如角色需要用手撑着跳过一个石头或一堵墙,或者跳起抓住房梁。TargetMatch就是让动画的特定片段去匹配特定的位置。洪流学堂,让你快人几步。你好,我是跟着大智学Unity的萌新,我叫小新,这几周一起来复(yu)习(xi)动......
  • 一统天下 flutter - widget 基础: Key - 键
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget基础:Key-键示例如下:lib\widget\basic\key.dart/**Key-键**LocalKey和GlobalKey用于让element找到widget*GlobalKey也可以用于获取对应的Widget/State/Rende......
  • pyqt5-QListWidget
    1、介绍list组件,或者说列表组件。2、类和初始化classQListWidget(QListView):快速查询:QListWidget(parent:typing.Optional[QWidget]=None)addItem(self,aitem:QListWidgetItem)addItem(self,label:str)addItems(self,labels:Iterable[str])clear(self)closeP......
  • 一统天下 flutter - widget 布局类(可以有多个子): CustomMultiChildLayout - 自定义多
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget布局类(可以有多个子):CustomMultiChildLayout-自定义多组件布局示例如下:lib\widget\layout\custom_multi_child_layout.dart/**CustomMultiChildLayout-自定义多组件布局*......
  • 一统天下 flutter - widget 容器类(只能有一个子): CustomSingleChildLayout - 自定义单
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget容器类(只能有一个子):CustomSingleChildLayout-自定义单组件布局示例如下:lib\widget\container\custom_single_child_layout.dart/**CustomSingleChildLayout-自定义单组件布......