C++11 std::thread std::bind 启动多线程

#include <iostream>
#include <thread>

class Task
{
    public:
    void executeThread(std::string command)
    {
        for(int i = 0; i < 5; i++)
        {
            std::cout << command << " :: " << i << std::endl;
        }
    }

    void startThread(void);
    std::thread th2;
    std::thread th3;
};

void Task::startThread(void)
{
    std::cout << "\nthis: " << this << std::endl;

    #if 0
        std::thread th1(&Task::executeThread, this, "Thread1");
        th1.join(); // Without join(), thread will terminate
    #elif 0
        th2 = std::thread(&Task::executeThread, this, "Thread2");
        th2.join();
    #else
        th3 = std::thread( std::bind(&Task::executeThread, this, "Thread3") );
        th3.join();
    #endif
}

这里没有解决内存泄露的隐患,需要使用unique_ptr修饰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值