C++11 线程,无参线程的创建方法

// threadTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <thread>
using namespace std;

void hello()
{
	cout << "hello thread1 world" << endl;
}

class CHello
{
public:
	void operator()()
	{
		cout << "hello thread2 world" << endl;
	}
};

class Hello
{
public:
	void hello()
	{
		cout << "hello thread4 world" << endl;
	}
};
int main()
{
	// 使用普通函数,创建线程
	thread t1(hello);
	t1.join();

	// 使用仿函数创建线程
	CHello hi;
	thread t2(hi);
	t2.join();

	// 使用lambda表达式创建线程
	thread t3([] {
		cout << "hello thread3 world" << endl;
	});
	t3.join();

	// 使用类成员函数创建线程
	Hello h;
	thread t4(&Hello::hello, &h);
	t4.join();
    return 0;
}

发布了257 篇原创文章 · 获赞 22 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_24127015/article/details/104790764