c++ thread线程join、detach、joinable方法

(621条消息) 线程中断Thread的interrupt()方法_thread interrupt_萝卜阿咕咕的博客-优快云博客

C/C++编程:std::thread 详解-优快云博客

#include <iostream>
#include <thread>

void do_some_work()
{
    std::cout<<"Hello Concurrent World\n";
}

int main()
{
    std::thread t(do_some_work);
    t.join();
}



#include<thread>
#include<iostream>
 
int main()
{
    std::thread t([](){
        std::cout<< "lambda thread"<< std::endl; 
    });
    
    t.join();
    std::cout << "resume main thread" << std::endl;
    return 0;
}
 
// 打印结果
 
lambda thread
resume main thread
 

C++:std::thread:线程用法_std::thread用法-优快云博客

join、detach、joinable方法

C++多线程:线程的创建、join、detach、joinable方法(二)_c++ joinable-优快云博客

#include <iostream>
#include <thread>

void thread_func1()
{
    std::cout << "子线程开始执行" << std::endl;

    std::cout << "子线程执行完毕" << std::endl;
}

int main()
{
    std::thread mythread1(thread_func1);
    if(mythread1.joinable()){
        std::cout << "joinable() == true" << std::endl;
        mythread1.join();
    }
    else{
        std::cout << "joinable() == false" << std::endl;
    }
    std::cout << "------------------------------" << std::endl;
    if(mythread1.joinable()){
        std::cout << "joinable() == true" << std::endl;
    }
    else{
        std::cout << "joinable() == false" << std::endl;
    }
    std::cout << "main thread executed finish!" << std::endl;
    return 0;
}
joinable方法
  • joinable方法主要判断是否可以使用join方法或者detach方法,可以返回true,不可以返回false
  • 一个线程最多只能调用一次join或者detach

使用make_shared <std :: thread>创建shared_ptr <std :: thread>的实例 | (1r1g.com)

C++ std::thread的基础使用和管理 - 今天的小马同学 - 博客园 (cnblogs.com)

    std::thread thread_(&MobileComSettingsClient::subscribeNotifyThread,this);
    //thread_.join();        //阻塞
    thread_.detach();        //分离
main_thread_ = std::thread(&xxxx::InitThread, this);

(359条消息) C++11多线程_lie to me的博客-优快云博客_c++11 多线程

(360条消息) C++11中5种创建线程方法_求则得之,舍则失之的博客-优快云博客_c++新建线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值