c++11 std::thread调用方法

std::thread调用函数

// test.cpp

#include <iostream>
#include <thread>


void print_test(int _value)
{
	std::cout << _value << std::endl;
}

int main(int argc, char* argv[])
{
	std::thread threads[10];

	for (int i = 0; i < 10; i++)
		threads[i] = std::thread(print_test, i + 1);

	for (int i = 0; i < 10; i++)
		threads[i].join()

	return 0;
}

std::thread不在类中调用类的方法

// a.h

#pragma once


class a
{
private:
public:
	void print_test(int _value);
};
// a.cpp

#include <iostream>

#include "a.h"


void a::print_test(int _value)
{
	std::cout << _value << std::endl;
}
// test.cpp

#pragma once
#include <iostream>
#include <thread>

#include "a.h"

int main(int argc, char* argv[])
{
	std::thread threads[10];

	for (int i = 0; i < 10; i++)
	{
		a test;
		threads[i] = std::thread(&test::print_test, test, i + 1);
	}

	for (int i = 0; i < 10; i++)
		threads[i].join();

	return 0;
}

std::thread在类中调用类的方法

// a.h

#pragma once


class a
{
private:
public:
	void print_test(int _value);
	void thread_test();
};
// a.cpp

#include <iostream>
#include <thread>

#include "a.h"


void a::print_test(int _value)
{
	std::cout << _value << std::endl;
}

void a::thread_test()
{
	std::thread threads[10];

	for (int i = 0; i < 10; i++)
		threads[i] = std::thread(&a::print_test, this, i + 1);

	for (int i = 0; i < 10; i++)
		threads[i].join();
}