Why can I call boost::unique_future::get many times, unlike std::future?

送分小仙女□ 提交于 2019-12-06 14:17:48

From the documentation it's clear that there are at least 3 macro that are playing a significant role in what you are using

BOOST_THREAD_VERSION

which sets the version of the library, even if there are breaking changes between 4 and 3, you don't seem to have a problem with that.

second macro is BOOST_THREAD_PROVIDES_FUTURE, this macro is a switch, no value attached, if defined it enables the "standard" futures, not the unique_futures, what you are calling unique_future is just a placeholder for something that is defined by this macro at compile time.

from the file boost/thread/future.hpp

#if defined BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_FUTURE future
#else
#define BOOST_THREAD_FUTURE unique_future
#endif

in the file boost/thread/detail/config.hpp you also have BOOST_THREAD_DONT_PROVIDE_FUTURE which is another switch for default actions

// PROVIDE_FUTURE
#if ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE \
 && ! defined BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE
#endif

note the negation !, which means that if you define BOOST_THREAD_DONT_PROVIDE_FUTURE you should get "real" unique_future as documented by the library.

You are basically getting the default behaviour and the fact that you are using the version 4 of the library doesn't matter that much.

A side note about C++11 and beyond:

std::async([]{ return 42; });

this is bad practice in C++11 and it's undefined behaviour in C++14, you should always specify a launch policy for your async.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!