我有一个函数返回对std :: promise的引用:
std::shared_ptr<std::promise<void>> play();
Run Code Online (Sandbox Code Playgroud)
(更多信息:该功能在某些设备上播放媒体,返回值表示此播放完成时.如果第二次调用播放,则会在第一次返回的承诺上设置一个值,并创建一个新的承诺,返回第二次电话)
然后调用者可以捕获值并等待未来:
auto this_future = play()->get_future();
this_future.wait();
Run Code Online (Sandbox Code Playgroud)
返回对promise的引用是否有意义,或者我应该返回未来,以便调用函数不必调用get_future()?
我有一个应用程序,需要在某些窗口内工作(在这种情况下,窗口相隔30秒).当时间不在窗口内时,计算下一个窗口中间的时间,并且线程休眠该时间量(以毫秒为单位,使用boost::this_thread::sleep_for).
使用Boost 1.55,我能够在极限可靠性的范围内(+/- 100ms)击中窗户.在迁移到Boost 1.58后,我无法打到这些窗口.替换boost::this_thread::sleep_forwith std::this_thread::sleep_for修复了问题; 但是,我需要提供的可中断功能boost::thread和中断点boost::this_thread::sleep_for.
以下是一些说明问题的示例代码:
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <chrono>
#include <iostream>
#include <thread>
void boostThreadFunction ()
{
std::cout << "Starting Boost thread" << std::endl;
for (int i = 0; i < 10; ++i)
{
auto sleep_time = boost::chrono::milliseconds {29000 + 100 * i};
auto mark = std::chrono::steady_clock::now ();
boost::this_thread::sleep_for (sleep_time);
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now () - mark);
std::cout << "Boost thread:" << std::endl;
std::cout << …Run Code Online (Sandbox Code Playgroud)