mirror of
https://github.com/boostorg/fiber.git
synced 2026-02-08 22:53:21 +00:00
110 lines
2.4 KiB
C++
110 lines
2.4 KiB
C++
|
|
// 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)
|
|
|
|
#include <cstdio>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
#include <boost/atomic.hpp>
|
|
#include <boost/thread.hpp>
|
|
#include <boost/utility.hpp>
|
|
|
|
#include <boost/fiber/all.hpp>
|
|
|
|
#define MAXCOUNT 100
|
|
|
|
#include <cstdio>
|
|
#include <sstream>
|
|
|
|
boost::atomic< bool > fini( false);
|
|
boost::fibers::round_robin * other_ds = 0;
|
|
|
|
boost::fibers::future< int > fibonacci( int);
|
|
|
|
int fibonacci_( int n)
|
|
{
|
|
boost::this_fiber::yield();
|
|
|
|
int res = 1;
|
|
|
|
// if ( 0 != n && 1 != n)
|
|
// {
|
|
// boost::fibers::future< int > f1 = fibonacci( n - 1);
|
|
// boost::fibers::future< int > f2 = fibonacci( n - 2);
|
|
//
|
|
// res = f1.get() + f2.get();
|
|
// }
|
|
|
|
return res;
|
|
}
|
|
|
|
boost::fibers::future< int > fibonacci( int n)
|
|
{
|
|
boost::fibers::packaged_task< int() > pt( boost::bind( fibonacci_, n) );
|
|
boost::fibers::future< int > f( pt.get_future() );
|
|
boost::fibers::fiber( boost::move( pt) ).detach();
|
|
return boost::move( f);
|
|
}
|
|
|
|
void create_fibers( int n)
|
|
{
|
|
int res = fibonacci( n).get();
|
|
fprintf( stderr, "fibonacci(%d) = %d", n, res);
|
|
}
|
|
|
|
void fn_create_fibers( boost::fibers::round_robin * ds, boost::barrier * b, int n)
|
|
{
|
|
boost::fibers::scheduling_algorithm( ds);
|
|
|
|
b->wait();
|
|
|
|
boost::fibers::fiber( boost::bind( create_fibers, n) ).join();
|
|
|
|
fini = true;
|
|
}
|
|
|
|
void fn_steel_fibers( boost::fibers::round_robin * other_ds, boost::barrier * b, int * count)
|
|
{
|
|
BOOST_ASSERT( other_ds);
|
|
boost::fibers::round_robin ds;
|
|
boost::fibers::scheduling_algorithm( & ds);
|
|
|
|
b->wait();
|
|
|
|
while ( ! fini)
|
|
{
|
|
boost::fibers::fiber f( other_ds->steel_from() );
|
|
if ( f)
|
|
{
|
|
++( * count);
|
|
ds.migrate_to( f);
|
|
f.detach();
|
|
}
|
|
while ( boost::fibers::detail::scheduler::instance().run() );
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
for ( int i = 0; i < MAXCOUNT; ++i) {
|
|
fini = false;
|
|
int n = 10, count = 0;
|
|
|
|
boost::fibers::round_robin * ds = new boost::fibers::round_robin();
|
|
boost::barrier b( 2);
|
|
boost::thread t1( boost::bind( fn_create_fibers, ds, &b, n) );
|
|
boost::thread t2( boost::bind( fn_steel_fibers, ds, &b, &count) );
|
|
|
|
t1.join();
|
|
t2.join();
|
|
|
|
fprintf(stderr, ", %d fibers stolen\n", count);
|
|
delete ds;
|
|
}
|
|
|
|
return 0;
|
|
}
|