建立執行緒後,新增的回撥函式中,如果加入了引用符號,編譯時會報如下錯誤:
錯誤 C2893 未能使函式模板“unknown-type std::invoke(_Callable &&,_Types &&...)”
#include <thread>
#include <iostream>
#include <memory>
class sharedClassTest {
public:
void init(int v, int& z) {
printf("hello mint: %d\n", v);
z = 2 * v;
}
void test()
{
int val = 0;
std::thread thr = std::thread(&sharedClassTest::init, this, 3, val);
for (int i = 0; i < 100; i++)
{
printf("test mint %d\n", i);
}
thr.join();
printf("val = %d\n", val);
}
};
int main()
{
std::shared_ptr<sharedClassTest> ptr(new sharedClassTest);
ptr->test();
system("pause");
return 0;
}
需要將引用改為指標即可void init(int v, int* z)