C lambda值引用 失效

#include 
#include 
#include 
#include 
using namespace std;

std::thread thrd;
void work(const std::function &func)
{
	func();
}
void startwork()
{
	int a = 1;
	cout << hex << &a << endl;
	auto fun = [&](){
		std::this_thread::sleep_for(std::chrono::milliseconds(10000));
		cout << hex << &a << " " << a << endl;
	};
	thrd.swap(std::thread(std::bind(&work, fun)));
}

int main(int argc, char **argv)
{
	startwork();

	if (thrd.joinable()) thrd.join();
	system("pause");
	return 0;
}

       输出结果:

                     002BF768

                     002BF768   cccccccc

        注意,捕获引用时,注意引用对象的作用域。

你可能感兴趣的:(C lambda值引用 失效)