C++ - Boost.Promise、Boost.Unique_Future 和移动语义
我正在关注 Bartosz Milewski 此处 的一些教程,我发现这些教程非常有用。 然而,由于作者使用了 C++11 线程标准的 just::thread 实现(其中我还没有),我决定暂时使用 boost 线程,因为本教程的作者说这样做很简单。该系列的前三个教程似乎就是这种情况,但我在第四个教程中遇到了一些问题。这是我的代码:
#include <iostream>
#include <cassert>
#include <boost\thread.hpp>
#include <boost\thread\future.hpp>
void thFun(boost::promise<std::string> & prms)
{
std::string str("Hi from future!");
prms.set_value(str);
}
int main()
{
boost::promise<std::string> prms;
boost::unique_future<std::string> fut = prms.get_future();
boost::thread th(&thFun, std::move(prms)); // error C2248: 'boost::promise<R>::promise' : cannot access private member declared in class 'boost::promise<R>'
std::cout << "Hi from main!";
std::string str = fut.get();
std::cout << str << std::endl;
th.join();
return 0;
}
以下行似乎提出了一个我不明白的问题:
boost::thread th(&thFun, std::move(prms));
编译器抱怨的地方:
错误 C2248:“boost::promise::promise”:无法访问私有 在“boost::promise”类中声明的成员
任何人都可以建议问题可能是什么?
提前致谢!
I'm following some tutorials by Bartosz Milewski here which I find very useful.
However, as the author uses the just::thread implementation of the C++11 threading standard (which I don't yet have), I have decided to go with boost threads for now as the author of the tutorial says its trivial to do so. This seems to be the case for the first three tutorials in the series but I bumped into some problems with the fourth. Here is my code:
#include <iostream>
#include <cassert>
#include <boost\thread.hpp>
#include <boost\thread\future.hpp>
void thFun(boost::promise<std::string> & prms)
{
std::string str("Hi from future!");
prms.set_value(str);
}
int main()
{
boost::promise<std::string> prms;
boost::unique_future<std::string> fut = prms.get_future();
boost::thread th(&thFun, std::move(prms)); // error C2248: 'boost::promise<R>::promise' : cannot access private member declared in class 'boost::promise<R>'
std::cout << "Hi from main!";
std::string str = fut.get();
std::cout << str << std::endl;
th.join();
return 0;
}
The following line seems to raise an issue that I don't understand:
boost::thread th(&thFun, std::move(prms));
where the compiler complains:
error C2248: 'boost::promise::promise' : cannot access private
member declared in class 'boost::promise'
Can anyone suggest what the problem might be?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
boost::thread
使用boost::bind
处理带有附加参数的线程函数,这要求它们是可复制的。您可以通过指针或引用传递 Promise(例如使用boost::ref
),但这要求对象的寿命比线程长。在此示例中,这是可以的,但对于分离的线程,或者比启动它的函数寿命更长的线程,这将阻止在堆栈上使用boost::promise
对象。boost::thread
usesboost::bind
to handle a thread function with additional arguments, which requires that they are copyable. You could pass the promise by pointer or reference (using e.g.boost::ref
), but that requires that the object outlives the thread. In this example it is OK, but for a detached thread, or one which outlives the function that started it, this would prevent the use ofboost::promise
objects on the stack.