C ++ 11 threads, non-parametric method of creating threads

// 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;
}

 

Published 257 original articles · won praise 22 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_24127015/article/details/104790764