future.qbk 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
[/
          Copyright Oliver Kowalke 2013.
 Distributed under the Boost Software License, Version 1.0.
    (See accompanying file LICENSE_1_0.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt
]

[section:future Future]

A future provides a mechanism to access the result of an asynchronous operation.

[#shared_state]
[heading shared state]

Behind a [template_link promise] and its [template_link future] lies an
unspecified object called their ['shared state]. The shared state is what will
actually hold the async result (or the exception).

The shared state is instantiated along with the [template_link promise].

Aside from its originating `promise<>`, a [template_link future] holds a
unique reference to a particular shared state. However, multiple
[template_link shared_future] instances can reference the same underlying
shared state.

As [template_link packaged_task] and [ns_function_link fibers..async] are
implemented using [template_link promise], discussions of shared state apply
to them as well.

[#class_future_status]
[heading Enumeration `future_status`]

Timed wait-operations (__wait_for__ and __wait_until__) return the state of the future.

        enum class future_status {
            ready,
            timeout,
            deferred  // not supported yet
        };

[heading `ready`]
[variablelist
[[Effects:] [The [link shared_state shared state] is ready.]]
]

[heading `timeout`]
[variablelist
[[Effects:] [The [link shared_state shared state] did not become ready before timeout has passed.]]
]

[note Deferred futures are not supported.]


[template_heading future]

A __future__ contains a [link shared_state shared state] which is not shared with any other future.

        #include <boost/fiber/future/future.hpp>

        namespace boost {
        namespace fibers {

        template< typename R >
        class future {
        public:
            future() noexcept;

            future( future const& other) = delete;

            future & operator=( future const& other) = delete;

            future( future && other) noexcept;

            future & operator=( future && other) noexcept;

            ~future();

            bool valid() const noexcept;

            shared_future< R > share();

            R get();    // member only of generic future template
            R & get();  // member only of future< R & > template specialization
            void get(); // member only of future< void > template specialization

            std::exception_ptr get_exception_ptr();

            void wait() const;

            template< class Rep, class Period >
            future_status wait_for(
                std::chrono::duration< Rep, Period > const& timeout_duration) const;

            template< typename Clock, typename Duration >
            future_status wait_until(
                std::chrono::time_point< Clock, Duration > const& timeout_time) const;
        };

        }}

[heading Default constructor]

        future() noexcept;

[template future_ctor_variablelist[xfuture]
[variablelist
[[Effects:] [Creates a [xfuture] with no [link shared_state shared state].
After construction `false == valid()`.]]
[[Throws:] [Nothing.]]
]
]
[future_ctor_variablelist future]

[heading Move constructor]

        future( future && other) noexcept;

[template future_move_copy_ctor_variablelist[xfuture post_valid]
[variablelist
[[Effects:] [Constructs a [xfuture] with the [link shared_state shared state] of other.
After construction [post_valid].]]
[[Throws:] [Nothing.]]
]
]
[future_move_copy_ctor_variablelist future..`false == other.valid()`]

[heading Destructor]

        ~future();

[template future_dtor_variablelist[xfuture end]
[variablelist
[[Effects:] [Destroys the [xfuture]; ownership is abandoned[end]]]
[[Note:] [[`~[xfuture]()] does ['not] block the calling fiber.]]
]
]
[future_dtor_variablelist future .]

Consider a sequence such as:

# instantiate [template_link promise]
# obtain its `future<>` via [member_link promise..get_future]
# launch [class_link fiber], capturing `promise<>`
# destroy `future<>`
# call [member_link promise..set_value]

The final `set_value()` call succeeds, but the value is silently discarded: no
additional `future<>` can be obtained from that `promise<>`.

[operator_heading future..operator_assign..operator=]

        future & operator=( future && other) noexcept;

[variablelist
[[Effects:] [Moves the [link shared_state shared state] of other to `this`.
After the assignment, `false == other.valid()`.]]
[[Throws:] [Nothing.]]
]

[template future_valid[xfuture]
[member_heading [xfuture]..valid]

        bool valid() const noexcept;

[variablelist
[[Effects:] [Returns `true` if [xfuture] contains a [link shared_state shared state].]]
[[Throws:] [Nothing.]]
]
]
[future_valid future]

[member_heading future..share]

        shared_future< R > share();

[variablelist
[[Effects:] [Move the state to a __shared_future__.]]
[[Returns:] [a __shared_future__ containing the [link shared_state shared
state] formerly belonging to `*this`.]]
[[Postcondition:] [`false == valid()`]]
[[Throws:] [__future_error__ with error condition __no_state__.]]
]

[template future_method_wait[end] Waits until [member_link promise..set_value] or
[member_link promise..set_exception] is called[end]]

[member_heading future..get]

        R get();    // member only of generic future template
        R & get();  // member only of future< R & > template specialization
        void get(); // member only of future< void > template specialization

[template future_get_variablelist[]
[variablelist
[[Precondition:] [`true == valid()`]]
[[Returns:] [[future_method_wait .] If [member_link promise..set_value] is
called, returns the value. If [member_link promise..set_exception] is called,
throws the indicated exception.]]
[[Postcondition:] [`false == valid()`]]
[[Throws:] [__future_error__ with error condition __no_state__,
__broken_promise__. Any exception passed to
`promise::set_exception()`.]]
]
]
[future_get_variablelist]

[template future_get_exception_ptr[xfuture]
[member_heading [xfuture]..get_exception_ptr]

        std::exception_ptr get_exception_ptr();

[variablelist
[[Precondition:] [`true == valid()`]]
[[Returns:] [[future_method_wait .] If `set_value()` is called, returns a
default-constructed `std::exception_ptr`. If `set_exception()` is called,
returns the passed `std::exception_ptr`.]]
[[Throws:] [__future_error__ with error condition __no_state__.]]
[[Note:] [ `get_exception_ptr()` does ['not] invalidate the [`[xfuture]].
After calling `get_exception_ptr()`, you may still call [member_link
[xfuture]..get].]]
]
]
[future_get_exception_ptr future]

[template future_wait_etc[xfuture]
[member_heading [xfuture]..wait]

        void wait();

[variablelist
[[Effects:] [[future_method_wait .]]]
[[Throws:] [__future_error__ with error condition __no_state__.]]
]

[template_member_heading [xfuture]..wait_for]

        template< class Rep, class Period >
        future_status wait_for( std::chrono::duration< Rep, Period > const& timeout_duration) const;

[variablelist
[[Effects:] [[future_method_wait , or `timeout_duration` has passed.]]]
[[Result:] [A `future_status` is returned indicating the reason for returning.]]
[[Throws:] [__future_error__ with error condition __no_state__ or
timeout-related exceptions.]]
]

[template_member_heading [xfuture]..wait_until]

        template< typename Clock, typename Duration >
        future_status wait_until( std::chrono::time_point< Clock, Duration > const& timeout_time) const;

[variablelist
[[Effects:] [[future_method_wait , or `timeout_time` has passed.]]]
[[Result:] [A `future_status` is returned indicating the reason for returning.]]
[[Throws:] [__future_error__ with error condition __no_state__ or
timeout-related exceptions.]]
]
]
[future_wait_etc future]


[template_heading shared_future]

A __shared_future__ contains a [link shared_state shared state] which might be
shared with other __shared_future__ instances.

        #include <boost/fiber/future/future.hpp>

        namespace boost {
        namespace fibers {

        template< typename R >
        class shared_future {
        public:
            shared_future() noexcept;

            ~shared_future();

            shared_future( shared_future const& other);

            shared_future( future< R > && other) noexcept;

            shared_future( shared_future && other) noexcept;

            shared_future & operator=( shared_future && other) noexcept;

            shared_future & operator=( future< R > && other) noexcept;

            shared_future & operator=( shared_future const& other) noexcept;

            bool valid() const noexcept;

            R const& get(); // member only of generic shared_future template
            R & get();      // member only of shared_future< R & > template specialization
            void get();     // member only of shared_future< void > template specialization

            std::exception_ptr get_exception_ptr();

            void wait() const;

            template< class Rep, class Period >
            future_status wait_for(
                std::chrono::duration< Rep, Period > const& timeout_duration) const;

            template< typename Clock, typename Duration >
            future_status wait_until(
                std::chrono::time_point< Clock, Duration > const& timeout_time) const;
        };

        }}

[heading Default constructor]

        shared_future();

[future_ctor_variablelist shared_future]

[heading Move constructor]

        shared_future( future< R > && other) noexcept;
        shared_future( shared_future && other) noexcept;

[future_move_copy_ctor_variablelist shared_future..`false == other.valid()`]

[heading Copy constructor]

        shared_future( shared_future const& other) noexcept;

[future_move_copy_ctor_variablelist shared_future..`other.valid()` is unchanged]

[heading Destructor]

        ~shared_future();

[future_dtor_variablelist shared_future.. if not shared.]

[operator_heading shared_future..operator_assign..operator=]

        shared_future & operator=( future< R > && other) noexcept;
        shared_future & operator=( shared_future && other) noexcept;
        shared_future & operator=( shared_future const& other) noexcept;

[variablelist
[[Effects:] [Moves or copies the [link shared_state shared state] of other to
`this`. After the assignment, the state of `other.valid()` depends on which
overload was invoked: unchanged for the overload accepting `shared_future
const&`, otherwise `false`.]]
[[Throws:] [Nothing.]]
]

[future_valid shared_future]

[member_heading shared_future..get]

        R const& get(); // member only of generic shared_future template
        R & get();      // member only of shared_future< R & > template specialization
        void get();     // member only of shared_future< void > template specialization

[future_get_variablelist]

[future_get_exception_ptr shared_future]

[future_wait_etc shared_future]

[ns_function_heading fibers..async]

        #include <boost/fiber/future/async.hpp>

        namespace boost {
        namespace fibers {

        template< class Function, class ... Args >
        future<
            std::result_of_t<
                std::decay_t< Function >( std::decay_t< Args > ... )
            >
        >
        async( Function && fn, Args && ... args);

        template< class Function, class ... Args >
        future<
            std::result_of_t<
                std::decay_t< Function >( std::decay_t< Args > ... )
            >
        >
        async( ``[link class_launch `launch`]`` policy, Function && fn, Args && ... args);

        template< typename __StackAllocator__, class Function, class ... Args >
        future<
            std::result_of_t<
                std::decay_t< Function >( std::decay_t< Args > ... )
            >
        >
        async( ``[link class_launch `launch`]`` policy, __allocator_arg_t__, __StackAllocator__ salloc,
               Function && fn, Args && ... args);

        template< typename __StackAllocator__, typename Allocator, class Function, class ... Args >
        future<
            std::result_of_t<
                std::decay_t< Function >( std::decay_t< Args > ... )
            >
        >
        async( ``[link class_launch `launch`]`` policy, __allocator_arg_t__, __StackAllocator__ salloc,
               Allocator alloc, Function && fn, Args && ... args);

        }}

[variablelist
[[Effects:] [Executes `fn` in a [class_link fiber] and returns an associated
[template_link future].]]
[[Result:] [``future<
    std::result_of_t<
        std::decay_t< Function >( std::decay_t< Args > ... )
    >
>`` representing the [link
shared_state shared state] associated with the asynchronous execution of
`fn`.]]
[[Throws:] [__fiber_error__ or __future_error__ if an error occurs.]]
[[Notes:] [The overloads accepting __allocator_arg_t__ use the passed
__StackAllocator__ when constructing the launched `fiber`. The overloads
accepting [class_link launch] use the passed `launch` when
constructing the launched `fiber`. The default `launch` is `post`, as
for the `fiber` constructor.]]
]

[note Deferred futures are not supported.]

[endsect]