mirror of
https://github.com/boostorg/fiber.git
synced 2026-02-20 14:42:21 +00:00
91 lines
2.3 KiB
Plaintext
91 lines
2.3 KiB
Plaintext
[/ Copyright Oliver Kowalke 2009.
|
|
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:async_execution Asynchronous Execution]
|
|
|
|
[heading Synopsis]
|
|
|
|
In order to execute a task it has to be passed to function __fn_async__. The task is store inside
|
|
the __ep__ (may be given as an additional argument to __fn_async__) and will by means of __ep__.
|
|
|
|
|
|
[section:async Non-member function `async()`]
|
|
|
|
Function __fn_async__ applies the moved __task__ to the __ep__ which executes the __task__ (for this purpose __ep__ is
|
|
required to provide `handle< R > operator()( task< R > && t)`).
|
|
The function returns a __handle__ which controls the submitted __task__.
|
|
|
|
long fibonacci( long n)
|
|
{
|
|
if ( n == 0) return 0;
|
|
if ( n == 1) return 1;
|
|
long k1( 1), k2( 0);
|
|
for ( int i( 2); i <= n; ++i)
|
|
{
|
|
long tmp( k1);
|
|
k1 = k1 + k2;
|
|
k2 = tmp;
|
|
}
|
|
return k1;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
// task computing fibonacci(10)
|
|
// move the task to executor
|
|
boost::tasks::handle< long > h1(
|
|
boost::tasks::async(
|
|
boost::tasks::make_task( fibonacci, 10) ) );
|
|
|
|
// task computing fibonacci(5)
|
|
boost::task< long > t( fibonacci, 5);
|
|
// move the task to executor
|
|
boost::tasks::handle< long > h2(
|
|
boost::tasks::async(
|
|
boost::move( t) ) );
|
|
|
|
// access the results
|
|
std::cout << "fibonacci(10) == " << h1.get() << std::endl;
|
|
std::cout << "fibonacci(5) == " << h2.get() << std::endl;
|
|
}
|
|
|
|
|
|
[section:async Non-member function `async()`]
|
|
|
|
#include <boost/task/async.hpp>
|
|
|
|
template< typename R >
|
|
handle< R > async( task< R > t);
|
|
|
|
template< typename R >
|
|
handle< R > async( task< R > t, own_thread ep);
|
|
|
|
template< typename R >
|
|
handle< R > async( task< R > t, new_thread ep);
|
|
|
|
template< typename R, typename Channel >
|
|
handle< R > async( task< R > t, pool< Channel > & ep);
|
|
|
|
template< typename R, typename Channel, typename Attr >
|
|
handle< R > async( task< R > t, Attr attr, pool< Channel > & ep);
|
|
|
|
template< typename R, typename Strategy >
|
|
handle< R > async( task< R > t, tasklets::scheduler< Strategy > & ep);
|
|
|
|
[variablelist
|
|
[[Effects:] [moves task to an asyncrounous executer and returns a handle associated with the task]]
|
|
[[Throws:] [`boost::thread_resource_error`]]
|
|
]
|
|
[endsect]
|
|
|
|
[endsect]
|
|
|
|
[include async_completion_token.qbk]
|
|
[include execution_policies.qbk]
|
|
|
|
[endsect]
|