如何在成员变量中实例化由 std::async 创建的 future?

发布于 2025-01-15 23:51:03 字数 2453 浏览 2 评论 0原文

当我尝试下面将 std::async 返回的 future 添加到成员变量中时,我做错了什么?

class PrinterDriver
{
    std::vector<std::future<void>> m_PendingFutures;

    public:

    int DriverThread() 
    {
        // Do something
    }
    
    void Start() 
    {
        m_PendingFutures.emplace_back(
           std::async(std::launch::async, 
               &PrinterDriver::DriverThread, this));
    }
}

我不明白这个错误,也不知道如何修复它:

error C2664: 'std::future::future(std::future &&) noexcept': 无法转换参数 1 从 '_Ty' 到 'std::future &&'

下面给出了作为我想使用它的上下文的完整代码:

#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <future>
#include <thread>

class PrinterDriver
{
    std::mutex m_stop_driver_mutex;
    std::condition_variable_any m_StopDriverEvent;
    std::vector<std::future<void>> m_PendingFutures;

public:
    ~PrinterDriver() = default;

    int DriverThread()
    {
        printf("Printer driver started\n");
        auto quit = false;
        while (!quit)
        {
            // Wait 200 ms except if we are signaled to stop
            {
                using namespace std::chrono_literals;
                std::unique_lock<std::mutex> lock(m_stop_driver_mutex);
                const auto cv_status = m_StopDriverEvent.wait_for(lock, 200ms);
                quit = (cv_status == std::cv_status::no_timeout);
            }
        }
        printf("Printer driver stopped\n");
        return 0;
    }

    void Start()
    {
        // How to instantiate a future created by std::async in a member variable ?
        // Error C2664: 'std::future<void>::future(std::future<void> &&) noexcept':
        // cannot convert argument 1 from '_Ty' to 'std::future<void> &&'
        m_PendingFutures.emplace_back(std::async(std::launch::async, &PrinterDriver::DriverThread, this));
    }

    void Stop()
    {
        std::unique_lock<std::mutex> lock(m_stop_driver_mutex);
        m_StopDriverEvent.notify_all();
        for (auto&& future_handle : m_PendingFutures)
        {
            future_handle.get();
        }
        m_PendingFutures.clear();
    }
};

int main()
{
    PrinterDriver driver;
    driver.Start();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    driver.Stop();
    return 0;
}

What do I do wrong when I try below to add the future returned by std::async into a member variable?

class PrinterDriver
{
    std::vector<std::future<void>> m_PendingFutures;

    public:

    int DriverThread() 
    {
        // Do something
    }
    
    void Start() 
    {
        m_PendingFutures.emplace_back(
           std::async(std::launch::async, 
               &PrinterDriver::DriverThread, this));
    }
}

I don't understand the error nor can't find-out how to fix it:

error C2664: 'std::future::future(std::future &&) noexcept': cannot convert argument 1
from '_Ty' to 'std::future &&'

Below the complete code given as a context in which I want to use it:

#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <future>
#include <thread>

class PrinterDriver
{
    std::mutex m_stop_driver_mutex;
    std::condition_variable_any m_StopDriverEvent;
    std::vector<std::future<void>> m_PendingFutures;

public:
    ~PrinterDriver() = default;

    int DriverThread()
    {
        printf("Printer driver started\n");
        auto quit = false;
        while (!quit)
        {
            // Wait 200 ms except if we are signaled to stop
            {
                using namespace std::chrono_literals;
                std::unique_lock<std::mutex> lock(m_stop_driver_mutex);
                const auto cv_status = m_StopDriverEvent.wait_for(lock, 200ms);
                quit = (cv_status == std::cv_status::no_timeout);
            }
        }
        printf("Printer driver stopped\n");
        return 0;
    }

    void Start()
    {
        // How to instantiate a future created by std::async in a member variable ?
        // Error C2664: 'std::future<void>::future(std::future<void> &&) noexcept':
        // cannot convert argument 1 from '_Ty' to 'std::future<void> &&'
        m_PendingFutures.emplace_back(std::async(std::launch::async, &PrinterDriver::DriverThread, this));
    }

    void Stop()
    {
        std::unique_lock<std::mutex> lock(m_stop_driver_mutex);
        m_StopDriverEvent.notify_all();
        for (auto&& future_handle : m_PendingFutures)
        {
            future_handle.get();
        }
        m_PendingFutures.clear();
    }
};

int main()
{
    PrinterDriver driver;
    driver.Start();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    driver.Stop();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文