错误:
1.error C2893: 未能使函数模板“unknown-type std::invoke(_Callable &&,_Types &&...)”专用化。
解决方案:参考https://blog.csdn.net/wangshubo1989/article/details/49593429。
参考https://www.cnblogs.com/renyuan/p/6513188.html 。 C++中 线程函数为静态函数 及 类成员函数作为回调函数
#include <thread>
#include <iostream>
using namespace std;
class TestClass
{
public:
void TestMethod();
void ThreadFunc();
static void* callback(void* arg);
protected:
private:
};
void TestClass::TestMethod()
{
//必须传入this
thread tTest(callback, this);
tTest.detach();
//tTest.join();
}
//应用回调函数,解决开启线程函数,第一个参数带来的问题
void* TestClass::callback(void* arg)
{
((TestClass *)arg)->ThreadFunc();
return NULL;
}
void TestClass::ThreadFunc()
{
while (true)
{
cout << "应用子线程,调用成员函数\n" << endl;
}
}
void main()
{
TestClass test;
test.TestMethod();
while (true)
{
cout << "主线程,执行了一段代码\n" << endl;
}
}
更多关于std::thread,可以参考以下内容:
https://zh.cppreference.com/w/cpp/thread/thread/thread 线程支持库
https://blog.csdn.net/liuker888/article/details/46848905 C++11并发之std::thread
https://www.cnblogs.com/slysky/p/3945126.html C++ 11 Lambda表达式、auto、function、bind、final、override
https://www.cnblogs.com/lidabo/p/3908663.html C++11新特性:Lambda函数(匿名函数)