线程回调函数形参不能用引用

时间:2024-01-27 16:10:44

创建线程后,添加的回调函数中,如果加入了引用符号,编译时会报如下错误:

错误 C2893 未能使函数模板“unknown-type std::invoke(_Callable &&,_Types &&...)”

include

include

include

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 ptr(new sharedClassTest);
ptr->test();
system("pause");
return 0;
}

需要将引用改为指针即可void init(int v, int* z)