C++ - Boost.Promise、Boost.Unique_Future 和移动语义

发布于 2024-12-25 18:20:18 字数 1334 浏览 3 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-01-01 18:20:18

boost::thread 使用 boost::bind 处理带有附加参数的线程函数,这要求它们是可复制的。您可以通过指针或引用传递 Promise(例如使用 boost::ref),但这要求对象的寿命比线程长。在此示例中,这是可以的,但对于分离的线程,或者比启动它的函数寿命更长的线程,这将阻止在堆栈上使用 boost::promise 对象。

boost::thread uses boost::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 of boost::promise objects on the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文