std::thread 创建线程的几种方式

简介

本文主要介绍标准C++中 thread的创建线程的几种方式。

使用时需要加头文件:

#include

位于std命名空间中,是跨平台的线程操作

 

使用说明

1、通过函数指针创建

一般来说,像CreateThread、_beginthread等创建线程时,都会先写一个含有一个参数(LPVOID lpParam)的全局函数,用于通过函数指针创建线程。

在标准C++提供的 thread方法中,函数指针的方式也需要一个全局函数,不过参数个数已经不再受限制

比如

void func(int param1,int param2,int param3);//全局函数

thread t(func,param1,param2,param3);//创建线程

此种方式通过参数的形式传递线程数据

 

2、通过函数对象创建

通过构建一个函数对象传给thread,以创建线程,因为函数对象的创建方式多样,所以对应创建线程的方式也有多种,具体参照示例代码。

通过函数对象,可以向函数对象类添加成员变量,并初始化和使用这些变量,以传递线程数据

 

3、通过lamda表达式创建

构建一个lambda表达式创建线程,通过lambda参数传递数据。

 

4、通过成员函数创建

通过成员函数创建线程,可以在不同的线程中执行对象的方法。在多线程中,如果访问同一个对象,需要保证线程安全,避免竞争条件。

 

示例代码

#include 
#include 

using namespace std;

void counter( int id, int numIter )
{
    for( int i = 0; i < numIter; ++i )
    {
        cout << "counter id:" << id << endl;
        cout << "iteraion:" << i << endl;
    }
}

//通过函数指针创建线程
int main1()
{
    thread t1( counter, 1, 6 );
    thread t2( counter, 2, 4 );

    //如果没有join,main函数加载两个线程后立即结束,导致线程也中止
    //可以确保主线程一直运行,直到两个线程都执行完毕
    t1.join();
    t2.join();

    //从不同线程中访问cout是线程安全的,没有任何数据竞争
    //即使没有数据竞争,不同线程的输出仍然可以交错(可以通过同步解决)
    system( "pause" );
    return 0;
}

class CCount
{
public:
    CCount( int id, int numIter ): m_ID( id ), mNum( numIter )
    {

    }

    void operator()()const
    {
        for( int i = 0; i < mNum; ++i )
        {
            cout << "counter id:" << m_ID << endl;
            cout << "iteraion:" << i << endl;
        }
    }
private:
    int m_ID;
    int mNum;
};

//通过函数对象创建线程
int main2()
{
    //方法1
    thread t1{ CCount{1, 20} };
    t1.join;

    //方法2
    CCount c( 2, 12 );
    thread t2( c );
    t2.join();

    //方法3
    thread t3( CCount( 3, 10 ) );
    t3.join();

    system( "pause" );
    return 0;
}

//通过lambda创建线程
int main3()
{
    int id = 1;
    int numIter = 10;

    thread t1( [id, numIter]
    {
        for( int i = 0; i < numIter; ++i )
        {
            cout << "counter id:" << id << endl;
            cout << "iteraion:" << i << endl;
        }
    } );
    t1.join();
    system( "pause" );
    return 0;
}

class MyClass
{
public:
    MyClass( int id = 0, int nNum = 0 ) : m_ID( id ), mNum( nNum )
    {

    }
    ~MyClass() = default;

    void func()
    {
        for( int i = 0; i < mNum; ++i )
        {
            cout << "counter id:" << m_ID << endl;
            cout << "iteraion:" << i << endl;
        }
    }

private:
    int m_ID;
    int mNum;
};

//通过成员函数创建线程
int main()
{
    MyClass mc( 1, 5 );
    thread t1( &MyClass::func, &mc );
    t1.join();
    system( "pause" );
    return 0;
}

 

 

你可能感兴趣的:(C++11,多线程编程)