future.qbk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. [/
  2. Copyright Oliver Kowalke 2013.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. ]
  7. [section:future Future]
  8. A future provides a mechanism to access the result of an asynchronous operation.
  9. [#shared_state]
  10. [heading shared state]
  11. Behind a [template_link promise] and its [template_link future] lies an
  12. unspecified object called their ['shared state]. The shared state is what will
  13. actually hold the async result (or the exception).
  14. The shared state is instantiated along with the [template_link promise].
  15. Aside from its originating `promise<>`, a [template_link future] holds a
  16. unique reference to a particular shared state. However, multiple
  17. [template_link shared_future] instances can reference the same underlying
  18. shared state.
  19. As [template_link packaged_task] and [ns_function_link fibers..async] are
  20. implemented using [template_link promise], discussions of shared state apply
  21. to them as well.
  22. [#class_future_status]
  23. [heading Enumeration `future_status`]
  24. Timed wait-operations (__wait_for__ and __wait_until__) return the state of the future.
  25. enum class future_status {
  26. ready,
  27. timeout,
  28. deferred // not supported yet
  29. };
  30. [heading `ready`]
  31. [variablelist
  32. [[Effects:] [The [link shared_state shared state] is ready.]]
  33. ]
  34. [heading `timeout`]
  35. [variablelist
  36. [[Effects:] [The [link shared_state shared state] did not become ready before timeout has passed.]]
  37. ]
  38. [note Deferred futures are not supported.]
  39. [template_heading future]
  40. A __future__ contains a [link shared_state shared state] which is not shared with any other future.
  41. #include <boost/fiber/future/future.hpp>
  42. namespace boost {
  43. namespace fibers {
  44. template< typename R >
  45. class future {
  46. public:
  47. future() noexcept;
  48. future( future const& other) = delete;
  49. future & operator=( future const& other) = delete;
  50. future( future && other) noexcept;
  51. future & operator=( future && other) noexcept;
  52. ~future();
  53. bool valid() const noexcept;
  54. shared_future< R > share();
  55. R get(); // member only of generic future template
  56. R & get(); // member only of future< R & > template specialization
  57. void get(); // member only of future< void > template specialization
  58. std::exception_ptr get_exception_ptr();
  59. void wait() const;
  60. template< class Rep, class Period >
  61. future_status wait_for(
  62. std::chrono::duration< Rep, Period > const& timeout_duration) const;
  63. template< typename Clock, typename Duration >
  64. future_status wait_until(
  65. std::chrono::time_point< Clock, Duration > const& timeout_time) const;
  66. };
  67. }}
  68. [heading Default constructor]
  69. future() noexcept;
  70. [template future_ctor_variablelist[xfuture]
  71. [variablelist
  72. [[Effects:] [Creates a [xfuture] with no [link shared_state shared state].
  73. After construction `false == valid()`.]]
  74. [[Throws:] [Nothing.]]
  75. ]
  76. ]
  77. [future_ctor_variablelist future]
  78. [heading Move constructor]
  79. future( future && other) noexcept;
  80. [template future_move_copy_ctor_variablelist[xfuture post_valid]
  81. [variablelist
  82. [[Effects:] [Constructs a [xfuture] with the [link shared_state shared state] of other.
  83. After construction [post_valid].]]
  84. [[Throws:] [Nothing.]]
  85. ]
  86. ]
  87. [future_move_copy_ctor_variablelist future..`false == other.valid()`]
  88. [heading Destructor]
  89. ~future();
  90. [template future_dtor_variablelist[xfuture end]
  91. [variablelist
  92. [[Effects:] [Destroys the [xfuture]; ownership is abandoned[end]]]
  93. [[Note:] [[`~[xfuture]()] does ['not] block the calling fiber.]]
  94. ]
  95. ]
  96. [future_dtor_variablelist future .]
  97. Consider a sequence such as:
  98. # instantiate [template_link promise]
  99. # obtain its `future<>` via [member_link promise..get_future]
  100. # launch [class_link fiber], capturing `promise<>`
  101. # destroy `future<>`
  102. # call [member_link promise..set_value]
  103. The final `set_value()` call succeeds, but the value is silently discarded: no
  104. additional `future<>` can be obtained from that `promise<>`.
  105. [operator_heading future..operator_assign..operator=]
  106. future & operator=( future && other) noexcept;
  107. [variablelist
  108. [[Effects:] [Moves the [link shared_state shared state] of other to `this`.
  109. After the assignment, `false == other.valid()`.]]
  110. [[Throws:] [Nothing.]]
  111. ]
  112. [template future_valid[xfuture]
  113. [member_heading [xfuture]..valid]
  114. bool valid() const noexcept;
  115. [variablelist
  116. [[Effects:] [Returns `true` if [xfuture] contains a [link shared_state shared state].]]
  117. [[Throws:] [Nothing.]]
  118. ]
  119. ]
  120. [future_valid future]
  121. [member_heading future..share]
  122. shared_future< R > share();
  123. [variablelist
  124. [[Effects:] [Move the state to a __shared_future__.]]
  125. [[Returns:] [a __shared_future__ containing the [link shared_state shared
  126. state] formerly belonging to `*this`.]]
  127. [[Postcondition:] [`false == valid()`]]
  128. [[Throws:] [__future_error__ with error condition __no_state__.]]
  129. ]
  130. [template future_method_wait[end] Waits until [member_link promise..set_value] or
  131. [member_link promise..set_exception] is called[end]]
  132. [member_heading future..get]
  133. R get(); // member only of generic future template
  134. R & get(); // member only of future< R & > template specialization
  135. void get(); // member only of future< void > template specialization
  136. [template future_get_variablelist[]
  137. [variablelist
  138. [[Precondition:] [`true == valid()`]]
  139. [[Returns:] [[future_method_wait .] If [member_link promise..set_value] is
  140. called, returns the value. If [member_link promise..set_exception] is called,
  141. throws the indicated exception.]]
  142. [[Postcondition:] [`false == valid()`]]
  143. [[Throws:] [__future_error__ with error condition __no_state__,
  144. __broken_promise__. Any exception passed to
  145. `promise::set_exception()`.]]
  146. ]
  147. ]
  148. [future_get_variablelist]
  149. [template future_get_exception_ptr[xfuture]
  150. [member_heading [xfuture]..get_exception_ptr]
  151. std::exception_ptr get_exception_ptr();
  152. [variablelist
  153. [[Precondition:] [`true == valid()`]]
  154. [[Returns:] [[future_method_wait .] If `set_value()` is called, returns a
  155. default-constructed `std::exception_ptr`. If `set_exception()` is called,
  156. returns the passed `std::exception_ptr`.]]
  157. [[Throws:] [__future_error__ with error condition __no_state__.]]
  158. [[Note:] [ `get_exception_ptr()` does ['not] invalidate the [`[xfuture]].
  159. After calling `get_exception_ptr()`, you may still call [member_link
  160. [xfuture]..get].]]
  161. ]
  162. ]
  163. [future_get_exception_ptr future]
  164. [template future_wait_etc[xfuture]
  165. [member_heading [xfuture]..wait]
  166. void wait();
  167. [variablelist
  168. [[Effects:] [[future_method_wait .]]]
  169. [[Throws:] [__future_error__ with error condition __no_state__.]]
  170. ]
  171. [template_member_heading [xfuture]..wait_for]
  172. template< class Rep, class Period >
  173. future_status wait_for( std::chrono::duration< Rep, Period > const& timeout_duration) const;
  174. [variablelist
  175. [[Effects:] [[future_method_wait , or `timeout_duration` has passed.]]]
  176. [[Result:] [A `future_status` is returned indicating the reason for returning.]]
  177. [[Throws:] [__future_error__ with error condition __no_state__ or
  178. timeout-related exceptions.]]
  179. ]
  180. [template_member_heading [xfuture]..wait_until]
  181. template< typename Clock, typename Duration >
  182. future_status wait_until( std::chrono::time_point< Clock, Duration > const& timeout_time) const;
  183. [variablelist
  184. [[Effects:] [[future_method_wait , or `timeout_time` has passed.]]]
  185. [[Result:] [A `future_status` is returned indicating the reason for returning.]]
  186. [[Throws:] [__future_error__ with error condition __no_state__ or
  187. timeout-related exceptions.]]
  188. ]
  189. ]
  190. [future_wait_etc future]
  191. [template_heading shared_future]
  192. A __shared_future__ contains a [link shared_state shared state] which might be
  193. shared with other __shared_future__ instances.
  194. #include <boost/fiber/future/future.hpp>
  195. namespace boost {
  196. namespace fibers {
  197. template< typename R >
  198. class shared_future {
  199. public:
  200. shared_future() noexcept;
  201. ~shared_future();
  202. shared_future( shared_future const& other);
  203. shared_future( future< R > && other) noexcept;
  204. shared_future( shared_future && other) noexcept;
  205. shared_future & operator=( shared_future && other) noexcept;
  206. shared_future & operator=( future< R > && other) noexcept;
  207. shared_future & operator=( shared_future const& other) noexcept;
  208. bool valid() const noexcept;
  209. R const& get(); // member only of generic shared_future template
  210. R & get(); // member only of shared_future< R & > template specialization
  211. void get(); // member only of shared_future< void > template specialization
  212. std::exception_ptr get_exception_ptr();
  213. void wait() const;
  214. template< class Rep, class Period >
  215. future_status wait_for(
  216. std::chrono::duration< Rep, Period > const& timeout_duration) const;
  217. template< typename Clock, typename Duration >
  218. future_status wait_until(
  219. std::chrono::time_point< Clock, Duration > const& timeout_time) const;
  220. };
  221. }}
  222. [heading Default constructor]
  223. shared_future();
  224. [future_ctor_variablelist shared_future]
  225. [heading Move constructor]
  226. shared_future( future< R > && other) noexcept;
  227. shared_future( shared_future && other) noexcept;
  228. [future_move_copy_ctor_variablelist shared_future..`false == other.valid()`]
  229. [heading Copy constructor]
  230. shared_future( shared_future const& other) noexcept;
  231. [future_move_copy_ctor_variablelist shared_future..`other.valid()` is unchanged]
  232. [heading Destructor]
  233. ~shared_future();
  234. [future_dtor_variablelist shared_future.. if not shared.]
  235. [operator_heading shared_future..operator_assign..operator=]
  236. shared_future & operator=( future< R > && other) noexcept;
  237. shared_future & operator=( shared_future && other) noexcept;
  238. shared_future & operator=( shared_future const& other) noexcept;
  239. [variablelist
  240. [[Effects:] [Moves or copies the [link shared_state shared state] of other to
  241. `this`. After the assignment, the state of `other.valid()` depends on which
  242. overload was invoked: unchanged for the overload accepting `shared_future
  243. const&`, otherwise `false`.]]
  244. [[Throws:] [Nothing.]]
  245. ]
  246. [future_valid shared_future]
  247. [member_heading shared_future..get]
  248. R const& get(); // member only of generic shared_future template
  249. R & get(); // member only of shared_future< R & > template specialization
  250. void get(); // member only of shared_future< void > template specialization
  251. [future_get_variablelist]
  252. [future_get_exception_ptr shared_future]
  253. [future_wait_etc shared_future]
  254. [ns_function_heading fibers..async]
  255. #include <boost/fiber/future/async.hpp>
  256. namespace boost {
  257. namespace fibers {
  258. template< class Function, class ... Args >
  259. future<
  260. std::result_of_t<
  261. std::decay_t< Function >( std::decay_t< Args > ... )
  262. >
  263. >
  264. async( Function && fn, Args && ... args);
  265. template< class Function, class ... Args >
  266. future<
  267. std::result_of_t<
  268. std::decay_t< Function >( std::decay_t< Args > ... )
  269. >
  270. >
  271. async( ``[link class_launch `launch`]`` policy, Function && fn, Args && ... args);
  272. template< typename __StackAllocator__, class Function, class ... Args >
  273. future<
  274. std::result_of_t<
  275. std::decay_t< Function >( std::decay_t< Args > ... )
  276. >
  277. >
  278. async( ``[link class_launch `launch`]`` policy, __allocator_arg_t__, __StackAllocator__ salloc,
  279. Function && fn, Args && ... args);
  280. template< typename __StackAllocator__, typename Allocator, class Function, class ... Args >
  281. future<
  282. std::result_of_t<
  283. std::decay_t< Function >( std::decay_t< Args > ... )
  284. >
  285. >
  286. async( ``[link class_launch `launch`]`` policy, __allocator_arg_t__, __StackAllocator__ salloc,
  287. Allocator alloc, Function && fn, Args && ... args);
  288. }}
  289. [variablelist
  290. [[Effects:] [Executes `fn` in a [class_link fiber] and returns an associated
  291. [template_link future].]]
  292. [[Result:] [``future<
  293. std::result_of_t<
  294. std::decay_t< Function >( std::decay_t< Args > ... )
  295. >
  296. >`` representing the [link
  297. shared_state shared state] associated with the asynchronous execution of
  298. `fn`.]]
  299. [[Throws:] [__fiber_error__ or __future_error__ if an error occurs.]]
  300. [[Notes:] [The overloads accepting __allocator_arg_t__ use the passed
  301. __StackAllocator__ when constructing the launched `fiber`. The overloads
  302. accepting [class_link launch] use the passed `launch` when
  303. constructing the launched `fiber`. The default `launch` is `post`, as
  304. for the `fiber` constructor.]]
  305. ]
  306. [note Deferred futures are not supported.]
  307. [endsect]