如何在成员变量中实例化由 std::async 创建的 future?
当我尝试下面将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论