版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csenjoy/article/details/89177781
#include <algorithm>
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
std::thread thrd;
void work(const std::function<void()> &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
注意,捕获引用时,注意引用对象的作用域。