c++ 线程创建的多种方法

基本创建线程

通过类创建线程

通过Lambda创建线程

带参数的线程创建方式

以智能指针为参数的线程

以类的成员函数去充当线程处理函数

  • cout<<this_thread::get_id()<<endl; // 获取线程Id
  • 注意点:
    • 创建一个线程,不做处理,会调用abort函数中止处理
    • 一个线程只能join一次,不能重复join
    • join函数 加入,汇合线程,阻塞主线程,等待子线程执行结束,才会回到主线程中
    • detach函数 分离 打破依赖关系,把子线程驻留后台
    • 当前detach后,就不能够在join
    • joinable判断当前线程是否可以join,可以返回true,不可以返回false

基本创建线程

#include <iostream>
#include <thread>
#include <unistd.h>

using namespace std;

void print()
{
    sleep(5);
    cout <<"子线程。。。。。。"<<endl;
}

int main()
{
    // 创建线程
    thread test1(print);
    thread test2(print);
    test1.join(); // 阻塞
    test2.join();                                                                                                          
    cout<<"主线程......"<<endl;
    return 0;
}

通过类创建线程

#include <iostream>
#include <thread>
                                                                                                                           
using namespace std;

class MM
{
public:
    void operator()()
    {
        cout<<"子线程。。。start"<<endl;
    }   
};  

int main()
{
    MM();
    // 正常写法 1
#if 0
    MM mm;
    thread test1(mm);
    test1.join();
#endif 
    // 正常写法 2
    thread test1((MM()));
    test1.join();
    return 0;
} 

通过Lambda创建线程

#include <iostream>
#include <thread>


using namespace std;

int Max(int a, int b)
{
    return a>b?a:b;
}

int main()
{
    int (*pMax)(int,int)=nullptr;
    pMax = [](int a, int b)->int {return a > b ? a : b;};
    []{cout << "hello world" <<endl;}();
    cout<<pMax(2,4)<<endl;

    thread test([](){cout << "子线程启动......" <<endl;});
    test.join();
    cout<<"主线程运行"<<endl;
    return 0;                                                                                                              
}

带参数的线程创建方式

#include <iostream>
#include <thread>

using namespace std;

//void printInfo(int &num)
void printInfo(int num)
{
    num++;
    cout<<"子线程\t"<<num<<endl;
}

int main()
{
    int num = 0;
    // std::ref 用于包装引用传递值
    // thread test1(printInfo,std::ref(num));                                                                              
    thread test1(printInfo,num);
    test1.join();
    cout<<"主线程"<<num<<endl;
    return 0;
}

以智能指针为参数的线程

#include <iostream>
#include <thread>

using namespace std;

void printInfo(unique_ptr<int> ptr)
{
    cout<<"子线程:"<<ptr.get()<<endl;
    cout<<this_thread::get_id()<<endl; // 获取线程Id                                                             
}

int main()
{
    unique_ptr<int> ptr(new int(1000));
    cout<<"主线程..."<<*(ptr.get())<<endl;

    thread test1(printInfo,move(ptr));
    test1.join();

    cout<<"主线程..."<<ptr.get()<<endl;
    return 0;
}

以类的成员函数去充当线程处理函数

#include <iostream>
#include <thread>

using namespace std;

class MM{ 
    public:
        void print(int &num)
        {   
            num  = 1001;
            cout<<"子线程..."<<this_thread::get_id()<<endl;
        }   
};


int main()
{
    MM mm; 
    int num = 1007;
    // 成员函数指针 是那个对象                                                                                   
    thread test1(&MM::print,mm,ref(num));
    test1.join();
    return 0;
}

posted on 2021-08-30 11:09  lodger47  阅读(1200)  评论(0)    收藏  举报

导航