cpp std::move thread

复制代码
#include <assert.h>
#include <atomic>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <numeric>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>
#include <vector>

void print_num(const int &len)
{
    std::cout << "Thread Id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
    for (int i = 0; i < len; i++)
    {
        std::cout << i + 1 << ",";
    }
    std::cout << std::endl;
}

void thread_move(const int &len)
{
    try
    {
        std::cout << "Thread Id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
        std::thread t1(print_num, std::cref(len));
        std::cout << std::boolalpha << "t1.joinable()" << t1.joinable() << std::endl;
        std::thread t2 = std::move(t1);
        std::cout << std::boolalpha << "t1.joinable()" << t1.joinable() << std::endl;
        std::cout << std::boolalpha << "t2.joinable()" << t2.joinable() << std::endl;
        t2.join();
        t1.join();
    }
    catch (const std::exception &e)
    {
    }
}


int main(int args, char **argv)
{
    std::cout << "Thread Id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
    thread_move(atoi(argv[1])); 
}
复制代码

 

Compile

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

 

 

Run

./h1 10;

 

 

posted @   FredGrit  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示