如何将数据传递给将在不同线程中运行的C++ 0x lambda函数?

Dim*_* C. 5 c++ lambda c++11

重要更新:显然我在提出这个问题时得出了错误的结论.感谢我发现的响应,lambda函数[=]()在多线程场景中运行良好.我为提出这个令人困惑的问题而道歉.请投票结束,因为这不是问题.


在我们公司,我们编写了一个库函数,在一个单独的线程中异步调用一个函数.它使用继承和模板魔法的组合工作.客户端代码如下所示:

DemoThread thread;
std::string stringToPassByValue = "The string to pass by value";
AsyncCall(thread, &DemoThread::SomeFunction, stringToPassByValue);
Run Code Online (Sandbox Code Playgroud)

自从引入lambda函数以来,我想将它与lambda函数结合使用.我想写下面的客户端代码:

DemoThread thread;
std::string stringToPassByValue = "The string to pass by value";
AsyncCall(thread, [=]()
{
    const std::string someCopy = stringToPassByValue;
});
Run Code Online (Sandbox Code Playgroud)

更新:与我在提出这个问题时首先相信的情况相反,此代码运行正常.

现在,使用Visual C++ 2010,此代码不起作用.会发生什么stringToPassByValue是不复制.相反,"按值捕获"功能通过引用传递数据.结果是,如果函数在stringToPassByValue超出范围后执行,则应用程序崩溃,因为它的析构函数已经被调用.

所以我想知道:是否可以将数据作为副本传递给lambda函数

注意:一种可能的解决方案是修改我们的框架以传递lambda参数声明列表中的数据,如下所示:

DemoThread thread;
std::string stringToPassByValue = "The string to pass by value";
AsyncCall(thread, [=](const std::string stringPassedByValue)
{
    const std::string someCopy = stringPassedByValue;
}
, stringToPassByValue);
Run Code Online (Sandbox Code Playgroud)

但是,这个解决方案非常冗长,我们的原始函数指针解决方案既简短又易于阅读.

更新:完整的实施AsyncCall太大了,无法在此发布.简而言之,发生的是AsyncCall模板函数实例化一个包含lambda函数的模板类.此类派生自包含虚Execute()函数的基类,并在AsyncCall()调用时将函数调用类放在调用队列中.然后,另一个线程通过调用虚Execute()函数来执行排队调用,该虚函数被多态地分派给模板类,然后模板类执行lambda函数.