最近看C++并发编程实战发现基本上都是使用函数创建一个线程,再使用线程调用另外一个函数。
如果是一个类中需要这么做。
写个小demo作为笔记。
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <windows.h>
using namespace std;
class Quote
{
public:
void hello();
void createHelloThread();
};
void Quote::createHelloThread()
{
std::thread t([&]() {hello(); });
t.join();
}
void Quote::hello()
{
cout << "hello" << endl;
}
int main()
{
Quote qu;
qu.createHelloThread();
getchar();
return 0;
}