前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++多线程的三种创建方式

C++多线程的三种创建方式

作者头像
Ch_Zaqdt
发布2020-02-16 13:51:04
3.9K0
发布2020-02-16 13:51:04
举报
文章被收录于专栏:Zaqdt_ACMZaqdt_ACM

       C++11的新特性std::thread的创建方式主要有:函数指针,类或结构体,lambda表达式。下面将会附上这三种创建线程方式的code。

首先第一个是通过函数指针的方式来创建:

不带参数:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

void A() {
    cout << "this is A thread" << endl;
}

int main()
{
    thread t(A);
    t.join();
    cout << "this is main thread" << endl;
    return 0;
}

带参数:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

void A(int a,int b) {
    cout << "this is A thread" << endl;
}

int main()
{
    thread t(A, 2, 3);
    t.join();
    cout << "this is main thread" << endl;
    return 0;
}

       第二种是通过类和结构体来创建,那么对于类来说结构体就相当于一个访问限定符为public的类,那么下面就以类来举例。类的对象是不能通过像函数那样直接去创建线程的,那么就需要对()进行运算符重载,使其变为一个仿函数,从而再去通过这个入口去创建线程。

不带参数:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

class A {
public:
    void operator()() const{
        cout << "this is A thread" << endl;
    }
};

int main()
{
    thread t1{A{}};
    t1.join();

    A a;
    thread t2(a);
    t2.join();

    thread t3 = thread(A());
    t3.join();

    cout << "this is main thread" << endl;
    return 0;
}

带参数:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

class A {
public:
    A(int a,int b) : m_iA(a), m_iB(b) {}
    void operator()(int a,int b) const{
        cout << a + b << endl;
        cout << m_iA + m_iB << endl;
        cout << "this is A thread" << endl;
    }
private:
    int m_iA;
    int m_iB;
};

int main()
{
    // 下列输出均为5 3
    thread t1{ A{1,2} ,2, 3};
    t1.join();

    A a(1, 2);
    thread t2(a, 2, 3);
    t2.join();

    thread t3 = thread(A(1, 2), 2, 3);
    t3.join();

    cout << "this is main thread" << endl;
    return 0;
}

       第三种就是通过lambda表达式进行创建,其实lambda和函数指针的方法是差不多的,先来看一下比较简单的lambda的不带参数的创建线程的方法:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

int main()
{
	auto f = [] {
		cout << "This is f thread" << endl;
	};
	thread t(f);
	t.join();
	cout << "this is main thread" << endl;
	return 0;
}

       为了方便,我们也可以这么去写:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

int main()
{
	thread t = thread([]{
		cout << "This is f thread" << endl;
	});
	t.join();
	cout << "this is main thread" << endl;
	return 0;
}

       带上参数就可以这么去写:

代码语言:javascript
复制
#include <iostream>
#include <thread>
using namespace std;

int main()
{
	int a = 1;
	thread t = thread([a](int b,int c){
		cout << a + b + c << endl;              // 输出为6
		cout << "This is f thread" << endl;
	}, 2, 3);
	t.join();
	cout << "this is main thread" << endl;
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档