| 379 | == !AsynchronousExecutors == |
| 380 | * '''Suggested by:''' Vicente J. Botet Escriba |
| 381 | * '''Description:''' Asynchronous Executors (AE) and Asynchronous Completion Tokens (ACT) in a generic way (or at least this was my intention). The library can be considered as a front-end for several Asynchronous Execution models making it possible to share common algorithms and making easier to switch from an Asynchronous Executors to another. |
| 382 | |
| 383 | Next follows the motivation for the library and how the library can manage with this concepts. |
| 384 | |
| 385 | In N1833 - Preliminary Threading Library Proposal for TR2 Kevlin Henney introduce the concept of threader and a function thread that evaluate a function asynchronously and returns a joiner handle. |
| 386 | In N2185 - Proposed Text for Parallel Task Execution Peter Dimov introduce a fork function able to evaluate a function asynchronously and returns a future handle. |
| 387 | In N2276 - Thread Pools and Futures Anthony William introduce launch_in_thread and launch_in_pool function templates which evaluate a function asynchronously either in a specific thread or a thread pool and returns a unique_future handle. |
| 388 | In Boost.ThreadPool Oliver Kowalke propose a complete implementation of a thread pool with a submit function which evaluate a function asynchronously and returns a task handle. |
| 389 | |
| 390 | Behind all these proposal there is a concept of asynchronous executor, fork-launch-like function and the asynchronous completion token handle. |
| 391 | |
| 392 | || Name || executor || fork-like || ACT || |
| 393 | || Boost.Thread || ?? || thread constructor || thread || |
| 394 | || Boost.!ThreadPool || tp::pool || submit || tp::task || |
| 395 | || N2276 || thread || launch_in_thread || unique_future<T> || |
| 396 | || N2276 || thread_pool || launch_in_pool || unique_future<T> || |
| 397 | || N2185 || ?? || fork || future<T> || |
| 398 | || N1833 || threader || thread || joiner<T> || |
| 399 | |
| 400 | The asynchronous completion token models can follows two interfaces, the thread interface and the unique_future interface. |
| 401 | Some asynchronous completion token handles allows to recover the result of the evaluation of the function (futures), other allows to manage the underlying thread of execution (thread) and some both (joiner and tp::task). |
| 402 | |
| 403 | It seems natural to make a generic fork function that will evaluate a function asynchronously with respect to the calling thread and returns an ACT handle. The following meta-function associated an ACT handle to a asynchronous executor. |
| 404 | |
| 405 | {{{ |
| 406 | #!cpp |
| 407 | template <typename AE, typename T> |
| 408 | struct asynchronous_completion_token { |
| 409 | typedef typename AE::template handle<T>::type type; |
| 410 | }; |
| 411 | }}} |
| 412 | |
| 413 | The result of forking a nullary function by an asynchronous executor is given by the following meta-function: |
| 414 | |
| 415 | {{{ |
| 416 | #!cpp |
| 417 | namespace result_of { |
| 418 | template <typename AE,typename F> |
| 419 | struct fork { |
| 420 | typedef typename asynchronous_completion_token<AE, typename result_of<F()>::type>::type type; |
| 421 | }; |
| 422 | } |
| 423 | }}} |
| 424 | |
| 425 | The default implementation of fork delegates on fork asynchronous executor function. |
| 426 | |
| 427 | {{{ |
| 428 | #!cpp |
| 429 | template< typename AE, typename F > |
| 430 | result_of::fork<AE, F>::type fork( AE& ae, F fn ) { |
| 431 | return ae.fork(fn); |
| 432 | } |
| 433 | }}} |
| 434 | |
| 435 | Forking n-ary functions relies on the nullary version and bind. |
| 436 | |
| 437 | {{{ |
| 438 | #!cpp |
| 439 | template< typename AE, typename F, typename A1, ..., typename An > |
| 440 | asynchronous_completion_token<AE, typename result_of<F(A1,..., An)>::type >::type |
| 441 | fork( AE& ae, F fn, A1 a1, ..., An an ) { |
| 442 | return ae.fork( bind( fn, a1, ..., an ) ); |
| 443 | } |
| 444 | }}} |
| 445 | |
| 446 | --------------------------------------------------------------------------------------------------- |