Unnamed: 0
int64
0
0
repo_id
stringlengths
5
186
file_path
stringlengths
15
223
content
stringlengths
1
32.8M
0
repos/fiber
repos/fiber/test/test_future_post.cpp
// (C) Copyright 2008-10 Anthony Williams // 2015 Oliver Kowalke // // 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 <chrono> #include <memory> #include <stdexcept> #include <string> #include <utility> #include <boost/test/unit_test.hpp> #include <boost/fiber/all.hpp> typedef std::chrono::milliseconds ms; typedef std::chrono::high_resolution_clock Clock; int gi = 7; struct my_exception : public std::runtime_error { my_exception() : std::runtime_error("my_exception") { } }; struct A { A() = default; A( A const&) = delete; A( A &&) = default; A & operator=( A const&) = delete; A & operator=( A &&) = default; int value; }; void fn1( boost::fibers::promise< int > * p, int i) { boost::this_fiber::yield(); p->set_value( i); } void fn2() { boost::fibers::promise< int > p; boost::fibers::future< int > f( p.get_future() ); boost::this_fiber::yield(); boost::fibers::fiber( boost::fibers::launch::post, fn1, & p, 7).detach(); boost::this_fiber::yield(); BOOST_CHECK( 7 == f.get() ); } int fn3() { return 3; } void fn4() { } int fn5() { boost::throw_exception( my_exception() ); return 3; } void fn6() { boost::throw_exception( my_exception() ); } int & fn7() { return gi; } int fn8( int i) { return i; } A fn9() { A a; a.value = 3; return a; } A fn10() { boost::throw_exception( my_exception() ); return A(); } void fn11( boost::fibers::promise< int > p) { boost::this_fiber::sleep_for( ms(500) ); p.set_value(3); } void fn12( boost::fibers::promise< int& > p) { boost::this_fiber::sleep_for( ms(500) ); gi = 5; p.set_value( gi); } void fn13( boost::fibers::promise< void > p) { boost::this_fiber::sleep_for( ms(400) ); p.set_value(); } // future void test_future_create() { // default constructed future is not valid boost::fibers::future< int > f1; BOOST_CHECK( ! f1.valid() ); // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p2; boost::fibers::future< int > f2 = p2.get_future(); BOOST_CHECK( f2.valid() ); } void test_future_create_ref() { // default constructed future is not valid boost::fibers::future< int& > f1; BOOST_CHECK( ! f1.valid() ); // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p2; boost::fibers::future< int& > f2 = p2.get_future(); BOOST_CHECK( f2.valid() ); } void test_future_create_void() { // default constructed future is not valid boost::fibers::future< void > f1; BOOST_CHECK( ! f1.valid() ); // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p2; boost::fibers::future< void > f2 = p2.get_future(); BOOST_CHECK( f2.valid() ); } void test_future_move() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::future< int > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_future_move_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::future< int& > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_future_move_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::future< void > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_future_get() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; p1.set_value( 7); boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); BOOST_CHECK( 7 == f1.get() ); BOOST_CHECK( ! f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< int > p2; f1 = p2.get_future(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( thrown); } void test_future_get_move() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< A > p1; A a; a.value = 7; p1.set_value( std::move( a) ); boost::fibers::future< A > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); BOOST_CHECK( 7 == f1.get().value); BOOST_CHECK( ! f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< A > p2; f1 = p2.get_future(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( thrown); } void test_future_get_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; int i = 7; p1.set_value( i); boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); int & j = f1.get(); BOOST_CHECK( &i == &j); BOOST_CHECK( ! f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< int& > p2; f1 = p2.get_future(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( thrown); } void test_future_get_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; p1.set_value(); boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); f1.get(); BOOST_CHECK( ! f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< void > p2; f1 = p2.get_future(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( thrown); } void test_future_share() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; int i = 7; p1.set_value( i); boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< int > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); int j = sf1.get(); BOOST_CHECK_EQUAL( i, j); BOOST_CHECK( sf1.valid() ); } void test_future_share_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; int i = 7; p1.set_value( i); boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< int& > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); int & j = sf1.get(); BOOST_CHECK( &i == &j); BOOST_CHECK( sf1.valid() ); } void test_future_share_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; p1.set_value(); boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< void > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); sf1.get(); BOOST_CHECK( sf1.valid() ); } void test_future_wait() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); // wait on future p1.set_value( 7); f1.wait(); BOOST_CHECK( 7 == f1.get() ); } void test_future_wait_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); // wait on future int i = 7; p1.set_value( i); f1.wait(); int & j = f1.get(); BOOST_CHECK( &i == &j); } void test_future_wait_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); // wait on future p1.set_value(); f1.wait(); f1.get(); BOOST_CHECK( ! f1.valid() ); } void test_future_wait_for() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn11, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_for_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn12, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_for_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn13, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_until() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn11, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_until_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn12, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_until_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); boost::fibers::fiber( boost::fibers::launch::post, fn13, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(400) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_future_wait_with_fiber_1() { boost::fibers::promise< int > p1; boost::fibers::fiber( boost::fibers::launch::post, fn1, & p1, 7).detach(); boost::fibers::future< int > f1 = p1.get_future(); // wait on future BOOST_CHECK( 7 == f1.get() ); } void test_future_wait_with_fiber_2() { boost::fibers::fiber( boost::fibers::launch::post, fn2).join(); } boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Fiber: future test suite"); test->add(BOOST_TEST_CASE(test_future_create)); test->add(BOOST_TEST_CASE(test_future_create_ref)); test->add(BOOST_TEST_CASE(test_future_create_void)); test->add(BOOST_TEST_CASE(test_future_move)); test->add(BOOST_TEST_CASE(test_future_move_ref)); test->add(BOOST_TEST_CASE(test_future_move_void)); test->add(BOOST_TEST_CASE(test_future_get)); test->add(BOOST_TEST_CASE(test_future_get_move)); test->add(BOOST_TEST_CASE(test_future_get_ref)); test->add(BOOST_TEST_CASE(test_future_get_void)); test->add(BOOST_TEST_CASE(test_future_share)); test->add(BOOST_TEST_CASE(test_future_share_ref)); test->add(BOOST_TEST_CASE(test_future_share_void)); test->add(BOOST_TEST_CASE(test_future_wait)); test->add(BOOST_TEST_CASE(test_future_wait_ref)); test->add(BOOST_TEST_CASE(test_future_wait_void)); test->add(BOOST_TEST_CASE(test_future_wait_for)); test->add(BOOST_TEST_CASE(test_future_wait_for_ref)); test->add(BOOST_TEST_CASE(test_future_wait_for_void)); test->add(BOOST_TEST_CASE(test_future_wait_until)); test->add(BOOST_TEST_CASE(test_future_wait_until_ref)); test->add(BOOST_TEST_CASE(test_future_wait_until_void)); test->add(BOOST_TEST_CASE(test_future_wait_with_fiber_1)); test->add(BOOST_TEST_CASE(test_future_wait_with_fiber_2)); return test; }
0
repos/fiber
repos/fiber/test/test_promise_post.cpp
// (C) Copyright 2008-10 Anthony Williams // // 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 <utility> #include <memory> #include <stdexcept> #include <string> #include <boost/test/unit_test.hpp> #include <boost/fiber/all.hpp> int gi = 7; struct my_exception : public std::runtime_error { my_exception() : std::runtime_error("my_exception") { } }; struct A { A() = default; A( A const&) = delete; A( A &&) = default; A & operator=( A const&) = delete; A & operator=( A &&) = default; int value{ 0 }; }; void fn1( boost::fibers::promise< int > * p, int i) { boost::this_fiber::yield(); p->set_value( i); } void fn2() { boost::fibers::promise< int > p; boost::fibers::future< int > f( p.get_future() ); boost::this_fiber::yield(); boost::fibers::fiber( boost::fibers::launch::post, fn1, & p, 7).detach(); boost::this_fiber::yield(); BOOST_CHECK( 7 == f.get() ); } int fn3() { return 3; } void fn4() { } int fn5() { boost::throw_exception( my_exception() ); return 3; } void fn6() { boost::throw_exception( my_exception() ); } int & fn7() { return gi; } int fn8( int i) { return i; } A fn9() { A a; a.value = 3; return a; } A fn10() { boost::throw_exception( my_exception() ); return A(); } // promise void test_promise_create() { // use std::allocator<> as default boost::fibers::promise< int > p1; // use std::allocator<> as user defined std::allocator< boost::fibers::promise< int > > alloc; boost::fibers::promise< int > p2( std::allocator_arg, alloc); } void test_promise_create_ref() { // use std::allocator<> as default boost::fibers::promise< int& > p1; // use std::allocator<> as user defined std::allocator< boost::fibers::promise< int& > > alloc; boost::fibers::promise< int& > p2( std::allocator_arg, alloc); } void test_promise_create_void() { // use std::allocator<> as default boost::fibers::promise< void > p1; // use std::allocator<> as user defined std::allocator< boost::fibers::promise< void > > alloc; boost::fibers::promise< void > p2( std::allocator_arg, alloc); } void test_promise_move() { boost::fibers::promise< int > p1; // move construction boost::fibers::promise< int > p2( std::move( p1) ); // move assigment p1 = std::move( p2); } void test_promise_move_ref() { boost::fibers::promise< int& > p1; // move construction boost::fibers::promise< int& > p2( std::move( p1) ); // move assigment p1 = std::move( p2); } void test_promise_move_void() { boost::fibers::promise< void > p1; // move construction boost::fibers::promise< void > p2( std::move( p1) ); // move assigment p1 = std::move( p2); } void test_promise_swap() { boost::fibers::promise< int > p1; // move construction boost::fibers::promise< int > p2( std::move( p1) ); // swap p1.swap( p2); } void test_promise_swap_ref() { boost::fibers::promise< int& > p1; // move construction boost::fibers::promise< int& > p2( std::move( p1) ); // swap p1.swap( p2); } void test_promise_swap_void() { boost::fibers::promise< void > p1; // move construction boost::fibers::promise< void > p2( std::move( p1) ); // swap p1.swap( p2); } void test_promise_get_future() { boost::fibers::promise< int > p1; // retrieve future boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // retrieve future a second time bool thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::future_already_retrieved const&) { thrown = true; } BOOST_CHECK( thrown); // move construction boost::fibers::promise< int > p2( std::move( p1) ); // retrieve future from uninitialized thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::promise_uninitialized const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_get_future_ref() { boost::fibers::promise< int& > p1; // retrieve future boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // retrieve future a second time bool thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::future_already_retrieved const&) { thrown = true; } BOOST_CHECK( thrown); // move construction boost::fibers::promise< int& > p2( std::move( p1) ); // retrieve future from uninitialized thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::promise_uninitialized const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_get_future_void() { boost::fibers::promise< void > p1; // retrieve future boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // retrieve future a second time bool thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::future_already_retrieved const&) { thrown = true; } BOOST_CHECK( thrown); // move construction boost::fibers::promise< void > p2( std::move( p1) ); // retrieve future from uninitialized thrown = false; try { f1 = p1.get_future(); } catch ( boost::fibers::promise_uninitialized const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_value() { // promise takes a copyable as return type boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // copy value p1.set_value( 7); BOOST_CHECK( 7 == f1.get() ); // set value a second time bool thrown = false; try { p1.set_value( 11); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_value_move() { // promise takes a copyable as return type boost::fibers::promise< A > p1; boost::fibers::future< A > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // move value A a1; a1.value = 7; p1.set_value( std::move( a1) ); A a2 = f1.get(); BOOST_CHECK( 7 == a2.value); // set value a second time bool thrown = false; try { A a; p1.set_value( std::move( a) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_value_ref() { // promise takes a reference as return type boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // copy value int i = 7; p1.set_value( i); int & j = f1.get(); BOOST_CHECK( &i == &j); // set value a second time bool thrown = false; try { p1.set_value( i); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_value_void() { // promise takes a copyable as return type boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // set void p1.set_value(); f1.get(); // set value a second time bool thrown = false; try { p1.set_value(); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_exception() { boost::fibers::promise< int > p1; boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); p1.set_exception( std::make_exception_ptr( my_exception() ) ); // set exception a second time bool thrown = false; try { p1.set_exception( std::make_exception_ptr( my_exception() ) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); // set value thrown = false; try { p1.set_value( 11); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_exception_ref() { boost::fibers::promise< int& > p1; boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); p1.set_exception( std::make_exception_ptr( my_exception() ) ); // set exception a second time bool thrown = false; try { p1.set_exception( std::make_exception_ptr( my_exception() ) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); // set value thrown = false; int i = 11; try { p1.set_value( i); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } void test_promise_set_exception_void() { boost::fibers::promise< void > p1; boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); p1.set_exception( std::make_exception_ptr( my_exception() ) ); // set exception a second time bool thrown = false; try { p1.set_exception( std::make_exception_ptr( my_exception() ) ); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); // set value thrown = false; try { p1.set_value(); } catch ( boost::fibers::promise_already_satisfied const&) { thrown = true; } BOOST_CHECK( thrown); } boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Fiber: promise test suite"); test->add(BOOST_TEST_CASE(test_promise_create)); test->add(BOOST_TEST_CASE(test_promise_create_ref)); test->add(BOOST_TEST_CASE(test_promise_create_void)); test->add(BOOST_TEST_CASE(test_promise_move)); test->add(BOOST_TEST_CASE(test_promise_move_ref)); test->add(BOOST_TEST_CASE(test_promise_move_void)); test->add(BOOST_TEST_CASE(test_promise_swap)); test->add(BOOST_TEST_CASE(test_promise_swap_ref)); test->add(BOOST_TEST_CASE(test_promise_swap_void)); test->add(BOOST_TEST_CASE(test_promise_get_future)); test->add(BOOST_TEST_CASE(test_promise_get_future_ref)); test->add(BOOST_TEST_CASE(test_promise_get_future_void)); test->add(BOOST_TEST_CASE(test_promise_set_value)); test->add(BOOST_TEST_CASE(test_promise_set_value_move)); test->add(BOOST_TEST_CASE(test_promise_set_value_ref)); test->add(BOOST_TEST_CASE(test_promise_set_value_void)); test->add(BOOST_TEST_CASE(test_promise_set_exception)); test->add(BOOST_TEST_CASE(test_promise_set_exception_ref)); test->add(BOOST_TEST_CASE(test_promise_set_exception_void)); return test; }
0
repos/fiber
repos/fiber/test/test_unbuffered_channel_dispatch.cpp
// 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) #include <chrono> #include <sstream> #include <string> #include <vector> #include <boost/assert.hpp> #include <boost/test/unit_test.hpp> #include <boost/fiber/all.hpp> struct moveable { bool state; int value; moveable() : state( false), value( -1) { } moveable( int v) : state( true), value( v) { } moveable( moveable && other) : state( other.state), value( other.value) { other.state = false; other.value = -1; } moveable & operator=( moveable && other) { if ( this == & other) return * this; state = other.state; other.state = false; value = other.value; other.value = -1; return * this; } }; void test_push() { boost::fibers::unbuffered_channel< int > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 1) ); }); int value = 0; BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( value) ); BOOST_CHECK_EQUAL( 1, value); f.join(); } void test_push_closed() { boost::fibers::unbuffered_channel< int > c; c.close(); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.push( 1) ); } void test_push_wait_for() { boost::fibers::unbuffered_channel< int > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push_wait_for( 1, std::chrono::seconds( 1) ) ); }); int value = 0; BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( value) ); BOOST_CHECK_EQUAL( 1, value); f.join(); } void test_push_wait_for_closed() { boost::fibers::unbuffered_channel< int > c; c.close(); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.push_wait_for( 1, std::chrono::seconds( 1) ) ); } void test_push_wait_for_timeout() { boost::fibers::unbuffered_channel< int > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c](){ int value = 0; BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( value) ); BOOST_CHECK_EQUAL( 1, value); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push_wait_for( 1, std::chrono::seconds( 1) ) ); BOOST_CHECK( boost::fibers::channel_op_status::timeout == c.push_wait_for( 1, std::chrono::seconds( 1) ) ); f.join(); } void test_push_wait_until() { boost::fibers::unbuffered_channel< int > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push_wait_until( 1, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); }); int value = 0; BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( value) ); BOOST_CHECK_EQUAL( 1, value); f.join(); } void test_push_wait_until_closed() { boost::fibers::unbuffered_channel< int > c; c.close(); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.push_wait_until( 1, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); } void test_push_wait_until_timeout() { boost::fibers::unbuffered_channel< int > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c](){ int value = 0; BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( value) ); BOOST_CHECK_EQUAL( 1, value); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push_wait_until( 1, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); BOOST_CHECK( boost::fibers::channel_op_status::timeout == c.push_wait_until( 1, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); f.join(); } void test_pop() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&v1,&c](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( v2) ); BOOST_CHECK_EQUAL( v1, v2); f.join(); } void test_pop_closed() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&v1,&c](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); c.close(); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( v2) ); BOOST_CHECK_EQUAL( v1, v2); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.pop( v2) ); f.join(); } void test_pop_success() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v2](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( v2) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_value_pop() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); }); v2 = c.value_pop(); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_value_pop_closed() { boost::fibers::unbuffered_channel< int > c; int v1 = 2; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); c.close(); }); int v2 = c.value_pop(); BOOST_CHECK_EQUAL( v1, v2); f.join(); bool thrown = false; try { c.value_pop(); } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); } void test_value_pop_success() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v2](){ v2 = c.value_pop(); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_pop_wait_for() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_for( v2, std::chrono::seconds( 1) ) ); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_pop_wait_for_closed() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); c.close(); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_for( v2, std::chrono::seconds( 1) ) ); BOOST_CHECK_EQUAL( v1, v2); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.pop_wait_for( v2, std::chrono::seconds( 1) ) ); f.join(); } void test_pop_wait_for_success() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v2](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_for( v2, std::chrono::seconds( 1) ) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_pop_wait_for_timeout() { boost::fibers::unbuffered_channel< int > c; int v = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v](){ BOOST_CHECK( boost::fibers::channel_op_status::timeout == c.pop_wait_for( v, std::chrono::seconds( 1) ) ); }); f.join(); } void test_pop_wait_until() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_until( v2, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); BOOST_CHECK_EQUAL( v1, v2); f.join(); } void test_pop_wait_until_closed() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v1](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); c.close(); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_until( v2, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); BOOST_CHECK_EQUAL( v1, v2); BOOST_CHECK( boost::fibers::channel_op_status::closed == c.pop_wait_until( v2, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); f.join(); } void test_pop_wait_until_success() { boost::fibers::unbuffered_channel< int > c; int v1 = 2, v2 = 0; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c,&v2](){ BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop_wait_until( v2, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); }); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( v1) ); f.join(); BOOST_CHECK_EQUAL( v1, v2); } void test_pop_wait_until_timeout() { boost::fibers::unbuffered_channel< int > c; int v = 0; BOOST_CHECK( boost::fibers::channel_op_status::timeout == c.pop_wait_until( v, std::chrono::system_clock::now() + std::chrono::seconds( 1) ) ); } void test_wm_1() { boost::fibers::unbuffered_channel< int > c; std::vector< boost::fibers::fiber::id > ids; boost::fibers::fiber f1( boost::fibers::launch::dispatch, [&c,&ids](){ ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 1) ); ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 2) ); ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 3) ); ids.push_back( boost::this_fiber::get_id() ); // would be blocked because channel is full BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 4) ); ids.push_back( boost::this_fiber::get_id() ); // would be blocked because channel is full BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( 5) ); ids.push_back( boost::this_fiber::get_id() ); }); boost::fibers::fiber f2( boost::fibers::launch::dispatch, [&c,&ids](){ ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK_EQUAL( 1, c.value_pop() ); // let other fiber run boost::this_fiber::yield(); ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK_EQUAL( 2, c.value_pop() ); ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK_EQUAL( 3, c.value_pop() ); ids.push_back( boost::this_fiber::get_id() ); BOOST_CHECK_EQUAL( 4, c.value_pop() ); ids.push_back( boost::this_fiber::get_id() ); // would block because channel is empty BOOST_CHECK_EQUAL( 5, c.value_pop() ); ids.push_back( boost::this_fiber::get_id() ); }); boost::fibers::fiber::id id1 = f1.get_id(); boost::fibers::fiber::id id2 = f2.get_id(); f1.join(); f2.join(); BOOST_CHECK_EQUAL( 12u, ids.size() ); BOOST_CHECK_EQUAL( id1, ids[0]); BOOST_CHECK_EQUAL( id2, ids[1]); BOOST_CHECK_EQUAL( id1, ids[2]); BOOST_CHECK_EQUAL( id2, ids[3]); BOOST_CHECK_EQUAL( id2, ids[4]); BOOST_CHECK_EQUAL( id1, ids[5]); BOOST_CHECK_EQUAL( id2, ids[6]); BOOST_CHECK_EQUAL( id1, ids[7]); BOOST_CHECK_EQUAL( id2, ids[8]); BOOST_CHECK_EQUAL( id1, ids[9]); BOOST_CHECK_EQUAL( id2, ids[10]); BOOST_CHECK_EQUAL( id1, ids[11]); } void test_moveable() { boost::fibers::unbuffered_channel< moveable > c; boost::fibers::fiber f( boost::fibers::launch::dispatch, [&c]{ moveable m1( 3); BOOST_CHECK( m1.state); BOOST_CHECK_EQUAL( 3, m1.value); BOOST_CHECK( boost::fibers::channel_op_status::success == c.push( std::move( m1) ) ); }); moveable m2; BOOST_CHECK( ! m2.state); BOOST_CHECK_EQUAL( -1, m2.value); BOOST_CHECK( boost::fibers::channel_op_status::success == c.pop( m2) ); BOOST_CHECK( m2.state); BOOST_CHECK_EQUAL( 3, m2.value); f.join(); } void test_rangefor() { boost::fibers::unbuffered_channel< int > chan; std::vector< int > vec; boost::fibers::fiber f1( boost::fibers::launch::dispatch, [&chan]{ chan.push( 1); chan.push( 1); chan.push( 2); chan.push( 3); chan.push( 5); chan.push( 8); chan.push( 12); chan.close(); }); boost::fibers::fiber f2( boost::fibers::launch::dispatch, [&vec,&chan]{ for ( int value : chan) { vec.push_back( value); } }); f1.join(); f2.join(); BOOST_CHECK_EQUAL( 1, vec[0]); BOOST_CHECK_EQUAL( 1, vec[1]); BOOST_CHECK_EQUAL( 2, vec[2]); BOOST_CHECK_EQUAL( 3, vec[3]); BOOST_CHECK_EQUAL( 5, vec[4]); BOOST_CHECK_EQUAL( 8, vec[5]); BOOST_CHECK_EQUAL( 12, vec[6]); } void test_issue_181() { boost::fibers::unbuffered_channel< int > chan; boost::fibers::fiber f1( boost::fibers::launch::dispatch, [&chan]() { auto state = chan.push( 1); BOOST_CHECK( boost::fibers::channel_op_status::closed == state); }); boost::fibers::fiber f2( boost::fibers::launch::dispatch, [&chan]() { boost::this_fiber::sleep_for( std::chrono::milliseconds( 100) ); chan.close(); }); f2.join(); f1.join(); } boost::unit_test::test_suite * init_unit_test_suite( int, char* []) { boost::unit_test::test_suite * test = BOOST_TEST_SUITE("Boost.Fiber: unbuffered_channel test suite"); test->add( BOOST_TEST_CASE( & test_push) ); test->add( BOOST_TEST_CASE( & test_push_closed) ); test->add( BOOST_TEST_CASE( & test_push_wait_for) ); test->add( BOOST_TEST_CASE( & test_push_wait_for_closed) ); test->add( BOOST_TEST_CASE( & test_push_wait_for_timeout) ); test->add( BOOST_TEST_CASE( & test_push_wait_until) ); test->add( BOOST_TEST_CASE( & test_push_wait_until_closed) ); test->add( BOOST_TEST_CASE( & test_push_wait_until_timeout) ); test->add( BOOST_TEST_CASE( & test_pop) ); test->add( BOOST_TEST_CASE( & test_pop_closed) ); test->add( BOOST_TEST_CASE( & test_pop_success) ); test->add( BOOST_TEST_CASE( & test_value_pop) ); test->add( BOOST_TEST_CASE( & test_value_pop_closed) ); test->add( BOOST_TEST_CASE( & test_value_pop_success) ); test->add( BOOST_TEST_CASE( & test_pop_wait_for) ); test->add( BOOST_TEST_CASE( & test_pop_wait_for_closed) ); test->add( BOOST_TEST_CASE( & test_pop_wait_for_success) ); test->add( BOOST_TEST_CASE( & test_pop_wait_for_timeout) ); test->add( BOOST_TEST_CASE( & test_pop_wait_until) ); test->add( BOOST_TEST_CASE( & test_pop_wait_until_closed) ); test->add( BOOST_TEST_CASE( & test_pop_wait_until_success) ); test->add( BOOST_TEST_CASE( & test_pop_wait_until_timeout) ); test->add( BOOST_TEST_CASE( & test_wm_1) ); test->add( BOOST_TEST_CASE( & test_moveable) ); test->add( BOOST_TEST_CASE( & test_rangefor) ); test->add( BOOST_TEST_CASE( & test_issue_181) ); return test; }
0
repos/fiber
repos/fiber/test/test_shared_future_dispatch.cpp
// (C) Copyright 2008-10 Anthony Williams // 2015 Oliver Kowalke // // 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 <chrono> #include <memory> #include <stdexcept> #include <string> #include <utility> #include <boost/test/unit_test.hpp> #include <boost/fiber/all.hpp> typedef std::chrono::milliseconds ms; typedef std::chrono::high_resolution_clock Clock; int gi = 7; struct my_exception : public std::runtime_error { my_exception() : std::runtime_error("my_exception") { } }; struct A { A() = default; A( A const&) = delete; A( A &&) = default; A & operator=( A const&) = delete; A & operator=( A &&) = default; int value; }; void fn1( boost::fibers::promise< int > * p, int i) { boost::this_fiber::yield(); p->set_value( i); } void fn2() { boost::fibers::promise< int > p; boost::fibers::shared_future< int > f( p.get_future().share() ); boost::this_fiber::yield(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn1, & p, 7).detach(); boost::this_fiber::yield(); BOOST_CHECK( 7 == f.get() ); } int fn3() { return 3; } void fn4() { } int fn5() { boost::throw_exception( my_exception() ); return 3; } void fn6() { boost::throw_exception( my_exception() ); } int & fn7() { return gi; } int fn8( int i) { return i; } A fn9() { A a; a.value = 3; return a; } A fn10() { boost::throw_exception( my_exception() ); return A(); } void fn11( boost::fibers::promise< int > p) { boost::this_fiber::sleep_for( ms(500) ); p.set_value(3); } void fn12( boost::fibers::promise< int& > p) { boost::this_fiber::sleep_for( ms(500) ); gi = 5; p.set_value( gi); } void fn13( boost::fibers::promise< void > p) { boost::this_fiber::sleep_for( ms(500) ); p.set_value(); } // shared_future void test_shared_future_create() { { // default constructed and assigned shared_future is not valid boost::fibers::shared_future< int > f1; boost::fibers::shared_future< int > f2 = f1; BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( ! f2.valid() ); } { // shared_future retrieved from promise is valid boost::fibers::promise< int > p; boost::fibers::shared_future< int > f1 = p.get_future(); boost::fibers::shared_future< int > f2 = f1; BOOST_CHECK( f1.valid() ); BOOST_CHECK( f2.valid() ); } } void test_shared_future_create_ref() { { // default constructed and assigned shared_future is not valid boost::fibers::shared_future< int& > f1; boost::fibers::shared_future< int& > f2 = f1; BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( ! f2.valid() ); } { // shared_future retrieved from promise is valid boost::fibers::promise< int& > p; boost::fibers::shared_future< int& > f1 = p.get_future(); boost::fibers::shared_future< int& > f2 = f1; BOOST_CHECK( f1.valid() ); BOOST_CHECK( f2.valid() ); } } void test_shared_future_create_void() { { // default constructed and assigned shared_future is not valid boost::fibers::shared_future< void > f1; boost::fibers::shared_future< void > f2 = f1; BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( ! f2.valid() ); } { // shared_future retrieved from promise is valid boost::fibers::promise< void > p; boost::fibers::shared_future< void > f1 = p.get_future(); boost::fibers::shared_future< void > f2 = f1; BOOST_CHECK( f1.valid() ); BOOST_CHECK( f2.valid() ); } } void test_shared_future_get() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; p1.set_value( 7); boost::fibers::shared_future< int > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); BOOST_CHECK( 7 == f1.get() ); BOOST_CHECK( f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< int > p2; f1 = p2.get_future().share(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( f1.valid() ); BOOST_CHECK( thrown); } void test_shared_future_get_move() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< A > p1; A a; a.value = 7; p1.set_value( std::move( a) ); boost::fibers::shared_future< A > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); BOOST_CHECK( 7 == f1.get().value); BOOST_CHECK( f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< A > p2; f1 = p2.get_future().share(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( f1.valid() ); BOOST_CHECK( thrown); } void test_shared_future_get_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; int i = 7; p1.set_value( i); boost::fibers::shared_future< int& > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); int & j = f1.get(); BOOST_CHECK( &i == &j); BOOST_CHECK( f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< int& > p2; f1 = p2.get_future().share(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( f1.valid() ); BOOST_CHECK( thrown); } void test_shared_future_get_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; p1.set_value(); boost::fibers::shared_future< void > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // get BOOST_CHECK( ! f1.get_exception_ptr() ); f1.get(); BOOST_CHECK( f1.valid() ); // throw broken_promise if promise is destroyed without set { boost::fibers::promise< void > p2; f1 = p2.get_future().share(); } bool thrown = false; try { f1.get(); } catch ( boost::fibers::broken_promise const&) { thrown = true; } BOOST_CHECK( f1.valid() ); BOOST_CHECK( thrown); } void test_future_share() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; int i = 7; p1.set_value( i); boost::fibers::future< int > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< int > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); int j = sf1.get(); BOOST_CHECK_EQUAL( i, j); BOOST_CHECK( sf1.valid() ); } void test_future_share_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; int i = 7; p1.set_value( i); boost::fibers::future< int& > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< int& > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); int & j = sf1.get(); BOOST_CHECK( &i == &j); BOOST_CHECK( sf1.valid() ); } void test_future_share_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; p1.set_value(); boost::fibers::future< void > f1 = p1.get_future(); BOOST_CHECK( f1.valid() ); // share boost::fibers::shared_future< void > sf1 = f1.share(); BOOST_CHECK( sf1.valid() ); BOOST_CHECK( ! f1.valid() ); // get BOOST_CHECK( ! sf1.get_exception_ptr() ); sf1.get(); BOOST_CHECK( sf1.valid() ); } void test_shared_future_wait() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::shared_future< int > f1 = p1.get_future().share(); // wait on future p1.set_value( 7); f1.wait(); BOOST_CHECK( 7 == f1.get() ); } void test_shared_future_wait_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::shared_future< int& > f1 = p1.get_future().share(); // wait on future int i = 7; p1.set_value( i); f1.wait(); int & j = f1.get(); BOOST_CHECK( &i == &j); } void test_shared_future_wait_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::shared_future< void > f1 = p1.get_future().share(); // wait on future p1.set_value(); f1.wait(); f1.get(); BOOST_CHECK( f1.valid() ); } void test_shared_future_wait_for() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::shared_future< int > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn11, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_for_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::shared_future< int& > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn12, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_for_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::shared_future< void > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn13, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_for( ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_until() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::shared_future< int > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn11, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_until_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::shared_future< int& > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn12, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_until_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::shared_future< void > f1 = p1.get_future().share(); boost::fibers::fiber( boost::fibers::launch::dispatch, fn13, std::move( p1) ).detach(); // wait on future BOOST_CHECK( f1.valid() ); boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::timeout == status); BOOST_CHECK( f1.valid() ); status = f1.wait_until( Clock::now() + ms(300) ); BOOST_CHECK( boost::fibers::future_status::ready == status); BOOST_CHECK( f1.valid() ); f1.wait(); } void test_shared_future_wait_with_fiber_1() { boost::fibers::promise< int > p1; boost::fibers::fiber( boost::fibers::launch::dispatch, fn1, & p1, 7).detach(); boost::fibers::shared_future< int > f1 = p1.get_future().share(); // wait on future BOOST_CHECK( 7 == f1.get() ); } void test_shared_future_wait_with_fiber_2() { boost::fibers::fiber( boost::fibers::launch::dispatch, fn2).join(); } void test_shared_future_move() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int > p1; boost::fibers::shared_future< int > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::shared_future< int > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_shared_future_move_move() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< A > p1; boost::fibers::shared_future< A > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::shared_future< A > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_shared_future_move_ref() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< int& > p1; boost::fibers::shared_future< int& > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::shared_future< int& > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } void test_shared_future_move_void() { // future retrieved from promise is valid (if it is the first) boost::fibers::promise< void > p1; boost::fibers::shared_future< void > f1 = p1.get_future().share(); BOOST_CHECK( f1.valid() ); // move construction boost::fibers::shared_future< void > f2( std::move( f1) ); BOOST_CHECK( ! f1.valid() ); BOOST_CHECK( f2.valid() ); // move assignment f1 = std::move( f2); BOOST_CHECK( f1.valid() ); BOOST_CHECK( ! f2.valid() ); } boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Fiber: shared_future test suite"); test->add(BOOST_TEST_CASE(test_shared_future_create)); test->add(BOOST_TEST_CASE(test_shared_future_create_ref)); test->add(BOOST_TEST_CASE(test_shared_future_create_void)); test->add(BOOST_TEST_CASE(test_shared_future_move)); test->add(BOOST_TEST_CASE(test_shared_future_move_move)); test->add(BOOST_TEST_CASE(test_shared_future_move_ref)); test->add(BOOST_TEST_CASE(test_shared_future_move_void)); test->add(BOOST_TEST_CASE(test_shared_future_get)); test->add(BOOST_TEST_CASE(test_shared_future_get_move)); test->add(BOOST_TEST_CASE(test_shared_future_get_ref)); test->add(BOOST_TEST_CASE(test_shared_future_get_void)); test->add(BOOST_TEST_CASE(test_shared_future_wait)); test->add(BOOST_TEST_CASE(test_shared_future_wait_ref)); test->add(BOOST_TEST_CASE(test_shared_future_wait_void)); test->add(BOOST_TEST_CASE(test_shared_future_wait_for)); test->add(BOOST_TEST_CASE(test_shared_future_wait_for_ref)); test->add(BOOST_TEST_CASE(test_shared_future_wait_for_void)); test->add(BOOST_TEST_CASE(test_shared_future_wait_until)); test->add(BOOST_TEST_CASE(test_shared_future_wait_until_ref)); test->add(BOOST_TEST_CASE(test_shared_future_wait_until_void)); test->add(BOOST_TEST_CASE(test_shared_future_wait_with_fiber_1)); test->add(BOOST_TEST_CASE(test_shared_future_wait_with_fiber_2)); return test; }
0
repos/fiber
repos/fiber/test/test_async_dispatch.cpp
// (C) Copyright 2008-10 Anthony Williams // 2015 Oliver Kowalke // // 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 <utility> #include <memory> #include <stdexcept> #include <string> #include <boost/test/unit_test.hpp> #include <boost/fiber/all.hpp> struct A { A() = default; A( A const&) = delete; A & operator=( A const&) = delete; A( A && other) : value{ other.value } { other.value = 0; } A & operator=( A && other) { if ( this == & other) return * this; value = other.value; other.value = 0; return * this; } int value{ 0 }; }; struct X { int value; void foo( int i) { value = i; } }; void fn1() { } int fn2( int i) { return i; } int & fn3( int & i) { return i; } A fn4( A && a) { return std::forward< A >( a); } void test_async_1() { boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn1); BOOST_CHECK( f1.valid() ); f1.get(); } void test_async_2() { int i = 3; boost::fibers::future< int > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn2, i); BOOST_CHECK( f1.valid() ); BOOST_CHECK( i == f1.get()); } void test_async_3() { int i = 7; boost::fibers::future< int& > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn3, std::ref( i) ); BOOST_CHECK( f1.valid() ); BOOST_CHECK( & i == & f1.get()); } void test_async_4() { A a1; a1.value = 7; boost::fibers::future< A > f1 = boost::fibers::async( boost::fibers::launch::dispatch, fn4, std::move( a1) ); BOOST_CHECK( f1.valid() ); A a2 = f1.get(); BOOST_CHECK( 7 == a2.value); } void test_async_5() { X x = {0}; BOOST_CHECK( 0 == x.value); boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, std::bind( & X::foo, std::ref( x), 3) ); BOOST_CHECK( f1.valid() ); f1.get(); BOOST_CHECK( 3 == x.value); } void test_async_6() { X x = {0}; BOOST_CHECK( 0 == x.value); boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, std::bind( & X::foo, std::ref( x), std::placeholders::_1), 3); BOOST_CHECK( f1.valid() ); f1.get(); BOOST_CHECK( 3 == x.value); } void test_async_stack_alloc() { boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, std::allocator_arg, boost::fibers::fixedsize_stack{}, fn1); BOOST_CHECK( f1.valid() ); f1.get(); } void test_async_std_alloc() { struct none {}; boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, std::allocator_arg, boost::fibers::fixedsize_stack{}, std::allocator< none >{}, fn1); BOOST_CHECK( f1.valid() ); f1.get(); } boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Fiber: async test suite"); test->add(BOOST_TEST_CASE(test_async_1)); test->add(BOOST_TEST_CASE(test_async_2)); test->add(BOOST_TEST_CASE(test_async_3)); test->add(BOOST_TEST_CASE(test_async_4)); test->add(BOOST_TEST_CASE(test_async_5)); test->add(BOOST_TEST_CASE(test_async_6)); test->add(BOOST_TEST_CASE(test_async_stack_alloc)); test->add(BOOST_TEST_CASE(test_async_std_alloc)); return test; }
0
repos/fiber
repos/fiber/doc/fibers.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd"> <library id="fiber" name="Fiber" dirname="fiber" last-revision="$Date: 2018/10/22 08:13:04 $" xmlns:xi="http://www.w3.org/2001/XInclude"> <libraryinfo> <authorgroup> <author> <firstname>Oliver</firstname> <surname>Kowalke</surname> </author> </authorgroup> <copyright> <year>2013</year> <holder>Oliver Kowalke</holder> </copyright> <legalnotice id="fiber.legal"> <para> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <ulink url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>) </para> </legalnotice> <librarypurpose> C++ Library to cooperatively schedule and synchronize micro-threads </librarypurpose> <librarycategory name="category:text"></librarycategory> </libraryinfo> <title>Fiber</title> <section id="fiber.overview"> <title><link linkend="fiber.overview">Overview</link></title> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides a framework for micro-/userland-threads (fibers) scheduled cooperatively. The API contains classes and functions to manage and synchronize fibers similiarly to <ulink url="http://en.cppreference.com/w/cpp/thread">standard thread support library</ulink>. </para> <para> Each fiber has its own stack. </para> <para> A fiber can save the current execution state, including all registers and CPU flags, the instruction pointer, and the stack pointer and later restore this state. The idea is to have multiple execution paths running on a single thread using cooperative scheduling (versus threads, which are preemptively scheduled). The running fiber decides explicitly when it should yield to allow another fiber to run (context switching). <emphasis role="bold">Boost.Fiber</emphasis> internally uses <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html"><emphasis>call/cc</emphasis></ulink> from <ulink url="http://www.boost.org/doc/libs/release/libs/context/index.html">Boost.Context</ulink>; the classes in this library manage, schedule and, when needed, synchronize those execution contexts. A context switch between threads usually costs thousands of CPU cycles on x86, compared to a fiber switch with less than a hundred cycles. A fiber runs on a single thread at any point in time. </para> <para> In order to use the classes and functions described here, you can either include the specific headers specified by the descriptions of each class or function, or include the master library header: </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">all</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> </programlisting> <para> which includes all the other headers in turn. </para> <para> The namespaces used are: </para> <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.overview.h0"> <phrase id="fiber.overview.fibers_and_threads"/><link linkend="fiber.overview.fibers_and_threads">Fibers and Threads</link> </bridgehead> <para> Control is cooperatively passed between fibers launched on a given thread. At a given moment, on a given thread, at most one fiber is running. </para> <para> Spawning additional fibers on a given thread does not distribute your program across more hardware cores, though it can make more effective use of the core on which it's running. </para> <para> On the other hand, a fiber may safely access any resource exclusively owned by its parent thread without explicitly needing to defend that resource against concurrent access by other fibers on the same thread. You are already guaranteed that no other fiber on that thread is concurrently touching that resource. This can be particularly important when introducing concurrency in legacy code. You can safely spawn fibers running old code, using asynchronous I/O to interleave execution. </para> <para> In effect, fibers provide a natural way to organize concurrent code based on asynchronous I/O. Instead of chaining together completion handlers, code running on a fiber can make what looks like a normal blocking function call. That call can cheaply suspend the calling fiber, allowing other fibers on the same thread to run. When the operation has completed, the suspended fiber resumes, without having to explicitly save or restore its state. Its local stack variables persist across the call. </para> <para> A fiber can be migrated from one thread to another, though the library does not do this by default. It is possible for you to supply a custom scheduler that migrates fibers between threads. You may specify custom fiber properties to help your scheduler decide which fibers are permitted to migrate. Please see <link linkend="migration">Migrating fibers between threads</link> and <link linkend="custom">Customization</link> for more details. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> allows to <emphasis role="bold"><code><phrase role="identifier">multiplex</phrase> <phrase role="identifier">fibers</phrase> <phrase role="identifier">across</phrase> <phrase role="identifier">multiple</phrase> <phrase role="identifier">cores</phrase></code></emphasis> (see <link linkend="class_numa_work_stealing"><code>numa::work_stealing</code></link>). </para> <para> A fiber launched on a particular thread continues running on that thread unless migrated. It might be unblocked (see <link linkend="blocking">Blocking</link> below) by some other thread, but that only transitions the fiber from <quote>blocked</quote> to <quote>ready</quote> on its current thread &mdash; it does not cause the fiber to resume on the thread that unblocked it. </para> <anchor id="thread_local_storage"/> <bridgehead renderas="sect3" id="fiber.overview.h1"> <phrase id="fiber.overview.thread_local_storage"/><link linkend="fiber.overview.thread_local_storage">thread-local storage</link> </bridgehead> <para> Unless migrated, a fiber may access thread-local storage; however that storage will be shared among all fibers running on the same thread. For fiber-local storage, please see <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link>. </para> <anchor id="cross_thread_sync"/> <bridgehead renderas="sect3" id="fiber.overview.h2"> <phrase id="fiber.overview.boost_fibers_no_atomics"/><link linkend="fiber.overview.boost_fibers_no_atomics">BOOST_FIBERS_NO_ATOMICS</link> </bridgehead> <para> The fiber synchronization objects provided by this library will, by default, safely synchronize fibers running on different threads. However, this level of synchronization can be removed (for performance) by building the library with <emphasis role="bold"><code><phrase role="identifier">BOOST_FIBERS_NO_ATOMICS</phrase></code></emphasis> defined. When the library is built with that macro, you must ensure that all the fibers referencing a particular synchronization object are running in the same thread. Please see <link linkend="synchronization">Synchronization</link>. </para> <anchor id="blocking"/> <bridgehead renderas="sect3" id="fiber.overview.h3"> <phrase id="fiber.overview.blocking"/><link linkend="fiber.overview.blocking">Blocking</link> </bridgehead> <para> Normally, when this documentation states that a particular fiber <emphasis>blocks</emphasis> (or equivalently, <emphasis>suspends),</emphasis> it means that it yields control, allowing other fibers on the same thread to run. The synchronization mechanisms provided by <emphasis role="bold">Boost.Fiber</emphasis> have this behavior. </para> <para> A fiber may, of course, use normal thread synchronization mechanisms; however a fiber that invokes any of these mechanisms will block its entire thread, preventing any other fiber from running on that thread in the meantime. For instance, when a fiber wants to wait for a value from another fiber in the same thread, using <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase></code> would be unfortunate: <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> would block the whole thread, preventing the other fiber from delivering its value. Use <link linkend="class_future"><code>future&lt;&gt;</code></link> instead. </para> <para> Similarly, a fiber that invokes a normal blocking I/O operation will block its entire thread. Fiber authors are encouraged to consistently use asynchronous I/O. <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink> and other asynchronous I/O operations can straightforwardly be adapted for <emphasis role="bold">Boost.Fiber</emphasis>: see <link linkend="callbacks">Integrating Fibers with Asynchronous Callbacks</link>. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> depends upon <ulink url="http://www.boost.org/doc/libs/release/libs/context/index.html">Boost.Context</ulink>. Boost version 1.61.0 or greater is required. </para> <note> <para> This library requires C++11! </para> </note> <section id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber"> <title><anchor id="implementation"/><link linkend="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber">Implementations: fcontext_t, ucontext_t and WinFiber</link></title> <para> <emphasis role="bold">Boost.Fiber</emphasis> uses <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html"><emphasis>call/cc</emphasis></ulink> from <ulink url="http://www.boost.org/doc/libs/release/libs/context/index.html">Boost.Context</ulink> as building-block. </para> <bridgehead renderas="sect4" id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h0"> <phrase id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t"/><link linkend="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t">fcontext_t</link> </bridgehead> <para> The implementation uses <code><phrase role="identifier">fcontext_t</phrase></code> per default. fcontext_t is based on assembler and not available for all platforms. It provides a much better performance than <code><phrase role="identifier">ucontext_t</phrase></code> (the context switch takes two magnitudes of order less CPU cycles; see section <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/performance.html"><emphasis>performance</emphasis></ulink>) and <code><phrase role="identifier">WinFiber</phrase></code>. </para> <bridgehead renderas="sect4" id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h1"> <phrase id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t"/><link linkend="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t">ucontext_t</link> </bridgehead> <para> As an alternative, <ulink url="https://en.wikipedia.org/wiki/Setcontext"><code><phrase role="identifier">ucontext_t</phrase></code></ulink> can be used by compiling with <code><phrase role="identifier">BOOST_USE_UCONTEXT</phrase></code> and b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">ucontext</phrase></code>. <code><phrase role="identifier">ucontext_t</phrase></code> might be available on a broader range of POSIX-platforms but has some <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/rational.html#ucontext"><emphasis>disadvantages</emphasis></ulink> (for instance deprecated since POSIX.1-2003, not C99 conform). </para> <note> <para> <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html"><emphasis>call/cc</emphasis></ulink> supports <link linkend="segmented"><emphasis>Segmented stacks</emphasis></link> only with <code><phrase role="identifier">ucontext_t</phrase></code> as its implementation. </para> </note> <bridgehead renderas="sect4" id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h2"> <phrase id="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber"/><link linkend="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber">WinFiber</link> </bridgehead> <para> With <code><phrase role="identifier">BOOST_USE_WINFIB</phrase></code> and b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">winfib</phrase></code> Win32-Fibers are used as implementation for <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html"><emphasis>call/cc</emphasis></ulink>. </para> <para> Because the TIB (thread information block) is not fully described in the MSDN, it might be possible that not all required TIB-parts are swapped. </para> <note> <para> The first call of <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html"><emphasis>call/cc</emphasis></ulink> converts the thread into a Windows fiber by invoking <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase role="special">()</phrase></code>. If desired, <code><phrase role="identifier">ConvertFiberToThread</phrase><phrase role="special">()</phrase></code> has to be called by the user explicitly in order to release resources allocated by <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase role="special">()</phrase></code> (e.g. after using boost.context). </para> </note> </section> <important> <para> Windows using fcontext_t: turn off global program optimization (/GL) and change /EHsc (compiler assumes that functions declared as extern &quot;C&quot; never throw a C++ exception) to /EHs (tells compiler assumes that functions declared as extern &quot;C&quot; may throw an exception). </para> </important> </section> <section id="fiber.fiber_mgmt"> <title><link linkend="fiber.fiber_mgmt">Fiber management</link></title> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h0"> <phrase id="fiber.fiber_mgmt.synopsis"/><link linkend="fiber.fiber_mgmt.synopsis">Synopsis</link> </bridgehead> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">all</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">SchedAlgo</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">();</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">algorithm</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">algorithm_with_properties</phrase><phrase role="special">;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">round_robin</phrase><phrase role="special">;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">shared_round_robin</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">this_fiber</phrase> <phrase role="special">{</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">yield</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h1"> <phrase id="fiber.fiber_mgmt.tutorial"/><link linkend="fiber.fiber_mgmt.tutorial">Tutorial</link> </bridgehead> <para> Each <link linkend="class_fiber"><code>fiber</code></link> represents a micro-thread which will be launched and managed cooperatively by a scheduler. Objects of type <link linkend="class_fiber"><code>fiber</code></link> are move-only. </para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f1</phrase><phrase role="special">;</phrase> <phrase role="comment">// not-a-fiber</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f2</phrase><phrase role="special">(</phrase> <phrase role="identifier">some_fn</phrase><phrase role="special">);</phrase> <phrase role="identifier">f1</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">f2</phrase><phrase role="special">);</phrase> <phrase role="comment">// f2 moved to f1</phrase> <phrase role="special">}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h2"> <phrase id="fiber.fiber_mgmt.launching"/><link linkend="fiber.fiber_mgmt.launching">Launching</link> </bridgehead> <para> A new fiber is launched by passing an object of a callable type that can be invoked with no parameters. If the object must not be copied or moved, then <emphasis>std::ref</emphasis> can be used to pass in a reference to the function object. In this case, the user must ensure that the referenced object outlives the newly-created fiber. </para> <programlisting><phrase role="keyword">struct</phrase> <phrase role="identifier">callable</phrase> <phrase role="special">{</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()();</phrase> <phrase role="special">};</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">copies_are_safe</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">callable</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// x is destroyed, but the newly-created fiber has a copy, so this is OK</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">oops</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">callable</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// x is destroyed, but the newly-created fiber still has a reference</phrase> <phrase role="comment">// this leads to undefined behaviour</phrase> </programlisting> <para> The spawned <link linkend="class_fiber"><code>fiber</code></link> does not immediately start running. It is enqueued in the list of ready-to-run fibers, and will run when the scheduler gets around to it. </para> <anchor id="exceptions"/> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h3"> <phrase id="fiber.fiber_mgmt.exceptions"/><link linkend="fiber.fiber_mgmt.exceptions">Exceptions</link> </bridgehead> <para> An exception escaping from the function or callable object passed to the <link linkend="class_fiber"><code>fiber</code></link> constructor calls <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase><phrase role="special">()</phrase></code>. If you need to know which exception was thrown, use <link linkend="class_future"><code>future&lt;&gt;</code></link> or <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link>. </para> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h4"> <phrase id="fiber.fiber_mgmt.detaching"/><link linkend="fiber.fiber_mgmt.detaching">Detaching</link> </bridgehead> <para> A <link linkend="class_fiber"><code>fiber</code></link> can be detached by explicitly invoking the <link linkend="fiber_detach"><code>fiber::detach()</code></link> member function. After <link linkend="fiber_detach"><code>fiber::detach()</code></link> is called on a fiber object, that object represents <emphasis>not-a-fiber</emphasis>. The fiber object may then safely be destroyed. </para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">some_fn</phrase><phrase role="special">).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> </programlisting> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides a number of ways to wait for a running fiber to complete. You can coordinate even with a detached fiber using a <link linkend="class_mutex"><code>mutex</code></link>, or <link linkend="class_condition_variable"><code>condition_variable</code></link>, or any of the other <link linkend="synchronization">synchronization objects</link> provided by the library. </para> <para> If a detached fiber is still running when the thread&#8217;s main fiber terminates, the thread will not shut down. </para> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h5"> <phrase id="fiber.fiber_mgmt.joining"/><link linkend="fiber.fiber_mgmt.joining">Joining</link> </bridgehead> <para> In order to wait for a fiber to finish, the <link linkend="fiber_join"><code>fiber::join()</code></link> member function of the <link linkend="class_fiber"><code>fiber</code></link> object can be used. <link linkend="fiber_join"><code>fiber::join()</code></link> will block until the <link linkend="class_fiber"><code>fiber</code></link> object has completed. </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">some_fn</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase> <phrase role="identifier">some_fn</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> <phrase role="identifier">f</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> If the fiber has already completed, then <link linkend="fiber_join"><code>fiber::join()</code></link> returns immediately and the joined <link linkend="class_fiber"><code>fiber</code></link> object becomes <emphasis>not-a-fiber</emphasis>. </para> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h6"> <phrase id="fiber.fiber_mgmt.destruction"/><link linkend="fiber.fiber_mgmt.destruction">Destruction</link> </bridgehead> <para> When a <link linkend="class_fiber"><code>fiber</code></link> object representing a valid execution context (the fiber is <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>) is destroyed, the program terminates. If you intend the fiber to outlive the <link linkend="class_fiber"><code>fiber</code></link> object that launched it, use the <link linkend="fiber_detach"><code>fiber::detach()</code></link> method. </para> <programlisting><phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase> <phrase role="identifier">some_fn</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// std::terminate() will be called</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">some_fn</phrase><phrase role="special">);</phrase> <phrase role="identifier">f</phrase><phrase role="special">.</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="comment">// okay, program continues</phrase> </programlisting> <anchor id="class_fiber_id"/> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h7"> <phrase id="fiber.fiber_mgmt.fiber_ids"/><link linkend="fiber.fiber_mgmt.fiber_ids">Fiber IDs</link> </bridgehead> <para> Objects of class <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> can be used to identify fibers. Each running <link linkend="class_fiber"><code>fiber</code></link> has a unique <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> obtainable from the corresponding <link linkend="class_fiber"><code>fiber</code></link> by calling the <link linkend="fiber_get_id"><code>fiber::get_id()</code></link> member function. Objects of class <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> can be copied, and used as keys in associative containers: the full range of comparison operators is provided. They can also be written to an output stream using the stream insertion operator, though the output format is unspecified. </para> <para> Each instance of <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> either refers to some fiber, or <emphasis>not-a-fiber</emphasis>. Instances that refer to <emphasis>not-a-fiber</emphasis> compare equal to each other, but not equal to any instances that refer to an actual fiber. The comparison operators on <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> yield a total order for every non-equal <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link>. </para> <anchor id="class_launch"/> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h8"> <phrase id="fiber.fiber_mgmt.enumeration__code__phrase_role__identifier__launch__phrase___code_"/><link linkend="fiber.fiber_mgmt.enumeration__code__phrase_role__identifier__launch__phrase___code_">Enumeration <code><phrase role="identifier">launch</phrase></code></link> </bridgehead> <para> <code><phrase role="identifier">launch</phrase></code> specifies whether control passes immediately into a newly-launched fiber. </para> <programlisting><phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">launch</phrase> <phrase role="special">{</phrase> <phrase role="identifier">dispatch</phrase><phrase role="special">,</phrase> <phrase role="identifier">post</phrase> <phrase role="special">};</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h9"> <phrase id="fiber.fiber_mgmt._code__phrase_role__identifier__dispatch__phrase___code_"/><link linkend="fiber.fiber_mgmt._code__phrase_role__identifier__dispatch__phrase___code_"><code><phrase role="identifier">dispatch</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> A fiber launched with <code><phrase role="identifier">launch</phrase> <phrase role="special">==</phrase> <phrase role="identifier">dispatch</phrase></code> is entered immediately. In other words, launching a fiber with <code><phrase role="identifier">dispatch</phrase></code> suspends the caller (the previously-running fiber) until the fiber scheduler has a chance to resume it later. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect3" id="fiber.fiber_mgmt.h10"> <phrase id="fiber.fiber_mgmt._code__phrase_role__identifier__post__phrase___code_"/><link linkend="fiber.fiber_mgmt._code__phrase_role__identifier__post__phrase___code_"><code><phrase role="identifier">post</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> A fiber launched with <code><phrase role="identifier">launch</phrase> <phrase role="special">==</phrase> <phrase role="identifier">post</phrase></code> is passed to the fiber scheduler as ready, but it is not yet entered. The caller (the previously-running fiber) continues executing. The newly-launched fiber will be entered when the fiber scheduler has a chance to resume it later. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> If <code><phrase role="identifier">launch</phrase></code> is not explicitly specified, <code><phrase role="identifier">post</phrase></code> is the default. </para> </listitem> </varlistentry> </variablelist> <section id="fiber.fiber_mgmt.fiber"> <title><anchor id="class_fiber"/><link linkend="fiber.fiber_mgmt.fiber">Class <code><phrase role="identifier">fiber</phrase></code></link></title> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> <phrase role="keyword">constexpr</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link><phrase role="special">,</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...);</phrase> <phrase role="special">~</phrase><phrase role="identifier">fiber</phrase><phrase role="special">();</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">joinable</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">join</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">SchedAlgo</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.fiber.h0"> <phrase id="fiber.fiber_mgmt.fiber.default_constructor"/><link linkend="fiber.fiber_mgmt.fiber.default_constructor">Default constructor</link> </bridgehead> <programlisting><phrase role="keyword">constexpr</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs a <link linkend="class_fiber"><code>fiber</code></link> instance that refers to <emphasis>not-a-fiber</emphasis>. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <anchor id="fiber_fiber"/> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.fiber.h1"> <phrase id="fiber.fiber_mgmt.fiber.constructor"/><link linkend="fiber.fiber_mgmt.fiber.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link> <phrase role="identifier">policy</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link> <phrase role="identifier">policy</phrase><phrase role="special">,</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">Fn</phrase></code> must be copyable or movable. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> <code><phrase role="identifier">fn</phrase></code> is copied or moved into internal storage for access by the new fiber. If <link linkend="class_launch"><code>launch</code></link> is specified (or defaulted) to <code><phrase role="identifier">post</phrase></code>, the new fiber is marked <quote>ready</quote> and will be entered at the next opportunity. If <code><phrase role="identifier">launch</phrase></code> is specified as <code><phrase role="identifier">dispatch</phrase></code>, the calling fiber is suspended and the new fiber is entered immediately. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> refers to the newly created fiber of execution. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link> is required to allocate a stack for the internal __econtext__. If <code><phrase role="identifier">StackAllocator</phrase></code> is not explicitly passed, the default stack allocator depends on <code><phrase role="identifier">BOOST_USE_SEGMENTED_STACKS</phrase></code>: if defined, you will get a <link linkend="class_segmented_stack"><code>segmented_stack</code></link>, else a <link linkend="class_fixedsize_stack"><code>fixedsize_stack</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink>, <link linkend="stack">Stack allocation</link> </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.fiber.h2"> <phrase id="fiber.fiber_mgmt.fiber.move_constructor"/><link linkend="fiber.fiber_mgmt.fiber.move_constructor">Move constructor</link> </bridgehead> <programlisting><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Transfers ownership of the fiber managed by <code><phrase role="identifier">other</phrase></code> to the newly constructed <link linkend="class_fiber"><code>fiber</code></link> instance. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> returns the value of <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> prior to the construction </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.fiber.h3"> <phrase id="fiber.fiber_mgmt.fiber.move_assignment_operator"/><link linkend="fiber.fiber_mgmt.fiber.move_assignment_operator">Move assignment operator</link> </bridgehead> <programlisting><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Transfers ownership of the fiber managed by <code><phrase role="identifier">other</phrase></code> (if any) to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="identifier">other</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> returns the value of <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> prior to the assignment. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.fiber.h4"> <phrase id="fiber.fiber_mgmt.fiber.destructor"/><link linkend="fiber.fiber_mgmt.fiber.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">fiber</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If the fiber is <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>, calls std::terminate. Destroys <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The programmer must ensure that the destructor is never executed while the fiber is still <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>. Even if you know that the fiber has completed, you must still call either <link linkend="fiber_join"><code>fiber::join()</code></link> or <link linkend="fiber_detach"><code>fiber::detach()</code></link> before destroying the <code><phrase role="identifier">fiber</phrase></code> object. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_joinable_bridgehead"> <phrase id="fiber_joinable"/> <link linkend="fiber_joinable">Member function <code>joinable</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">joinable</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> refers to a fiber of execution, which may or may not have completed; otherwise <code><phrase role="keyword">false</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_join_bridgehead"> <phrase id="fiber_join"/> <link linkend="fiber_join">Member function <code>join</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> the fiber is <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Waits for the referenced fiber of execution to complete. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> The fiber of execution referenced on entry has completed. <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> no longer refers to any fiber of execution. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code>. <emphasis role="bold">invalid_argument</emphasis>: if the fiber is not <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_detach_bridgehead"> <phrase id="fiber_detach"/> <link linkend="fiber_detach">Member function <code>detach</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">detach</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> the fiber is <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> The fiber of execution becomes detached, and no longer has an associated <link linkend="class_fiber"><code>fiber</code></link> object. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> no longer refers to any fiber of execution. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">invalid_argument</emphasis>: if the fiber is not <link linkend="fiber_joinable"><code>fiber::joinable()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_get_id_bridgehead"> <phrase id="fiber_get_id"/> <link linkend="fiber_get_id">Member function <code>get_id</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> If <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> refers to a fiber of execution, an instance of <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> that represents that fiber. Otherwise returns a default-constructed <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="this_fiber_get_id"><code>this_fiber::get_id()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_properties_bridgehead"> <phrase id="fiber_properties"/> <link linkend="fiber_properties">Templated member function <code>properties</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> refers to a fiber of execution. <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link> has been called from this thread with a subclass of <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link> with the same template argument <code><phrase role="identifier">PROPS</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> a reference to the scheduler properties instance for <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bad_cast</phrase></code> if <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code> was called with a <code><phrase role="identifier">algorithm_with_properties</phrase></code> subclass with some other template parameter than <code><phrase role="identifier">PROPS</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link> provides a way for a user-coded scheduler to associate extended properties, such as priority, with a fiber instance. This method allows access to those user-provided properties. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="custom">Customization</link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_swap_bridgehead"> <phrase id="fiber_swap"/> <link linkend="fiber_swap">Member function <code>swap</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Exchanges the fiber of execution associated with <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> and <code><phrase role="identifier">other</phrase></code>, so <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> becomes associated with the fiber formerly associated with <code><phrase role="identifier">other</phrase></code>, and vice-versa. </para> </listitem> </varlistentry> <varlistentry> <term>Postconditions:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> returns the same value as <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> prior to the call. <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> returns the same value as <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> prior to the call. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="swap_for_fiber_bridgehead"> <phrase id="swap_for_fiber"/> <link linkend="swap_for_fiber">Non-member function <code>swap()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Same as <code><phrase role="identifier">l</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="operator&lt;_bridgehead"> <phrase id="operator&lt;"/> <link linkend="operator&lt;">Non-member function <code>operator&lt;()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">l</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">r</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> is <code><phrase role="keyword">true</phrase></code>, false otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="use_scheduling_algorithm_bridgehead"> <phrase id="use_scheduling_algorithm"/> <link linkend="use_scheduling_algorithm">Non-member function <code>use_scheduling_algorithm()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">SchedAlgo</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Directs <emphasis role="bold">Boost.Fiber</emphasis> to use <code><phrase role="identifier">SchedAlgo</phrase></code>, which must be a concrete subclass of <link linkend="class_algorithm"><code>algorithm</code></link>, as the scheduling algorithm for all fibers in the current thread. Pass any required <code><phrase role="identifier">SchedAlgo</phrase></code> constructor arguments as <code><phrase role="identifier">args</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> If you want a given thread to use a non-default scheduling algorithm, make that thread call <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code> before any other <emphasis role="bold">Boost.Fiber</emphasis> entry point. If no scheduler has been set for the current thread by the time <emphasis role="bold">Boost.Fiber</emphasis> needs to use it, the library will create a default <link linkend="class_round_robin"><code>round_robin</code></link> instance for this thread. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="scheduling">Scheduling</link>, <link linkend="custom">Customization</link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="has_ready_fibers_bridgehead"> <phrase id="has_ready_fibers"/> <link linkend="has_ready_fibers">Non-member function <code>has_ready_fibers()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Can be used for work-stealing to find an idle scheduler. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.fiber_mgmt.id"> <title><anchor id="class_id"/><link linkend="fiber.fiber_mgmt.id">Class fiber::id</link></title> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">id</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">constexpr</phrase> <phrase role="identifier">id</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">friend</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;,</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.id.h0"> <phrase id="fiber.fiber_mgmt.id.constructor"/><link linkend="fiber.fiber_mgmt.id.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="keyword">constexpr</phrase> <phrase role="identifier">id</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Represents an instance of <emphasis>not-a-fiber</emphasis>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_equal_bridgehead"> <phrase id="id_operator_equal"/> <link linkend="id_operator_equal">Member function <code>operator==</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> and <code><phrase role="identifier">other</phrase></code> represent the same fiber, or both represent <emphasis>not-a-fiber</emphasis>, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_not_equal_bridgehead"> <phrase id="id_operator_not_equal"/> <link linkend="id_operator_not_equal">Member function <code>operator!=</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code>! (other == * this)</code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_less_bridgehead"> <phrase id="id_operator_less"/> <link linkend="id_operator_less">Member function <code>operator&lt;</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">other</phrase></code> is true and the implementation-defined total order of <code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code> values places <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> before <code><phrase role="identifier">other</phrase></code>, false otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_greater_bridgehead"> <phrase id="id_operator_greater"/> <link linkend="id_operator_greater">Member function <code>operator&gt;</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="identifier">other</phrase> <phrase role="special">&lt;</phrase> <phrase role="special">*</phrase> <phrase role="keyword">this</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_less_equal_bridgehead"> <phrase id="id_operator_less_equal"/> <link linkend="id_operator_less_equal">Member function <code>operator&lt;=</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="special">!</phrase> <phrase role="special">(</phrase><phrase role="identifier">other</phrase> <phrase role="special">&lt;</phrase> <phrase role="special">*</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="id_operator_greater_equal_bridgehead"> <phrase id="id_operator_greater_equal"/> <link linkend="id_operator_greater_equal">Member function <code>operator&gt;=</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="special">!</phrase> <phrase role="special">(*</phrase> <phrase role="keyword">this</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.fiber_mgmt.id.h1"> <phrase id="fiber.fiber_mgmt.id.operator_lt__lt_"/><link linkend="fiber.fiber_mgmt.id.operator_lt__lt_">operator&lt;&lt;</link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">os</phrase><phrase role="special">,</phrase> <phrase role="identifier">id</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Efects:</term> <listitem> <para> Writes the representation of <code><phrase role="identifier">other</phrase></code> to stream <code><phrase role="identifier">os</phrase></code>. The representation is unspecified. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="identifier">os</phrase></code> </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.fiber_mgmt.this_fiber"> <title><link linkend="fiber.fiber_mgmt.this_fiber">Namespace this_fiber</link></title> <para> In general, <code><phrase role="identifier">this_fiber</phrase></code> operations may be called from the <quote>main</quote> fiber &mdash; the fiber on which function <code><phrase role="identifier">main</phrase><phrase role="special">()</phrase></code> is entered &mdash; as well as from an explicitly-launched thread&#8217;s thread-function. That is, in many respects the main fiber on each thread can be treated like an explicitly-launched fiber. </para> <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">this_fiber</phrase> <phrase role="special">{</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">yield</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">();</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="this_fiber_get_id_bridgehead"> <phrase id="this_fiber_get_id"/> <link linkend="this_fiber_get_id">Non-member function <code>this_fiber::get_id()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">operations</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> An instance of <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> that represents the currently executing fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="this_fiber_sleep_until_bridgehead"> <phrase id="this_fiber_sleep_until"/> <link linkend="this_fiber_sleep_until">Non-member function <code>this_fiber::sleep_until()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">operations</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">);</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Suspends the current fiber until the time point specified by <code><phrase role="identifier">abs_time</phrase></code> has been reached. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The current fiber will not resume before <code><phrase role="identifier">abs_time</phrase></code>, but there are no guarantees about how soon after <code><phrase role="identifier">abs_time</phrase></code> it might resume. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <quote>timeout-related exceptions</quote> are as defined in the C++ Standard, section <emphasis role="bold">30.2.4 Timing specifications [thread.req.timing]</emphasis>: <quote>A function that takes an argument which specifies a timeout will throw if, during its execution, a clock, time point, or time duration throws an exception. Such exceptions are referred to as <emphasis>timeout-related exceptions.</emphasis></quote> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="this_fiber_sleep_for_bridgehead"> <phrase id="this_fiber_sleep_for"/> <link linkend="this_fiber_sleep_for">Non-member function <code>this_fiber::sleep_for()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">operations</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">sleep_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">);</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Suspends the current fiber until the time duration specified by <code><phrase role="identifier">rel_time</phrase></code> has elapsed. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The current fiber will not resume before <code><phrase role="identifier">rel_time</phrase></code> has elapsed, but there are no guarantees about how soon after that it might resume. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="this_fiber_yield_bridgehead"> <phrase id="this_fiber_yield"/> <link linkend="this_fiber_yield">Non-member function <code>this_fiber::yield()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">operations</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">yield</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Relinquishes execution control, allowing other fibers to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> A fiber that calls <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> is not suspended: it is immediately passed to the scheduler as ready to run. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="this_fiber_properties_bridgehead"> <phrase id="this_fiber_properties"/> <link linkend="this_fiber_properties">Non-member function <code>this_fiber::properties()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">operations</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">();</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link> has been called from this thread with a subclass of <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link> with the same template argument <code><phrase role="identifier">PROPS</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> a reference to the scheduler properties instance for the currently running fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bad_cast</phrase></code> if <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code> was called with an <code><phrase role="identifier">algorithm_with_properties</phrase></code> subclass with some other template parameter than <code><phrase role="identifier">PROPS</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link> provides a way for a user-coded scheduler to associate extended properties, such as priority, with a fiber instance. This function allows access to those user-provided properties. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The first time this function is called from the main fiber of a thread, it may internally yield, permitting other fibers to run. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="custom">Customization</link> </para> </listitem> </varlistentry> </variablelist> </section> </section> <section id="fiber.scheduling"> <title><anchor id="scheduling"/><link linkend="fiber.scheduling">Scheduling</link></title> <para> The fibers in a thread are coordinated by a fiber manager. Fibers trade control cooperatively, rather than preemptively: the currently-running fiber retains control until it invokes some operation that passes control to the manager. Each time a fiber suspends (or yields), the fiber manager consults a scheduler to determine which fiber will run next. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides the fiber manager, but the scheduler is a customization point. (See <link linkend="custom">Customization</link>.) </para> <para> Each thread has its own scheduler. Different threads in a process may use different schedulers. By default, <emphasis role="bold">Boost.Fiber</emphasis> implicitly instantiates <link linkend="class_round_robin"><code>round_robin</code></link> as the scheduler for each thread. </para> <para> You are explicitly permitted to code your own <link linkend="class_algorithm"><code>algorithm</code></link> subclass. For the most part, your <code><phrase role="identifier">algorithm</phrase></code> subclass need not defend against cross-thread calls: the fiber manager intercepts and defers such calls. Most <code><phrase role="identifier">algorithm</phrase></code> methods are only ever directly called from the thread whose fibers it is managing &mdash; with exceptions as documented below. </para> <para> Your <code><phrase role="identifier">algorithm</phrase></code> subclass is engaged on a particular thread by calling <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link>: </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">thread_fn</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">my_fiber_scheduler</phrase> <phrase role="special">&gt;();</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> </programlisting> <para> A scheduler class must implement interface <link linkend="class_algorithm"><code>algorithm</code></link>. <emphasis role="bold">Boost.Fiber</emphasis> provides schedulers: <link linkend="class_round_robin"><code>round_robin</code></link>, <link linkend="class_work_stealing"><code>work_stealing</code></link>, <link linkend="class_numa_work_stealing"><code>numa::work_stealing</code></link> and <link linkend="class_shared_work"><code>shared_work</code></link>. </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// thread registers itself at work-stealing scheduler</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> <phrase role="comment">// count of logical cpus</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">thread_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">hardware_concurrency</phrase><phrase role="special">();</phrase> <phrase role="comment">// start worker-threads first</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">threads</phrase><phrase role="special">;</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">1</phrase> <phrase role="comment">/* count start-thread */</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// spawn thread</phrase> <phrase role="identifier">threads</phrase><phrase role="special">.</phrase><phrase role="identifier">emplace_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">thread</phrase><phrase role="special">,</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// start-thread registers itself at work-stealing scheduler</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> </programlisting> <para> The example spawns as many threads as <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">hardware_concurrency</phrase><phrase role="special">()</phrase></code> returns. Each thread runs a <link linkend="class_work_stealing"><code>work_stealing</code></link> scheduler. Each instance of this scheduler needs to know how many threads run the work-stealing scheduler in the program. If the local queue of one thread runs out of ready fibers, the thread tries to steal a ready fiber from another thread running this scheduler. </para> <para> <bridgehead renderas="sect4" id="class_algorithm_bridgehead"> <phrase id="class_algorithm"/> <link linkend="class_algorithm">Class <code>algorithm</code></link> </bridgehead> </para> <para> <code><phrase role="identifier">algorithm</phrase></code> is the abstract base class defining the interface that a fiber scheduler must implement. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">algorithm</phrase> <phrase role="special">{</phrase> <phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">();</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="algorithm_awakened_bridgehead"> <phrase id="algorithm_awakened"/> <link linkend="algorithm_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs the scheduler that fiber <code><phrase role="identifier">f</phrase></code> is ready to run. Fiber <code><phrase role="identifier">f</phrase></code> might be newly launched, or it might have been blocked but has just been awakened, or it might have called <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This method advises the scheduler to add fiber <code><phrase role="identifier">f</phrase></code> to its collection of fibers ready to run. A typical scheduler implementation places <code><phrase role="identifier">f</phrase></code> into a queue. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="class_round_robin"><code>round_robin</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_pick_next_bridgehead"> <phrase id="algorithm_pick_next"/> <link linkend="algorithm_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber which is to be resumed next, or <code><phrase role="keyword">nullptr</phrase></code> if there is no ready fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This is where the scheduler actually specifies the fiber which is to run next. A typical scheduler implementation chooses the head of the ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="class_round_robin"><code>round_robin</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_has_ready_fibers_bridgehead"> <phrase id="algorithm_has_ready_fibers"/> <link linkend="algorithm_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_suspend_until_bridgehead"> <phrase id="algorithm_suspend_until"/> <link linkend="algorithm_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs the scheduler that no fiber will be ready until time-point <code><phrase role="identifier">abs_time</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This method allows a custom scheduler to yield control to the containing environment in whatever way makes sense. The fiber manager is stating that <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> need not return until <code><phrase role="identifier">abs_time</phrase></code> &mdash; or <link linkend="algorithm_notify"><code>algorithm::notify()</code></link> is called &mdash; whichever comes first. The interaction with <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> means that, for instance, calling <ulink url="http://en.cppreference.com/w/cpp/thread/sleep_until"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">sleep_until</phrase><phrase role="special">(</phrase><phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase></code></ulink> would be too simplistic. <link linkend="round_robin_suspend_until"><code>round_robin::suspend_until()</code></link> uses a <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase></code></ulink> to coordinate with <link linkend="round_robin_notify"><code>round_robin::notify()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Given that <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> might be called from another thread, your <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> implementation &mdash; like the rest of your <code><phrase role="identifier">algorithm</phrase></code> implementation &mdash; must guard any data it shares with your <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> implementation. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_notify_bridgehead"> <phrase id="algorithm_notify"/> <link linkend="algorithm_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Requests the scheduler to return from a pending call to <link linkend="algorithm_suspend_until"><code>algorithm::suspend_until()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Alone among the <code><phrase role="identifier">algorithm</phrase></code> methods, <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> may be called from another thread. Your <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> implementation must guard any data it shares with the rest of your <code><phrase role="identifier">algorithm</phrase></code> implementation. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_round_robin_bridgehead"> <phrase id="class_round_robin"/> <link linkend="class_round_robin">Class <code>round_robin</code></link> </bridgehead> </para> <para> This class implements <link linkend="class_algorithm"><code>algorithm</code></link>, scheduling fibers in round-robin fashion. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">round_robin</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">round_robin</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">algorithm</phrase> <phrase role="special">{</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="round_robin_awakened_bridgehead"> <phrase id="round_robin_awakened"/> <link linkend="round_robin_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Enqueues fiber <code><phrase role="identifier">f</phrase></code> onto a ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="round_robin_pick_next_bridgehead"> <phrase id="round_robin_pick_next"/> <link linkend="round_robin_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber at the head of the ready queue, or <code><phrase role="keyword">nullptr</phrase></code> if the queue is empty. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Placing ready fibers onto the tail of a queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="round_robin_has_ready_fibers_bridgehead"> <phrase id="round_robin_has_ready_fibers"/> <link linkend="round_robin_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="round_robin_suspend_until_bridgehead"> <phrase id="round_robin_suspend_until"/> <link linkend="round_robin_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs <code><phrase role="identifier">round_robin</phrase></code> that no ready fiber will be available until time-point <code><phrase role="identifier">abs_time</phrase></code>. This implementation blocks in <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="round_robin_notify_bridgehead"> <phrase id="round_robin_notify"/> <link linkend="round_robin_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Wake up a pending call to <link linkend="round_robin_suspend_until"><code>round_robin::suspend_until()</code></link>, some fibers might be ready. This implementation wakes <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> via <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_work_stealing_bridgehead"> <phrase id="class_work_stealing"/> <link linkend="class_work_stealing">Class <code>work_stealing</code></link> </bridgehead> </para> <para> This class implements <link linkend="class_algorithm"><code>algorithm</code></link>; if the local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers.<sbr/> The victim scheduler (from which a ready fiber is stolen) is selected at random. </para> <note> <para> Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported. </para> </note> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">work_stealing</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">algorithm</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">suspend</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">);</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.scheduling.h0"> <phrase id="fiber.scheduling.constructor"/><link linkend="fiber.scheduling.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">thread_count</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">suspend</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs work-stealing scheduling algorithm. <code><phrase role="identifier">thread_count</phrase></code> represents the number of threads running this algorithm. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">system_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> If <code><phrase role="identifier">suspend</phrase></code> is set to <code><phrase role="keyword">true</phrase></code>, then the scheduler suspends if no ready fiber could be stolen. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote (other thread or fiber scheduler). </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="work_stealing_awakened_bridgehead"> <phrase id="work_stealing_awakened"/> <link linkend="work_stealing_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Enqueues fiber <code><phrase role="identifier">f</phrase></code> onto the shared ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="work_stealing_pick_next_bridgehead"> <phrase id="work_stealing_pick_next"/> <link linkend="work_stealing_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber at the head of the ready queue, or <code><phrase role="keyword">nullptr</phrase></code> if the queue is empty. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Placing ready fibers onto the tail of the sahred queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="work_stealing_has_ready_fibers_bridgehead"> <phrase id="work_stealing_has_ready_fibers"/> <link linkend="work_stealing_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="work_stealing_suspend_until_bridgehead"> <phrase id="work_stealing_suspend_until"/> <link linkend="work_stealing_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs <code><phrase role="identifier">work_stealing</phrase></code> that no ready fiber will be available until time-point <code><phrase role="identifier">abs_time</phrase></code>. This implementation blocks in <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="work_stealing_notify_bridgehead"> <phrase id="work_stealing_notify"/> <link linkend="work_stealing_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Wake up a pending call to <link linkend="work_stealing_suspend_until"><code>work_stealing::suspend_until()</code></link>, some fibers might be ready. This implementation wakes <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> via <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_shared_work_bridgehead"> <phrase id="class_shared_work"/> <link linkend="class_shared_work">Class <code>shared_work</code></link> </bridgehead> </para> <note> <para> Because of the non-locality of data, <emphasis>shared_work</emphasis> is less performant than <link linkend="class_work_stealing"><code>work_stealing</code></link>. </para> </note> <para> This class implements <link linkend="class_algorithm"><code>algorithm</code></link>, scheduling fibers in round-robin fashion. Ready fibers are shared between all instances (running on different threads) of shared_work, thus the work is distributed equally over all threads. </para> <note> <para> Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported. </para> </note> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">shared_work</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">shared_work</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">algorithm</phrase> <phrase role="special">{</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="shared_work_awakened_bridgehead"> <phrase id="shared_work_awakened"/> <link linkend="shared_work_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Enqueues fiber <code><phrase role="identifier">f</phrase></code> onto the shared ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_work_pick_next_bridgehead"> <phrase id="shared_work_pick_next"/> <link linkend="shared_work_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber at the head of the ready queue, or <code><phrase role="keyword">nullptr</phrase></code> if the queue is empty. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Placing ready fibers onto the tail of the shared queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_work_has_ready_fibers_bridgehead"> <phrase id="shared_work_has_ready_fibers"/> <link linkend="shared_work_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_work_suspend_until_bridgehead"> <phrase id="shared_work_suspend_until"/> <link linkend="shared_work_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs <code><phrase role="identifier">shared_work</phrase></code> that no ready fiber will be available until time-point <code><phrase role="identifier">abs_time</phrase></code>. This implementation blocks in <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_work_notify_bridgehead"> <phrase id="shared_work_notify"/> <link linkend="shared_work_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Wake up a pending call to <link linkend="shared_work_suspend_until"><code>shared_work::suspend_until()</code></link>, some fibers might be ready. This implementation wakes <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> via <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect3" id="fiber.scheduling.h1"> <phrase id="fiber.scheduling.custom_scheduler_fiber_properties"/><link linkend="fiber.scheduling.custom_scheduler_fiber_properties">Custom Scheduler Fiber Properties</link> </bridgehead> <para> A scheduler class directly derived from <link linkend="class_algorithm"><code>algorithm</code></link> can use any information available from <link linkend="class_context"><code>context</code></link> to implement the <code><phrase role="identifier">algorithm</phrase></code> interface. But a custom scheduler might need to track additional properties for a fiber. For instance, a priority-based scheduler would need to track a fiber&#8217;s priority. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides a mechanism by which your custom scheduler can associate custom properties with each fiber. </para> <para> <bridgehead renderas="sect4" id="class_fiber_properties_bridgehead"> <phrase id="class_fiber_properties"/> <link linkend="class_fiber_properties">Class <code>fiber_properties</code></link> </bridgehead> </para> <para> A custom fiber properties class must be derived from <code><phrase role="identifier">fiber_properties</phrase></code>. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">properties</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">fiber_properties</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">fiber_properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">fiber_properties</phrase><phrase role="special">();</phrase> <phrase role="keyword">protected</phrase><phrase role="special">:</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.scheduling.h2"> <phrase id="fiber.scheduling.constructor0"/><link linkend="fiber.scheduling.constructor0">Constructor</link> </bridgehead> <programlisting><phrase role="identifier">fiber_properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs base-class component of custom subclass. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Your subclass constructor must accept a <code><phrase role="identifier">context</phrase><phrase role="special">*</phrase></code> and pass it to the base-class <code><phrase role="identifier">fiber_properties</phrase></code> constructor. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_properties_notify_bridgehead"> <phrase id="fiber_properties_notify"/> <link linkend="fiber_properties_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Pass control to the custom <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link> subclass&#8217;s <link linkend="algorithm_with_properties_property_change"><code>algorithm_with_properties::property_change()</code></link> method. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> A custom scheduler&#8217;s <link linkend="algorithm_with_properties_pick_next"><code>algorithm_with_properties::pick_next()</code></link> method might dynamically select from the ready fibers, or <link linkend="algorithm_with_properties_awakened"><code>algorithm_with_properties::awakened()</code></link> might instead insert each ready fiber into some form of ready queue for <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code>. In the latter case, if application code modifies a fiber property (e.g. priority) that should affect that fiber&#8217;s relationship to other ready fibers, the custom scheduler must be given the opportunity to reorder its ready queue. The custom property subclass should implement an access method to modify such a property; that access method should call <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> once the new property value has been stored. This passes control to the custom scheduler&#8217;s <code><phrase role="identifier">property_change</phrase><phrase role="special">()</phrase></code> method, allowing the custom scheduler to reorder its ready queue appropriately. Use at your discretion. Of course, if you define a property which does not affect the behavior of the <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> method, you need not call <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> when that property is modified. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_algorithm_with_properties_bridgehead"> <phrase id="class_algorithm_with_properties"/> <link linkend="class_algorithm_with_properties">Template <code>algorithm_with_properties&lt;&gt;</code></link> </bridgehead> </para> <para> A custom scheduler that depends on a custom properties class <code><phrase role="identifier">PROPS</phrase></code> should be derived from <code><phrase role="identifier">algorithm_with_properties</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">PROPS</phrase><phrase role="special">&gt;</phrase></code>. <code><phrase role="identifier">PROPS</phrase></code> should be derived from <link linkend="class_fiber_properties"><code>fiber_properties</code></link>. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">algorithm_with_properties</phrase> <phrase role="special">{</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*,</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">property_change</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*,</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">fiber_properties</phrase> <phrase role="special">*</phrase> <phrase role="identifier">new_properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_awakened_bridgehead"> <phrase id="algorithm_with_properties_awakened"/> <link linkend="algorithm_with_properties_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs the scheduler that fiber <code><phrase role="identifier">f</phrase></code> is ready to run, like <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link>. Passes the fiber&#8217;s associated <code><phrase role="identifier">PROPS</phrase></code> instance. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> An <code><phrase role="identifier">algorithm_with_properties</phrase><phrase role="special">&lt;&gt;</phrase></code> subclass must override this method instead of <code><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_pick_next_bridgehead"> <phrase id="algorithm_with_properties_pick_next"/> <link linkend="algorithm_with_properties_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber which is to be resumed next, or <code><phrase role="keyword">nullptr</phrase></code> if there is no ready fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> same as <link linkend="algorithm_pick_next"><code>algorithm::pick_next()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_has_ready_fibers_bridgehead"> <phrase id="algorithm_with_properties_has_ready_fibers"/> <link linkend="algorithm_with_properties_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> same as <link linkend="algorithm_has_ready_fibers"><code>algorithm::has_ready_fibers()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_suspend_until_bridgehead"> <phrase id="algorithm_with_properties_suspend_until"/> <link linkend="algorithm_with_properties_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs the scheduler that no fiber will be ready until time-point <code><phrase role="identifier">abs_time</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> same as <link linkend="algorithm_suspend_until"><code>algorithm::suspend_until()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_notify_bridgehead"> <phrase id="algorithm_with_properties_notify"/> <link linkend="algorithm_with_properties_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Requests the scheduler to return from a pending call to <link linkend="algorithm_with_properties_suspend_until"><code>algorithm_with_properties::suspend_until()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> same as <link linkend="algorithm_notify"><code>algorithm::notify()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_properties_bridgehead"> <phrase id="algorithm_with_properties_properties"/> <link linkend="algorithm_with_properties_properties">Member function <code>properties</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">PROPS</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the <code><phrase role="identifier">PROPS</phrase></code> instance associated with fiber <code><phrase role="identifier">f</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The fiber&#8217;s associated <code><phrase role="identifier">PROPS</phrase></code> instance is already passed to <link linkend="algorithm_with_properties_awakened"><code>algorithm_with_properties::awakened()</code></link> and <link linkend="algorithm_with_properties_property_change"><code>algorithm_with_properties::property_change()</code></link>. However, every <link linkend="class_algorithm"><code>algorithm</code></link> subclass is expected to track a collection of ready <link linkend="class_context"><code>context</code></link> instances. This method allows your custom scheduler to retrieve the <link linkend="class_fiber_properties"><code>fiber_properties</code></link> subclass instance for any <code><phrase role="identifier">context</phrase></code> in its collection. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_property_change_bridgehead"> <phrase id="algorithm_with_properties_property_change"/> <link linkend="algorithm_with_properties_property_change">Member function <code>property_change</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">property_change</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">PROPS</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">properties</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Notify the custom scheduler of a possibly-relevant change to a property belonging to fiber <code><phrase role="identifier">f</phrase></code>. <code><phrase role="identifier">properties</phrase></code> contains the new values of all relevant properties. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This method is only called when a custom <link linkend="class_fiber_properties"><code>fiber_properties</code></link> subclass explicitly calls <link linkend="fiber_properties_notify"><code>fiber_properties::notify()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="algorithm_with_properties_new_properties_bridgehead"> <phrase id="algorithm_with_properties_new_properties"/> <link linkend="algorithm_with_properties_new_properties">Member function <code>new_properties</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">fiber_properties</phrase> <phrase role="special">*</phrase> <phrase role="identifier">new_properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> A new instance of <link linkend="class_fiber_properties"><code>fiber_properties</code></link> subclass <code><phrase role="identifier">PROPS</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> By default, <code><phrase role="identifier">algorithm_with_properties</phrase><phrase role="special">&lt;&gt;::</phrase><phrase role="identifier">new_properties</phrase><phrase role="special">()</phrase></code> simply returns <code><phrase role="keyword">new</phrase> <phrase role="identifier">PROPS</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase></code>, placing the <code><phrase role="identifier">PROPS</phrase></code> instance on the heap. Override this method to allocate <code><phrase role="identifier">PROPS</phrase></code> some other way. The returned <code><phrase role="identifier">fiber_properties</phrase></code> pointer must point to the <code><phrase role="identifier">PROPS</phrase></code> instance to be associated with fiber <code><phrase role="identifier">f</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <anchor id="context"/><bridgehead renderas="sect4" id="class_context_bridgehead"> <phrase id="class_context"/> <link linkend="class_context">Class <code>context</code></link> </bridgehead> </para> <para> While you are free to treat <code><phrase role="identifier">context</phrase><phrase role="special">*</phrase></code> as an opaque token, certain <code><phrase role="identifier">context</phrase></code> members may be useful to a custom scheduler implementation. </para> <para> <anchor id="ready_queue_t"/>Of particular note is the fact that <code><phrase role="identifier">context</phrase></code> contains a hook to participate in a <ulink url="http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">intrusive</phrase><phrase role="special">::</phrase><phrase role="identifier">list</phrase></code></ulink> <literal>typedef</literal>&#8217;ed as <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">scheduler</phrase><phrase role="special">::</phrase><phrase role="identifier">ready_queue_t</phrase></code>. This hook is reserved for use by <link linkend="class_algorithm"><code>algorithm</code></link> implementations. (For instance, <link linkend="class_round_robin"><code>round_robin</code></link> contains a <code><phrase role="identifier">ready_queue_t</phrase></code> instance to manage its ready fibers.) See <link linkend="context_ready_is_linked"><code>context::ready_is_linked()</code></link>, <link linkend="context_ready_link"><code>context::ready_link()</code></link>, <link linkend="context_ready_unlink"><code>context::ready_unlink()</code></link>. </para> <para> Your <code><phrase role="identifier">algorithm</phrase></code> implementation may use any container you desire to manage passed <code><phrase role="identifier">context</phrase></code> instances. <code><phrase role="identifier">ready_queue_t</phrase></code> avoids some of the overhead of typical STL containers. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">type</phrase> <phrase role="special">{</phrase> <phrase role="identifier">none</phrase> <phrase role="special">=</phrase> <emphasis>unspecified</emphasis><phrase role="special">,</phrase> <phrase role="identifier">main_context</phrase> <phrase role="special">=</phrase> <emphasis>unspecified</emphasis><phrase role="special">,</phrase> <phrase role="comment">// fiber associated with thread's stack</phrase> <phrase role="identifier">dispatcher_context</phrase> <phrase role="special">=</phrase> <emphasis>unspecified</emphasis><phrase role="special">,</phrase> <phrase role="comment">// special fiber for maintenance operations</phrase> <phrase role="identifier">worker_context</phrase> <phrase role="special">=</phrase> <emphasis>unspecified</emphasis><phrase role="special">,</phrase> <phrase role="comment">// fiber not special to the library</phrase> <phrase role="identifier">pinned_context</phrase> <phrase role="special">=</phrase> <emphasis>unspecified</emphasis> <phrase role="comment">// fiber must not be migrated to another thread</phrase> <phrase role="special">};</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">context</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> <phrase role="keyword">static</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">active</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">context</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">context</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">detach</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">attach</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">is_context</phrase><phrase role="special">(</phrase> <phrase role="identifier">type</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">is_terminated</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">ready_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">remote_ready_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">ready_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">remote_ready_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">ready_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">remote_ready_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">schedule</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="context_active_bridgehead"> <phrase id="context_active"/> <link linkend="context_active">Static member function <code>active</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">static</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">active</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> Pointer to instance of current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_get_id_bridgehead"> <phrase id="context_get_id"/> <link linkend="context_get_id">Member function <code>get_id</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> If <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> refers to a fiber of execution, an instance of <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link> that represents that fiber. Otherwise returns a default-constructed <link linkend="class_fiber_id"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <link linkend="fiber_get_id"><code>fiber::get_id()</code></link> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_attach_bridgehead"> <phrase id="context_attach"/> <link linkend="context_attach">Member function <code>attach</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">attach</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_scheduler</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="keyword">nullptr</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Attach fiber <code><phrase role="identifier">f</phrase></code> to scheduler running <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_scheduler</phrase><phrase role="special">()</phrase> <phrase role="special">!=</phrase> <phrase role="keyword">nullptr</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> A typical call: <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">active</phrase><phrase role="special">()-&gt;</phrase><phrase role="identifier">attach</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">);</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">f</phrase></code> must not be the running fiber&#8217;s context. It must not be <link linkend="blocking"><emphasis>blocked</emphasis></link> or terminated. It must not be a <code><phrase role="identifier">pinned_context</phrase></code>. It must be currently detached. It must not currently be linked into an <link linkend="class_algorithm"><code>algorithm</code></link> implementation&#8217;s ready queue. Most of these conditions are implied by <code><phrase role="identifier">f</phrase></code> being owned by an <code><phrase role="identifier">algorithm</phrase></code> implementation: that is, it has been passed to <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link> but has not yet been returned by <link linkend="algorithm_pick_next"><code>algorithm::pick_next()</code></link>. Typically a <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> implementation would call <code><phrase role="identifier">attach</phrase><phrase role="special">()</phrase></code> with the <code><phrase role="identifier">context</phrase><phrase role="special">*</phrase></code> it is about to return. It must first remove <code><phrase role="identifier">f</phrase></code> from its ready queue. You should never pass a <code><phrase role="identifier">pinned_context</phrase></code> to <code><phrase role="identifier">attach</phrase><phrase role="special">()</phrase></code> because you should never have called its <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> method in the first place. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_detach_bridgehead"> <phrase id="context_detach"/> <link linkend="context_detach">Member function <code>detach</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">detach</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_scheduler</phrase><phrase role="special">()</phrase> <phrase role="special">!=</phrase> <phrase role="keyword">nullptr</phrase><phrase role="special">)</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">!</phrase> <phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">is_context</phrase><phrase role="special">(</phrase><phrase role="identifier">pinned_context</phrase><phrase role="special">)</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Detach fiber <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> from its scheduler running <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_scheduler</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="keyword">nullptr</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This method must be called on the thread with which the fiber is currently associated. <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> must not be the running fiber&#8217;s context. It must not be <link linkend="blocking"><emphasis>blocked</emphasis></link> or terminated. It must not be a <code><phrase role="identifier">pinned_context</phrase></code>. It must not be detached already. It must not already be linked into an <link linkend="class_algorithm"><code>algorithm</code></link> implementation&#8217;s ready queue. Most of these conditions are implied by <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> being passed to <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link>; an <code><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code> implementation must, however, test for <code><phrase role="identifier">pinned_context</phrase></code>. It must call <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> <emphasis>before</emphasis> linking <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> into its ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> In particular, it is erroneous to attempt to migrate a fiber from one thread to another by calling both <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">attach</phrase><phrase role="special">()</phrase></code> in the <link linkend="algorithm_pick_next"><code>algorithm::pick_next()</code></link> method. <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> is called on the intended destination thread. <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> must be called on the fiber&#8217;s original thread. You must call <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> in the corresponding <code><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code> method. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Unless you intend make a fiber available for potential migration to a different thread, you should call neither <code><phrase role="identifier">detach</phrase><phrase role="special">()</phrase></code> nor <code><phrase role="identifier">attach</phrase><phrase role="special">()</phrase></code> with its <code><phrase role="identifier">context</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_is_context_bridgehead"> <phrase id="context_is_context"/> <link linkend="context_is_context">Member function <code>is_context</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">is_context</phrase><phrase role="special">(</phrase> <phrase role="identifier">type</phrase> <phrase role="identifier">t</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is of the specified type. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">type</phrase><phrase role="special">::</phrase><phrase role="identifier">worker_context</phrase></code> here means any fiber not special to the library. For <code><phrase role="identifier">type</phrase><phrase role="special">::</phrase><phrase role="identifier">main_context</phrase></code> the <code><phrase role="identifier">context</phrase></code> is associated with the <quote>main</quote> fiber of the thread: the one implicitly created by the thread itself, rather than one explicitly created by <emphasis role="bold">Boost.Fiber</emphasis>. For <code><phrase role="identifier">type</phrase><phrase role="special">::</phrase><phrase role="identifier">dispatcher_context</phrase></code> the <code><phrase role="identifier">context</phrase></code> is associated with a <quote>dispatching</quote> fiber, responsible for dispatching awakened fibers to a scheduler&#8217;s ready-queue. The <quote>dispatching</quote> fiber is an implementation detail of the fiber manager. The context of the <quote>main</quote> or <quote>dispatching</quote> fiber &mdash; any fiber for which <code><phrase role="identifier">is_context</phrase><phrase role="special">(</phrase><phrase role="identifier">pinned_context</phrase><phrase role="special">)</phrase></code> is <code><phrase role="keyword">true</phrase></code> &mdash; must never be passed to <link linkend="context_detach"><code>context::detach()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_is_terminated_bridgehead"> <phrase id="context_is_terminated"/> <link linkend="context_is_terminated">Member function <code>is_terminated</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">is_terminated</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is no longer a valid context. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The <code><phrase role="identifier">context</phrase></code> has returned from its fiber-function and is no longer considered a valid context. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_ready_is_linked_bridgehead"> <phrase id="context_ready_is_linked"/> <link linkend="context_ready_is_linked">Member function <code>ready_is_linked</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">ready_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is stored in an <link linkend="class_algorithm"><code>algorithm</code></link> implementation&#8217;s ready-queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Specifically, this method indicates whether <link linkend="context_ready_link"><code>context::ready_link()</code></link> has been called on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. <code><phrase role="identifier">ready_is_linked</phrase><phrase role="special">()</phrase></code> has no information about participation in any other containers. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_remote_ready_is_linked_bridgehead"> <phrase id="context_remote_ready_is_linked"/> <link linkend="context_remote_ready_is_linked">Member function <code>remote_ready_is_linked</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">remote_ready_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is stored in the fiber manager&#8217;s remote-ready-queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> A <code><phrase role="identifier">context</phrase></code> signaled as ready by another thread is first stored in the fiber manager&#8217;s remote-ready-queue. This is the mechanism by which the fiber manager protects an <link linkend="class_algorithm"><code>algorithm</code></link> implementation from cross-thread <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link> calls. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_wait_is_linked_bridgehead"> <phrase id="context_wait_is_linked"/> <link linkend="context_wait_is_linked">Member function <code>wait_is_linked</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">wait_is_linked</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is stored in the wait-queue of some synchronization object. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The <code><phrase role="identifier">context</phrase></code> of a fiber waiting on a synchronization object (e.g. <code><phrase role="identifier">mutex</phrase></code>, <code><phrase role="identifier">condition_variable</phrase></code> etc.) is stored in the wait-queue of that synchronization object. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_ready_link_bridgehead"> <phrase id="context_ready_link"/> <link linkend="context_ready_link">Member function <code>ready_link</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">ready_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">lst</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Stores <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in ready-queue <code><phrase role="identifier">lst</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Argument <code><phrase role="identifier">lst</phrase></code> must be a doubly-linked list from <ulink url="http://www.boost.org/doc/libs/release/libs/intrusive/index.html">Boost.Intrusive</ulink>, e.g. an instance of <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">scheduler</phrase><phrase role="special">::</phrase><phrase role="identifier">ready_queue_t</phrase></code>. Specifically, it must be a <ulink url="http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">intrusive</phrase><phrase role="special">::</phrase><phrase role="identifier">list</phrase></code></ulink> compatible with the <code><phrase role="identifier">list_member_hook</phrase></code> stored in the <code><phrase role="identifier">context</phrase></code> object. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_remote_ready_link_bridgehead"> <phrase id="context_remote_ready_link"/> <link linkend="context_remote_ready_link">Member function <code>remote_ready_link</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">remote_ready_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">lst</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Stores <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in remote-ready-queue <code><phrase role="identifier">lst</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Argument <code><phrase role="identifier">lst</phrase></code> must be a doubly-linked list from <ulink url="http://www.boost.org/doc/libs/release/libs/intrusive/index.html">Boost.Intrusive</ulink>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_wait_link_bridgehead"> <phrase id="context_wait_link"/> <link linkend="context_wait_link">Member function <code>wait_link</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_link</phrase><phrase role="special">(</phrase> <phrase role="identifier">List</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">lst</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Stores <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in wait-queue <code><phrase role="identifier">lst</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Argument <code><phrase role="identifier">lst</phrase></code> must be a doubly-linked list from <ulink url="http://www.boost.org/doc/libs/release/libs/intrusive/index.html">Boost.Intrusive</ulink>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_ready_unlink_bridgehead"> <phrase id="context_ready_unlink"/> <link linkend="context_ready_unlink">Member function <code>ready_unlink</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">ready_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Removes <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> from ready-queue: undoes the effect of <link linkend="context_ready_link"><code>context::ready_link()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_remote_ready_unlink_bridgehead"> <phrase id="context_remote_ready_unlink"/> <link linkend="context_remote_ready_unlink">Member function <code>remote_ready_unlink</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">remote_ready_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Removes <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> from remote-ready-queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_wait_unlink_bridgehead"> <phrase id="context_wait_unlink"/> <link linkend="context_wait_unlink">Member function <code>wait_unlink</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">wait_unlink</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Removes <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> from wait-queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_suspend_bridgehead"> <phrase id="context_suspend"/> <link linkend="context_suspend">Member function <code>suspend</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">suspend</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Suspends the running fiber (the fiber associated with <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>) until some other fiber passes <code><phrase role="keyword">this</phrase></code> to <link linkend="context_schedule"><code>context::schedule()</code></link>. <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is marked as not-ready, and control passes to the scheduler to select another fiber to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This is a low-level API potentially useful for integration with other frameworks. It is not intended to be directly invoked by a typical application program. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The burden is on the caller to arrange for a call to <code><phrase role="identifier">schedule</phrase><phrase role="special">()</phrase></code> with a pointer to <code><phrase role="keyword">this</phrase></code> at some future time. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_schedule_bridgehead"> <phrase id="context_schedule"/> <link linkend="context_schedule">Member function <code>schedule</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">schedule</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase> <phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Mark the fiber associated with context <code><phrase role="special">*</phrase><phrase role="identifier">ctx</phrase></code> as being ready to run. This does not immediately resume that fiber; rather it passes the fiber to the scheduler for subsequent resumption. If the scheduler is idle (has not returned from a call to <link linkend="algorithm_suspend_until"><code>algorithm::suspend_until()</code></link>), <link linkend="algorithm_notify"><code>algorithm::notify()</code></link> is called to wake it up. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This is a low-level API potentially useful for integration with other frameworks. It is not intended to be directly invoked by a typical application program. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> It is explicitly supported to call <code><phrase role="identifier">schedule</phrase><phrase role="special">(</phrase><phrase role="identifier">ctx</phrase><phrase role="special">)</phrase></code> from a thread other than the one on which <code><phrase role="special">*</phrase><phrase role="identifier">ctx</phrase></code> is currently suspended. The corresponding fiber will be resumed on its original thread in due course. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="context_less_bridgehead"> <phrase id="context_less"/> <link linkend="context_less">Non-member function <code>operator&lt;()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">context</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">l</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">r</phrase><phrase role="special">.</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> is <code><phrase role="keyword">true</phrase></code>, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.stack"> <title><anchor id="stack"/><link linkend="fiber.stack">Stack allocation</link></title> <para> A <link linkend="class_fiber"><code>fiber</code></link> uses internally an __econtext__ which manages a set of registers and a stack. The memory used by the stack is allocated/deallocated via a <emphasis>stack_allocator</emphasis> which is required to model a <link linkend="stack_allocator_concept"><emphasis>stack-allocator concept</emphasis></link>. </para> <para> A <emphasis>stack_allocator</emphasis> can be passed to <link linkend="fiber_fiber"><code><phrase role="identifier">fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">()</phrase></code></link> or to <link linkend="fibers_async"><code>fibers::async()</code></link>. </para> <anchor id="stack_allocator_concept"/> <bridgehead renderas="sect3" id="fiber.stack.h0"> <phrase id="fiber.stack.stack_allocator_concept"/><link linkend="fiber.stack.stack_allocator_concept">stack-allocator concept</link> </bridgehead> <para> A <emphasis>stack_allocator</emphasis> must satisfy the <emphasis>stack-allocator concept</emphasis> requirements shown in the following table, in which <code><phrase role="identifier">a</phrase></code> is an object of a <emphasis>stack_allocator</emphasis> type, <code><phrase role="identifier">sctx</phrase></code> is a <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html"><code><phrase role="identifier">stack_context</phrase></code></ulink>, and <code><phrase role="identifier">size</phrase></code> is a <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase></code>: </para> <informaltable frame="all"> <tgroup cols="3"> <thead> <row> <entry> <para> expression </para> </entry> <entry> <para> return type </para> </entry> <entry> <para> notes </para> </entry> </row> </thead> <tbody> <row> <entry> <para> <code><phrase role="identifier">a</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">)</phrase></code> </para> </entry> <entry> </entry> <entry> <para> creates a stack allocator </para> </entry> </row> <row> <entry> <para> <code><phrase role="identifier">a</phrase><phrase role="special">.</phrase><phrase role="identifier">allocate</phrase><phrase role="special">()</phrase></code> </para> </entry> <entry> <para> <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html"><code><phrase role="identifier">stack_context</phrase></code></ulink> </para> </entry> <entry> <para> creates a stack </para> </entry> </row> <row> <entry> <para> <code><phrase role="identifier">a</phrase><phrase role="special">.</phrase><phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">)</phrase></code> </para> </entry> <entry> <para> <code><phrase role="keyword">void</phrase></code> </para> </entry> <entry> <para> deallocates the stack created by <code><phrase role="identifier">a</phrase><phrase role="special">.</phrase><phrase role="identifier">allocate</phrase><phrase role="special">()</phrase></code> </para> </entry> </row> </tbody> </tgroup> </informaltable> <important> <para> The implementation of <code><phrase role="identifier">allocate</phrase><phrase role="special">()</phrase></code> might include logic to protect against exceeding the context's available stack size rather than leaving it as undefined behaviour. </para> </important> <important> <para> Calling <code><phrase role="identifier">deallocate</phrase><phrase role="special">()</phrase></code> with a <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html"><code><phrase role="identifier">stack_context</phrase></code></ulink> not obtained from <code><phrase role="identifier">allocate</phrase><phrase role="special">()</phrase></code> results in undefined behaviour. </para> </important> <note> <para> The memory for the stack is not required to be aligned; alignment takes place inside __econtext__. </para> </note> <para> See also <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack.html">Boost.Context stack allocation</ulink>. In particular, <code><phrase role="identifier">traits_type</phrase></code> methods are as described for <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_traits.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">stack_traits</phrase></code></ulink>. </para> <para> <bridgehead renderas="sect4" id="class_protected_fixedsize_stack_bridgehead"> <phrase id="class_protected_fixedsize_stack"/> <link linkend="class_protected_fixedsize_stack">Class <code>protected_fixedsize_stack</code></link> </bridgehead> </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides the class <link linkend="class_protected_fixedsize_stack"><code>protected_fixedsize_stack</code></link> which models the <link linkend="stack_allocator_concept"><emphasis>stack-allocator concept</emphasis></link>. It appends a guard page at the end of each stack to protect against exceeding the stack. If the guard page is accessed (read or write operation) a segmentation fault/access violation is generated by the operating system. </para> <important> <para> Using <link linkend="class_protected_fixedsize_stack"><code>protected_fixedsize_stack</code></link> is expensive. Launching a new fiber with a stack of this type incurs the overhead of setting the memory protection; once allocated, this stack is just as efficient to use as <link linkend="class_fixedsize_stack"><code>fixedsize_stack</code></link>. </para> </important> <note> <para> The appended <code><phrase role="identifier">guard</phrase> <phrase role="identifier">page</phrase></code> is <emphasis role="bold">not</emphasis> mapped to physical memory, only virtual addresses are used. </para> </note> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">protected_fixedsize</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">protected_fixedsize</phrase> <phrase role="special">{</phrase> <phrase role="identifier">protected_fixesize</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase> <phrase role="special">}</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="protected_fixedsize_allocate_bridgehead"> <phrase id="protected_fixedsize_allocate"/> <link linkend="protected_fixedsize_allocate">Member function <code>allocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">size</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Allocates memory of at least <code><phrase role="identifier">size</phrase></code> bytes and stores a pointer to the stack and its actual size in <code><phrase role="identifier">sctx</phrase></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="protected_fixesize_deallocate_bridgehead"> <phrase id="protected_fixesize_deallocate"/> <link linkend="protected_fixesize_deallocate">Member function <code>deallocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Deallocates the stack space. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_pooled_fixedsize_stack_bridgehead"> <phrase id="class_pooled_fixedsize_stack"/> <link linkend="class_pooled_fixedsize_stack">Class <code>pooled_fixedsize_stack</code></link> </bridgehead> </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides the class <link linkend="class_pooled_fixedsize_stack"><code>pooled_fixedsize_stack</code></link> which models the <link linkend="stack_allocator_concept"><emphasis>stack-allocator concept</emphasis></link>. In contrast to <link linkend="class_protected_fixedsize_stack"><code>protected_fixedsize_stack</code></link> it does not append a guard page at the end of each stack. The memory is managed internally by <ulink url="http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">pool</phrase><phrase role="special">&lt;&gt;</phrase></code></ulink>. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">pooled_fixedsize_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">pooled_fixedsize_stack</phrase> <phrase role="special">{</phrase> <phrase role="identifier">pooled_fixedsize_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">stack_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">(),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">next_size</phrase> <phrase role="special">=</phrase> <phrase role="number">32</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">max_size</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase> <phrase role="special">}</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="pooled_fixedsize_bridgehead"> <phrase id="pooled_fixedsize"/> <link linkend="pooled_fixedsize">Constructor</link> </bridgehead> </para> <programlisting><phrase role="identifier">pooled_fixedsize_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">stack_size</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">next_size</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">stack_size</phrase><phrase role="special">)</phrase></code> and <code><phrase role="number">0</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">next_size</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Allocates memory of at least <code><phrase role="identifier">stack_size</phrase></code> bytes and stores a pointer to the stack and its actual size in <code><phrase role="identifier">sctx</phrase></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. Argument <code><phrase role="identifier">next_size</phrase></code> determines the number of stacks to request from the system the first time that <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> needs to allocate system memory. The third argument <code><phrase role="identifier">max_size</phrase></code> controls how much memory might be allocated for stacks &mdash; a value of zero means no upper limit. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="pooled_fixedsize_allocate_bridgehead"> <phrase id="pooled_fixedsize_allocate"/> <link linkend="pooled_fixedsize_allocate">Member function <code>allocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">stack_size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Allocates memory of at least <code><phrase role="identifier">stack_size</phrase></code> bytes and stores a pointer to the stack and its actual size in <code><phrase role="identifier">sctx</phrase></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="pooled_fixesize_deallocate_bridgehead"> <phrase id="pooled_fixesize_deallocate"/> <link linkend="pooled_fixesize_deallocate">Member function <code>deallocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Deallocates the stack space. </para> </listitem> </varlistentry> </variablelist> <note> <para> This stack allocator is not thread safe. </para> </note> <para> <bridgehead renderas="sect4" id="class_fixedsize_stack_bridgehead"> <phrase id="class_fixedsize_stack"/> <link linkend="class_fixedsize_stack">Class <code>fixedsize_stack</code></link> </bridgehead> </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides the class <link linkend="class_fixedsize_stack"><code>fixedsize_stack</code></link> which models the <link linkend="stack_allocator_concept"><emphasis>stack-allocator concept</emphasis></link>. In contrast to <link linkend="class_protected_fixedsize_stack"><code>protected_fixedsize_stack</code></link> it does not append a guard page at the end of each stack. The memory is simply managed by <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">malloc</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">free</phrase><phrase role="special">()</phrase></code>. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">fixedsize_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">fixedsize_stack</phrase> <phrase role="special">{</phrase> <phrase role="identifier">fixedsize_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase> <phrase role="special">}</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="fixedsize_allocate_bridgehead"> <phrase id="fixedsize_allocate"/> <link linkend="fixedsize_allocate">Member function <code>allocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Allocates memory of at least <code><phrase role="identifier">size</phrase></code> bytes and stores a pointer to the stack and its actual size in <code><phrase role="identifier">sctx</phrase></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fixesize_deallocate_bridgehead"> <phrase id="fixesize_deallocate"/> <link linkend="fixesize_deallocate">Member function <code>deallocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Deallocates the stack space. </para> </listitem> </varlistentry> </variablelist> <para> <anchor id="segmented"/><bridgehead renderas="sect4" id="class_segmented_stack_bridgehead"> <phrase id="class_segmented_stack"/> <link linkend="class_segmented_stack">Class <code>segmented_stack</code></link> </bridgehead> </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> supports usage of a <link linkend="class_segmented_stack"><code>segmented_stack</code></link>, i.e. the stack grows on demand. The fiber is created with a minimal stack size which will be increased as required. Class <link linkend="class_segmented_stack"><code>segmented_stack</code></link> models the <link linkend="stack_allocator_concept"><emphasis>stack-allocator concept</emphasis></link>. In contrast to <link linkend="class_protected_fixedsize_stack"><code>protected_fixedsize_stack</code></link> and <link linkend="class_fixedsize_stack"><code>fixedsize_stack</code></link> it creates a stack which grows on demand. </para> <note> <para> Segmented stacks are currently only supported by <emphasis role="bold">gcc</emphasis> from version <emphasis role="bold">4.7</emphasis> and <emphasis role="bold">clang</emphasis> from version <emphasis role="bold">3.4</emphasis> onwards. In order to use a <link linkend="class_segmented_stack"><code>segmented_stack</code></link> <emphasis role="bold">Boost.Fiber</emphasis> must be built with property <code><phrase role="identifier">segmented</phrase><phrase role="special">-</phrase><phrase role="identifier">stacks</phrase></code>, e.g. <emphasis role="bold">toolset=gcc segmented-stacks=on</emphasis> and applying BOOST_USE_SEGMENTED_STACKS at b2/bjam command line. </para> </note> <note> <para> Segmented stacks can only be used with callcc() using property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">ucontext</phrase></code>. </para> </note> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">segmented_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">segmented_stack</phrase> <phrase role="special">{</phrase> <phrase role="identifier">segmented_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">stack_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase> <phrase role="special">}</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="segmented_allocate_bridgehead"> <phrase id="segmented_allocate"/> <link linkend="segmented_allocate">Member function <code>allocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Allocates memory of at least <code><phrase role="identifier">size</phrase></code> bytes and stores a pointer to the stack and its actual size in <code><phrase role="identifier">sctx</phrase></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="segmented_deallocate_bridgehead"> <phrase id="segmented_deallocate"/> <link linkend="segmented_deallocate">Member function <code>deallocate</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase></code> and <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Deallocates the stack space. </para> </listitem> </varlistentry> </variablelist> <note> <para> If the library is compiled for segmented stacks, <link linkend="class_segmented_stack"><code>segmented_stack</code></link> is the only available stack allocator. </para> </note> <section id="fiber.stack.valgrind"> <title><link linkend="fiber.stack.valgrind">Support for valgrind</link></title> <para> Running programs that switch stacks under valgrind causes problems. Property (b2 command-line) <code><phrase role="identifier">valgrind</phrase><phrase role="special">=</phrase><phrase role="identifier">on</phrase></code> let valgrind treat the memory regions as stack space which suppresses the errors. </para> </section> </section> <section id="fiber.synchronization"> <title><anchor id="synchronization"/><link linkend="fiber.synchronization">Synchronization</link></title> <para> In general, <emphasis role="bold">Boost.Fiber</emphasis> synchronization objects can neither be moved nor copied. A synchronization object acts as a mutually-agreed rendezvous point between different fibers. If such an object were copied somewhere else, the new copy would have no consumers. If such an object were <emphasis>moved</emphasis> somewhere else, leaving the original instance in an unspecified state, existing consumers would behave strangely. </para> <para> The fiber synchronization objects provided by this library will, by default, safely synchronize fibers running on different threads. However, this level of synchronization can be removed (for performance) by building the library with <emphasis role="bold"><code><phrase role="identifier">BOOST_FIBERS_NO_ATOMICS</phrase></code></emphasis> defined. When the library is built with that macro, you must ensure that all the fibers referencing a particular synchronization object are running in the same thread. </para> <section id="fiber.synchronization.mutex_types"> <title><link linkend="fiber.synchronization.mutex_types">Mutex Types</link></title> <para> <bridgehead renderas="sect4" id="class_mutex_bridgehead"> <phrase id="class_mutex"/> <link linkend="class_mutex">Class <code>mutex</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">mutex</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">mutex</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">mutex</phrase><phrase role="special">();</phrase> <phrase role="special">~</phrase><phrase role="identifier">mutex</phrase><phrase role="special">();</phrase> <phrase role="identifier">mutex</phrase><phrase role="special">(</phrase> <phrase role="identifier">mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">mutex</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <link linkend="class_mutex"><code>mutex</code></link> provides an exclusive-ownership mutex. At most one fiber can own the lock on a given instance of <link linkend="class_mutex"><code>mutex</code></link> at any time. Multiple concurrent calls to <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> shall be permitted. </para> <para> Any fiber blocked in <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> is suspended until the owning fiber releases the lock by calling <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code>. </para> <para> <bridgehead renderas="sect4" id="mutex_lock_bridgehead"> <phrase id="mutex_lock"/> <link linkend="mutex_lock">Member function <code>lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> The current fiber blocks until ownership can be obtained. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="mutex_try_lock_bridgehead"> <phrase id="mutex_try_lock"/> <link linkend="mutex_try_lock">Member function <code>try_lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber without blocking. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="mutex_unlock_bridgehead"> <phrase id="mutex_unlock"/> <link linkend="mutex_unlock">Member function <code>unlock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The current fiber owns <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Releases a lock on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">operation_not_permitted</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> does not own the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_timed_mutex_bridgehead"> <phrase id="class_timed_mutex"/> <link linkend="class_timed_mutex">Class <code>timed_mutex</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">timed_mutex</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">timed_mutex</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">timed_mutex</phrase><phrase role="special">();</phrase> <phrase role="special">~</phrase><phrase role="identifier">timed_mutex</phrase><phrase role="special">();</phrase> <phrase role="identifier">timed_mutex</phrase><phrase role="special">(</phrase> <phrase role="identifier">timed_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">timed_mutex</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">timed_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <link linkend="class_timed_mutex"><code>timed_mutex</code></link> provides an exclusive-ownership mutex. At most one fiber can own the lock on a given instance of <link linkend="class_timed_mutex"><code>timed_mutex</code></link> at any time. Multiple concurrent calls to <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock_until</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock_for</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> shall be permitted. </para> <para> <bridgehead renderas="sect4" id="timed_mutex_lock_bridgehead"> <phrase id="timed_mutex_lock"/> <link linkend="timed_mutex_lock">Member function <code>lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> The current fiber blocks until ownership can be obtained. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="timed_mutex_try_lock_bridgehead"> <phrase id="timed_mutex_try_lock"/> <link linkend="timed_mutex_try_lock">Member function <code>try_lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber without blocking. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="timed_mutex_unlock_bridgehead"> <phrase id="timed_mutex_unlock"/> <link linkend="timed_mutex_unlock">Member function <code>unlock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The current fiber owns <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Releases a lock on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">operation_not_permitted</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> does not own the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="timed_mutex_try_lock_until_bridgehead"> <phrase id="timed_mutex_try_lock_until"/> <link linkend="timed_mutex_try_lock_until">Templated member function <code>try_lock_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <link linkend="timed_mutex_try_lock"><code>timed_mutex::try_lock()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code>, timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="timed_mutex_try_lock_for_bridgehead"> <phrase id="timed_mutex_try_lock_for"/> <link linkend="timed_mutex_try_lock_for">Templated member function <code>try_lock_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> The calling fiber doesn't own the mutex. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <link linkend="timed_mutex_try_lock"><code>timed_mutex::try_lock()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code>, timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">resource_deadlock_would_occur</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> already owns the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_recursive_mutex_bridgehead"> <phrase id="class_recursive_mutex"/> <link linkend="class_recursive_mutex">Class <code>recursive_mutex</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">recursive_mutex</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">recursive_mutex</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">recursive_mutex</phrase><phrase role="special">();</phrase> <phrase role="special">~</phrase><phrase role="identifier">recursive_mutex</phrase><phrase role="special">();</phrase> <phrase role="identifier">recursive_mutex</phrase><phrase role="special">(</phrase> <phrase role="identifier">recursive_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">recursive_mutex</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">recursive_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <link linkend="class_recursive_mutex"><code>recursive_mutex</code></link> provides an exclusive-ownership recursive mutex. At most one fiber can own the lock on a given instance of <link linkend="class_recursive_mutex"><code>recursive_mutex</code></link> at any time. Multiple concurrent calls to <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> shall be permitted. A fiber that already has exclusive ownership of a given <link linkend="class_recursive_mutex"><code>recursive_mutex</code></link> instance can call <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code> to acquire an additional level of ownership of the mutex. <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> must be called once for each level of ownership acquired by a single fiber before ownership can be acquired by another fiber. </para> <para> <bridgehead renderas="sect4" id="recursive_mutex_lock_bridgehead"> <phrase id="recursive_mutex_lock"/> <link linkend="recursive_mutex_lock">Member function <code>lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The current fiber blocks until ownership can be obtained. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_mutex_try_lock_bridgehead"> <phrase id="recursive_mutex_try_lock"/> <link linkend="recursive_mutex_try_lock">Member function <code>try_lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber without blocking. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_mutex_unlock_bridgehead"> <phrase id="recursive_mutex_unlock"/> <link linkend="recursive_mutex_unlock">Member function <code>unlock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Releases a lock on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">operation_not_permitted</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> does not own the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_recursive_timed_mutex_bridgehead"> <phrase id="class_recursive_timed_mutex"/> <link linkend="class_recursive_timed_mutex">Class <code>recursive_timed_mutex</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">recursive_timed_mutex</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">recursive_timed_mutex</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">recursive_timed_mutex</phrase><phrase role="special">();</phrase> <phrase role="special">~</phrase><phrase role="identifier">recursive_timed_mutex</phrase><phrase role="special">();</phrase> <phrase role="identifier">recursive_timed_mutex</phrase><phrase role="special">(</phrase> <phrase role="identifier">recursive_timed_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">recursive_timed_mutex</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">recursive_timed_mutex</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <para> <link linkend="class_recursive_timed_mutex"><code>recursive_timed_mutex</code></link> provides an exclusive-ownership recursive mutex. At most one fiber can own the lock on a given instance of <link linkend="class_recursive_timed_mutex"><code>recursive_timed_mutex</code></link> at any time. Multiple concurrent calls to <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock_for</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock_until</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> shall be permitted. A fiber that already has exclusive ownership of a given <link linkend="class_recursive_timed_mutex"><code>recursive_timed_mutex</code></link> instance can call <code><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">try_lock_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">try_lock_until</phrase><phrase role="special">()</phrase></code> to acquire an additional level of ownership of the mutex. <code><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> must be called once for each level of ownership acquired by a single fiber before ownership can be acquired by another fiber. </para> <para> <bridgehead renderas="sect4" id="recursive_timed_mutex_lock_bridgehead"> <phrase id="recursive_timed_mutex_lock"/> <link linkend="recursive_timed_mutex_lock">Member function <code>lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">lock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The current fiber blocks until ownership can be obtained. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_timed_mutex_try_lock_bridgehead"> <phrase id="recursive_timed_mutex_try_lock"/> <link linkend="recursive_timed_mutex_try_lock">Member function <code>try_lock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber without blocking. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_timed_mutex_unlock_bridgehead"> <phrase id="recursive_timed_mutex_unlock"/> <link linkend="recursive_timed_mutex_unlock">Member function <code>unlock</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Releases a lock on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">lock_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">operation_not_permitted</emphasis>: if <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase></code> does not own the mutex. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_timed_mutex_try_lock_until_bridgehead"> <phrase id="recursive_timed_mutex_try_lock_until"/> <link linkend="recursive_timed_mutex_try_lock_until">Templated member function <code>try_lock_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <link linkend="recursive_timed_mutex_try_lock"><code>recursive_timed_mutex::try_lock()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="recursive_timed_mutex_try_lock_for_bridgehead"> <phrase id="recursive_timed_mutex_try_lock_for"/> <link linkend="recursive_timed_mutex_try_lock_for">Templated member function <code>try_lock_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">try_lock_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <link linkend="recursive_timed_mutex_try_lock"><code>recursive_timed_mutex::try_lock()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if ownership was obtained for the current fiber, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.synchronization.conditions"> <title><link linkend="fiber.synchronization.conditions">Condition Variables</link></title> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h0"> <phrase id="fiber.synchronization.conditions.synopsis"/><link linkend="fiber.synchronization.conditions.synopsis">Synopsis</link> </bridgehead> <programlisting><phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">cv_status</phrase><phrase role="special">;</phrase> <phrase role="special">{</phrase> <phrase role="identifier">no_timeout</phrase><phrase role="special">,</phrase> <phrase role="identifier">timeout</phrase> <phrase role="special">};</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">condition_variable</phrase><phrase role="special">;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">condition_variable_any</phrase><phrase role="special">;</phrase> </programlisting> <para> The class <link linkend="class_condition_variable"><code>condition_variable</code></link> provides a mechanism for a fiber to wait for notification from another fiber. When the fiber awakens from the wait, then it checks to see if the appropriate condition is now true, and continues if so. If the condition is not true, then the fiber calls <code><phrase role="identifier">wait</phrase></code> again to resume waiting. In the simplest case, this condition is just a boolean variable: </para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase> <phrase role="identifier">cond</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">data_ready</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">process_data</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_for_data_to_process</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">);</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">data_ready</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">cond</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">// release lk</phrase> <phrase role="identifier">process_data</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> <para> Notice that the <code><phrase role="identifier">lk</phrase></code> is passed to <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link>: <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> will atomically add the fiber to the set of fibers waiting on the condition variable, and unlock the <link linkend="class_mutex"><code>mutex</code></link>. When the fiber is awakened, the <code><phrase role="identifier">mutex</phrase></code> will be locked again before the call to <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> returns. This allows other fibers to acquire the <code><phrase role="identifier">mutex</phrase></code> in order to update the shared data, and ensures that the data associated with the condition is correctly synchronized. </para> <para> <code><phrase role="identifier">wait_for_data_to_process</phrase><phrase role="special">()</phrase></code> could equivalently be written: </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">wait_for_data_to_process</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">);</phrase> <phrase role="comment">// make condition_variable::wait() perform the loop</phrase> <phrase role="identifier">cond</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">data_ready</phrase><phrase role="special">;</phrase> <phrase role="special">});</phrase> <phrase role="special">}</phrase> <phrase role="comment">// release lk</phrase> <phrase role="identifier">process_data</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> <para> In the meantime, another fiber sets <code><phrase role="identifier">data_ready</phrase></code> to <code><phrase role="keyword">true</phrase></code>, and then calls either <link linkend="condition_variable_notify_one"><code>condition_variable::notify_one()</code></link> or <link linkend="condition_variable_notify_all"><code>condition_variable::notify_all()</code></link> on the <link linkend="class_condition_variable"><code>condition_variable</code></link> <code><phrase role="identifier">cond</phrase></code> to wake one waiting fiber or all the waiting fibers respectively. </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">retrieve_data</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">prepare_data</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">prepare_data_for_processing</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">retrieve_data</phrase><phrase role="special">();</phrase> <phrase role="identifier">prepare_data</phrase><phrase role="special">();</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">);</phrase> <phrase role="identifier">data_ready</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">cond</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> <para> Note that the same <link linkend="class_mutex"><code>mutex</code></link> is locked before the shared data is updated, but that the <code><phrase role="identifier">mutex</phrase></code> does not have to be locked across the call to <link linkend="condition_variable_notify_one"><code>condition_variable::notify_one()</code></link>. </para> <para> Locking is important because the synchronization objects provided by <emphasis role="bold">Boost.Fiber</emphasis> can be used to synchronize fibers running on different threads. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides both <link linkend="class_condition_variable"><code>condition_variable</code></link> and <link linkend="class_condition_variable_any"><code>condition_variable_any</code></link>. <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase></code> can only wait on <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase></code></ulink><code><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase></code><link linkend="class_mutex"><code>mutex</code></link><code> <phrase role="special">&gt;</phrase></code> while <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable_any</phrase></code> can wait on user-defined lock types. </para> <anchor id="condition_variable_spurious_wakeups"/> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h1"> <phrase id="fiber.synchronization.conditions.no_spurious_wakeups"/><link linkend="fiber.synchronization.conditions.no_spurious_wakeups">No Spurious Wakeups</link> </bridgehead> <para> Neither <link linkend="class_condition_variable"><code>condition_variable</code></link> nor <link linkend="class_condition_variable_any"><code>condition_variable_any</code></link> are subject to spurious wakeup: <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link> can only wake up when <link linkend="condition_variable_notify_one"><code>condition_variable::notify_one()</code></link> or <link linkend="condition_variable_notify_all"><code>condition_variable::notify_all()</code></link> is called. Even so, it is prudent to use one of the <code><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lock</phrase><phrase role="special">,</phrase> <phrase role="identifier">predicate</phrase> <phrase role="special">)</phrase></code> overloads. </para> <para> Consider a set of consumer fibers processing items from a <ulink url="http://en.cppreference.com/w/cpp/container/queue"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">queue</phrase></code></ulink>. The queue is continually populated by a set of producer fibers. </para> <para> The consumer fibers might reasonably wait on a <code><phrase role="identifier">condition_variable</phrase></code> as long as the queue remains <ulink url="http://en.cppreference.com/w/cpp/container/queue/empty"><code><phrase role="identifier">empty</phrase><phrase role="special">()</phrase></code></ulink>. </para> <para> Because producer fibers might <ulink url="http://en.cppreference.com/w/cpp/container/queue/push"><code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code></ulink> items to the queue in bursts, they call <link linkend="condition_variable_notify_all"><code>condition_variable::notify_all()</code></link> rather than <link linkend="condition_variable_notify_one"><code>condition_variable::notify_one()</code></link>. </para> <para> But a given consumer fiber might well wake up from <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link> and find the queue <code><phrase role="identifier">empty</phrase><phrase role="special">()</phrase></code>, because other consumer fibers might already have processed all pending items. </para> <para> (See also <link linkend="spurious_wakeup">spurious wakeup</link>.) </para> <anchor id="class_cv_status"/> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h2"> <phrase id="fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_"/><link linkend="fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_">Enumeration <code><phrase role="identifier">cv_status</phrase></code></link> </bridgehead> <para> A timed wait operation might return because of timeout or not. </para> <programlisting><phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="special">{</phrase> <phrase role="identifier">no_timeout</phrase><phrase role="special">,</phrase> <phrase role="identifier">timeout</phrase> <phrase role="special">};</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h3"> <phrase id="fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"/><link linkend="fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"><code><phrase role="identifier">no_timeout</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The condition variable was awakened with <code><phrase role="identifier">notify_one</phrase></code> or <code><phrase role="identifier">notify_all</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h4"> <phrase id="fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"/><link linkend="fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"><code><phrase role="identifier">timeout</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The condition variable was awakened by timeout. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_condition_variable_any_bridgehead"> <phrase id="class_condition_variable_any"/> <link linkend="class_condition_variable_any">Class <code>condition_variable_any</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> condition_variable_any <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> condition_variable_any<phrase role="special">();</phrase> <phrase role="special">~</phrase>condition_variable_any<phrase role="special">();</phrase> condition_variable_any<phrase role="special">(</phrase> condition_variable_any <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> condition_variable_any <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> condition_variable_any <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> template&lt; typename LockType &gt; void <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h5"> <phrase id="fiber.synchronization.conditions.constructor"/><link linkend="fiber.synchronization.conditions.constructor">Constructor</link> </bridgehead> <programlisting>condition_variable_any<phrase role="special">()</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates the object. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h6"> <phrase id="fiber.synchronization.conditions.destructor"/><link linkend="fiber.synchronization.conditions.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase>condition_variable_any<phrase role="special">()</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> All fibers waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> have been notified by a call to <code><phrase role="identifier">notify_one</phrase></code> or <code><phrase role="identifier">notify_all</phrase></code> (though the respective calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> need not have returned). </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys the object. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_any_notify_one_bridgehead"> <phrase id="condition_variable_any_notify_one"/> <link linkend="condition_variable_any_notify_one">Member function <code>notify_one</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If any fibers are currently <link linkend="blocking"><emphasis>blocked</emphasis></link> waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in a call to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code>, unblocks one of those fibers. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> It is arbitrary which waiting fiber is resumed. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_any_notify_all_bridgehead"> <phrase id="condition_variable_any_notify_all"/> <link linkend="condition_variable_any_notify_all">Member function <code>notify_all</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If any fibers are currently <link linkend="blocking"><emphasis>blocked</emphasis></link> waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in a call to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code>, unblocks all of those fibers. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This is why a waiting fiber must <emphasis>also</emphasis> check for the desired program state using a mechanism external to the <code>condition_variable_any</code>, and retry the wait until that state is reached. A fiber waiting on a <code>condition_variable_any</code> might well wake up a number of times before the desired state is reached. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_any_wait_bridgehead"> <phrase id="condition_variable_any_wait"/> <link linkend="condition_variable_any_wait">Templated member function <code>wait</code>()</link> </bridgehead> </para> <programlisting>template&lt; typename LockType &gt; void <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex"><code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code></ulink> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The Precondition is a bit dense. It merely states that all the fibers concurrently calling <code><phrase role="identifier">wait</phrase></code> on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> must wait on <code><phrase role="identifier">lk</phrase></code> objects governing the <emphasis>same</emphasis> <link linkend="class_mutex"><code>mutex</code></link>. Three distinct objects are involved in any <code>condition_variable_any::wait()</code> call: the <code>condition_variable_any</code> itself, the <code><phrase role="identifier">mutex</phrase></code> coordinating access between fibers and a local lock object (e.g. <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase></code></ulink>). In general, you can partition the lifespan of a given <code>condition_variable_any</code> instance into periods with one or more fibers waiting on it, separated by periods when no fibers are waiting on it. When more than one fiber is waiting on that <code>condition_variable_any</code>, all must pass lock objects referencing the <emphasis>same</emphasis> <code><phrase role="identifier">mutex</phrase></code> instance. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_any_wait_until_bridgehead"> <phrase id="condition_variable_any_wait_until"/> <link linkend="condition_variable_any_wait_until">Templated member function <code>wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait_until</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, when the system time would be equal to or later than the specified <code><phrase role="identifier">abs_time</phrase></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait_until</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase> <phrase role="special">==</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">pred</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> </programlisting> That is, even if <code><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code> times out, it can still return <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">pred</phrase><phrase role="special">()</phrase></code> returns <code><phrase role="keyword">true</phrase></code> at that time. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs or timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload without <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">no_timeout</phrase></code> if awakened by <code><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, or <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase></code> if awakened because the system time is past <code><phrase role="identifier">abs_time</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload accepting <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="keyword">false</phrase></code> if the call is returning because the time specified by <code><phrase role="identifier">abs_time</phrase></code> was reached and the predicate returns <code><phrase role="keyword">false</phrase></code>, <code><phrase role="keyword">true</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> See <emphasis role="bold">Note</emphasis> for <link linkend="condition_variable_any_wait"><code>condition_variable_any::wait()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_any_wait_for_bridgehead"> <phrase id="condition_variable_any_wait_for"/> <link linkend="condition_variable_any_wait_for">Templated member function <code>wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename LockType, typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> LockType <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait_for</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, when a time interval equal to or greater than the specified <code><phrase role="identifier">rel_time</phrase></code> has elapsed. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The <code><phrase role="identifier">wait_for</phrase><phrase role="special">()</phrase></code> member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase> <phrase role="special">==</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">pred</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> </programlisting> (except of course that <code><phrase role="identifier">rel_time</phrase></code> is adjusted for each iteration). The point is that, even if <code><phrase role="identifier">wait_for</phrase><phrase role="special">()</phrase></code> times out, it can still return <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">pred</phrase><phrase role="special">()</phrase></code> returns <code><phrase role="keyword">true</phrase></code> at that time. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs or timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload without <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">no_timeout</phrase></code> if awakened by <code><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, or <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase></code> if awakened because at least <code><phrase role="identifier">rel_time</phrase></code> has elapsed. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload accepting <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="keyword">false</phrase></code> if the call is returning because at least <code><phrase role="identifier">rel_time</phrase></code> has elapsed and the predicate returns <code><phrase role="keyword">false</phrase></code>, <code><phrase role="keyword">true</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> See <emphasis role="bold">Note</emphasis> for <link linkend="condition_variable_any_wait"><code>condition_variable_any::wait()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_condition_variable_bridgehead"> <phrase id="class_condition_variable"/> <link linkend="class_condition_variable">Class <code>condition_variable</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> condition_variable <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> condition_variable<phrase role="special">();</phrase> <phrase role="special">~</phrase>condition_variable<phrase role="special">();</phrase> condition_variable<phrase role="special">(</phrase> condition_variable <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> condition_variable <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> condition_variable <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> void <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h7"> <phrase id="fiber.synchronization.conditions.constructor0"/><link linkend="fiber.synchronization.conditions.constructor0">Constructor</link> </bridgehead> <programlisting>condition_variable<phrase role="special">()</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates the object. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.conditions.h8"> <phrase id="fiber.synchronization.conditions.destructor0"/><link linkend="fiber.synchronization.conditions.destructor0">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase>condition_variable<phrase role="special">()</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> All fibers waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> have been notified by a call to <code><phrase role="identifier">notify_one</phrase></code> or <code><phrase role="identifier">notify_all</phrase></code> (though the respective calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> need not have returned). </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys the object. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_notify_one_bridgehead"> <phrase id="condition_variable_notify_one"/> <link linkend="condition_variable_notify_one">Member function <code>notify_one</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If any fibers are currently <link linkend="blocking"><emphasis>blocked</emphasis></link> waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in a call to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code>, unblocks one of those fibers. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> It is arbitrary which waiting fiber is resumed. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_notify_all_bridgehead"> <phrase id="condition_variable_notify_all"/> <link linkend="condition_variable_notify_all">Member function <code>notify_all</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If any fibers are currently <link linkend="blocking"><emphasis>blocked</emphasis></link> waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> in a call to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code>, unblocks all of those fibers. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> This is why a waiting fiber must <emphasis>also</emphasis> check for the desired program state using a mechanism external to the <code>condition_variable</code>, and retry the wait until that state is reached. A fiber waiting on a <code>condition_variable</code> might well wake up a number of times before the desired state is reached. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_wait_bridgehead"> <phrase id="condition_variable_wait"/> <link linkend="condition_variable_wait">Templated member function <code>wait</code>()</link> </bridgehead> </para> <programlisting>void <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex"><code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code></ulink> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The Precondition is a bit dense. It merely states that all the fibers concurrently calling <code><phrase role="identifier">wait</phrase></code> on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> must wait on <code><phrase role="identifier">lk</phrase></code> objects governing the <emphasis>same</emphasis> <link linkend="class_mutex"><code>mutex</code></link>. Three distinct objects are involved in any <code>condition_variable::wait()</code> call: the <code>condition_variable</code> itself, the <code><phrase role="identifier">mutex</phrase></code> coordinating access between fibers and a local lock object (e.g. <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase></code></ulink>). In general, you can partition the lifespan of a given <code>condition_variable</code> instance into periods with one or more fibers waiting on it, separated by periods when no fibers are waiting on it. When more than one fiber is waiting on that <code>condition_variable</code>, all must pass lock objects referencing the <emphasis>same</emphasis> <code><phrase role="identifier">mutex</phrase></code> instance. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_wait_until_bridgehead"> <phrase id="condition_variable_wait_until"/> <link linkend="condition_variable_wait_until">Templated member function <code>wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait_until</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, when the system time would be equal to or later than the specified <code><phrase role="identifier">abs_time</phrase></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait_until</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase> <phrase role="special">==</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">pred</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> </programlisting> That is, even if <code><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code> times out, it can still return <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">pred</phrase><phrase role="special">()</phrase></code> returns <code><phrase role="keyword">true</phrase></code> at that time. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs or timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload without <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">no_timeout</phrase></code> if awakened by <code><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, or <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase></code> if awakened because the system time is past <code><phrase role="identifier">abs_time</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload accepting <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="keyword">false</phrase></code> if the call is returning because the time specified by <code><phrase role="identifier">abs_time</phrase></code> was reached and the predicate returns <code><phrase role="keyword">false</phrase></code>, <code><phrase role="keyword">true</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> See <emphasis role="bold">Note</emphasis> for <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="condition_variable_wait_for_bridgehead"> <phrase id="condition_variable_wait_for"/> <link linkend="condition_variable_wait_for">Templated member function <code>wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">cv_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> typename <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Pred</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> std::unique_lock&lt; mutex &gt; <phrase role="special">&amp;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">,</phrase> <phrase role="identifier">Pred</phrase> <phrase role="identifier">pred</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber, and either no other fiber is currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>, or the execution of the <code><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> member function on the <code><phrase role="identifier">lk</phrase></code> objects supplied in the calls to <code><phrase role="identifier">wait</phrase></code>, <code><phrase role="identifier">wait_for</phrase></code> or <code><phrase role="identifier">wait_until</phrase></code> in all the fibers currently waiting on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> would return the same value as <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">mutex</phrase><phrase role="special">()</phrase></code> for this call to <code><phrase role="identifier">wait_for</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Atomically call <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">()</phrase></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, when a time interval equal to or greater than the specified <code><phrase role="identifier">rel_time</phrase></code> has elapsed. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> before the call to <code><phrase role="identifier">wait</phrase></code> returns. The lock is also reacquired by invoking <code><phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">()</phrase></code> if the function exits with an exception. The <code><phrase role="identifier">wait_for</phrase><phrase role="special">()</phrase></code> member function accepting <code><phrase role="identifier">pred</phrase></code> is shorthand for: <programlisting><phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">pred</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase> <phrase role="special">==</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">rel_time</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">pred</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> </programlisting> (except of course that <code><phrase role="identifier">rel_time</phrase></code> is adjusted for each iteration). The point is that, even if <code><phrase role="identifier">wait_for</phrase><phrase role="special">()</phrase></code> times out, it can still return <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">pred</phrase><phrase role="special">()</phrase></code> returns <code><phrase role="keyword">true</phrase></code> at that time. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">lk</phrase></code> is locked by the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs or timeout-related exceptions. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload without <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">no_timeout</phrase></code> if awakened by <code><phrase role="identifier">notify_one</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code>, or <code><phrase role="identifier">cv_status</phrase><phrase role="special">::</phrase><phrase role="identifier">timeout</phrase></code> if awakened because at least <code><phrase role="identifier">rel_time</phrase></code> has elapsed. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> The overload accepting <code><phrase role="identifier">pred</phrase></code> returns <code><phrase role="keyword">false</phrase></code> if the call is returning because at least <code><phrase role="identifier">rel_time</phrase></code> has elapsed and the predicate returns <code><phrase role="keyword">false</phrase></code>, <code><phrase role="keyword">true</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> See <emphasis role="bold">Note</emphasis> for <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link>. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.synchronization.barriers"> <title><link linkend="fiber.synchronization.barriers">Barriers</link></title> <para> A barrier is a concept also known as a <emphasis>rendezvous</emphasis>, it is a synchronization point between multiple contexts of execution (fibers). The barrier is configured for a particular number of fibers (<code><phrase role="identifier">n</phrase></code>), and as fibers reach the barrier they must wait until all <code><phrase role="identifier">n</phrase></code> fibers have arrived. Once the <code><phrase role="identifier">n</phrase></code>-th fiber has reached the barrier, all the waiting fibers can proceed, and the barrier is reset. </para> <para> The fact that the barrier automatically resets is significant. Consider a case in which you launch some number of fibers and want to wait only until the first of them has completed. You might be tempted to use a <code><phrase role="identifier">barrier</phrase><phrase role="special">(</phrase><phrase role="number">2</phrase><phrase role="special">)</phrase></code> as the synchronization mechanism, making each new fiber call its <link linkend="barrier_wait"><code>barrier::wait()</code></link> method, then calling <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> in the launching fiber to wait until the first other fiber completes. </para> <para> That will in fact unblock the launching fiber. The unfortunate part is that it will continue blocking the <emphasis>remaining</emphasis> fibers. </para> <para> Consider the following scenario: </para> <orderedlist> <listitem> <simpara> Fiber <quote>main</quote> launches fibers A, B, C and D, then calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. </simpara> </listitem> <listitem> <simpara> Fiber C finishes first and likewise calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. </simpara> </listitem> <listitem> <simpara> Fiber <quote>main</quote> is unblocked, as desired. </simpara> </listitem> <listitem> <simpara> Fiber B calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. Fiber B is <emphasis>blocked!</emphasis> </simpara> </listitem> <listitem> <simpara> Fiber A calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. Fibers A and B are unblocked. </simpara> </listitem> <listitem> <simpara> Fiber D calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. Fiber D is blocked indefinitely. </simpara> </listitem> </orderedlist> <para> (See also <link linkend="wait_first_simple_section">when_any, simple completion</link>.) </para> <note> <para> It is unwise to tie the lifespan of a barrier to any one of its participating fibers. Although conceptually all waiting fibers awaken <quote>simultaneously,</quote> because of the nature of fibers, in practice they will awaken one by one in indeterminate order.<footnote id="fiber.synchronization.barriers.f0"> <para> The current implementation wakes fibers in FIFO order: the first to call <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> wakes first, and so forth. But it is perilous to rely on the order in which the various fibers will reach the <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> call. </para> </footnote> The rest of the waiting fibers will still be blocked in <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>, which must, before returning, access data members in the barrier object. </para> </note> <para> <bridgehead renderas="sect4" id="class_barrier_bridgehead"> <phrase id="class_barrier"/> <link linkend="class_barrier">Class <code>barrier</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">barrier</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">barrier</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase><phrase role="special">);</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">(</phrase> <phrase role="identifier">barrier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">barrier</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">barrier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <para> Instances of <link linkend="class_barrier"><code>barrier</code></link> are not copyable or movable. </para> <bridgehead renderas="sect4" id="fiber.synchronization.barriers.h0"> <phrase id="fiber.synchronization.barriers.constructor"/><link linkend="fiber.synchronization.barriers.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="keyword">explicit</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">initial</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Construct a barrier for <code><phrase role="identifier">initial</phrase></code> fibers. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">invalid_argument</emphasis>: if <code><phrase role="identifier">initial</phrase></code> is zero. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="barrier_wait_bridgehead"> <phrase id="barrier_wait"/> <link linkend="barrier_wait">Member function <code>wait</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">wait</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Block until <code><phrase role="identifier">initial</phrase></code> fibers have called <code><phrase role="identifier">wait</phrase></code> on <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. When the <code><phrase role="identifier">initial</phrase></code>-th fiber calls <code><phrase role="identifier">wait</phrase></code>, all waiting fibers are unblocked, and the barrier is reset. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> for exactly one fiber from each batch of waiting fibers, <code><phrase role="keyword">false</phrase></code> otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.synchronization.channels"> <title><link linkend="fiber.synchronization.channels">Channels</link></title> <para> A channel is a model to communicate and synchronize <code><phrase role="identifier">Threads</phrase> <phrase role="identifier">of</phrase> <phrase role="identifier">Execution</phrase></code> <footnote id="fiber.synchronization.channels.f0"> <para> The smallest ordered sequence of instructions that can be managed independently by a scheduler is called a <code><phrase role="identifier">Thread</phrase> <phrase role="identifier">of</phrase> <phrase role="identifier">Execution</phrase></code>. </para> </footnote> via message passing. </para> <anchor id="class_channel_op_status"/> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h0"> <phrase id="fiber.synchronization.channels.enumeration__code__phrase_role__identifier__channel_op_status__phrase___code_"/><link linkend="fiber.synchronization.channels.enumeration__code__phrase_role__identifier__channel_op_status__phrase___code_">Enumeration <code><phrase role="identifier">channel_op_status</phrase></code></link> </bridgehead> <para> channel operations return the state of the channel. </para> <programlisting><phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="special">{</phrase> <phrase role="identifier">success</phrase><phrase role="special">,</phrase> <phrase role="identifier">empty</phrase><phrase role="special">,</phrase> <phrase role="identifier">full</phrase><phrase role="special">,</phrase> <phrase role="identifier">closed</phrase><phrase role="special">,</phrase> <phrase role="identifier">timeout</phrase> <phrase role="special">};</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h1"> <phrase id="fiber.synchronization.channels._code__phrase_role__identifier__success__phrase___code_"/><link linkend="fiber.synchronization.channels._code__phrase_role__identifier__success__phrase___code_"><code><phrase role="identifier">success</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Operation was successful. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h2"> <phrase id="fiber.synchronization.channels._code__phrase_role__identifier__empty__phrase___code_"/><link linkend="fiber.synchronization.channels._code__phrase_role__identifier__empty__phrase___code_"><code><phrase role="identifier">empty</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> channel is empty, operation failed. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h3"> <phrase id="fiber.synchronization.channels._code__phrase_role__identifier__full__phrase___code_"/><link linkend="fiber.synchronization.channels._code__phrase_role__identifier__full__phrase___code_"><code><phrase role="identifier">full</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> channel is full, operation failed. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h4"> <phrase id="fiber.synchronization.channels._code__phrase_role__identifier__closed__phrase___code_"/><link linkend="fiber.synchronization.channels._code__phrase_role__identifier__closed__phrase___code_"><code><phrase role="identifier">closed</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> channel is closed, operation failed. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect4" id="fiber.synchronization.channels.h5"> <phrase id="fiber.synchronization.channels._code__phrase_role__identifier__timeout__phrase___code_"/><link linkend="fiber.synchronization.channels._code__phrase_role__identifier__timeout__phrase___code_"><code><phrase role="identifier">timeout</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The operation did not become ready before specified timeout elapsed. </para> </listitem> </varlistentry> </variablelist> <section id="fiber.synchronization.channels.buffered_channel"> <title><link linkend="fiber.synchronization.channels.buffered_channel">Buffered Channel</link></title> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides a bounded, buffered channel (MPMC queue) suitable to synchonize fibers (running on same or different threads) via asynchronouss message passing. </para> <programlisting><phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">send</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="number">5</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">i</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">recv</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;received &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="identifier">chan</phrase><phrase role="special">{</phrase> <phrase role="number">2</phrase> <phrase role="special">};</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f1</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="identifier">send</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f2</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="identifier">recv</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> <phrase role="identifier">f2</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> Class <code><phrase role="identifier">buffered_channel</phrase></code> supports range-for syntax: </para> <programlisting><phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">foo</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">5</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">8</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">bar</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase> <phrase role="special">:</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">value</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="class_buffered_channel_bridgehead"> <phrase id="class_buffered_channel"/> <link linkend="class_buffered_channel">Template <code>buffered_channel&lt;&gt;</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">buffered_channel</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">value_type</phrase><phrase role="special">;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">iterator</phrase><phrase role="special">;</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">);</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">buffered_channel</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">buffered_channel</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">close</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">value_type</phrase> <phrase role="identifier">value_pop</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">);</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.channels.buffered_channel.h0"> <phrase id="fiber.synchronization.channels.buffered_channel.constructor"/><link linkend="fiber.synchronization.channels.buffered_channel.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="keyword">explicit</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Preconditions:</term> <listitem> <para> <code><phrase role="number">2</phrase><phrase role="special">&lt;=</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="number">0</phrase><phrase role="special">==(</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">&amp;</phrase> <phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">-</phrase><phrase role="number">1</phrase><phrase role="special">))</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> The constructor constructs an object of class <code><phrase role="identifier">buffered_channel</phrase></code> with an internal buffer of size <code><phrase role="identifier">capacity</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Error Conditions:</term> <listitem> <para> <emphasis role="bold">invalid_argument</emphasis>: if <code><phrase role="number">0</phrase><phrase role="special">==</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">||</phrase> <phrase role="number">0</phrase><phrase role="special">!=(</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">&amp;</phrase> <phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">-</phrase><phrase role="number">1</phrase><phrase role="special">))</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Notes:</term> <listitem> <para> A <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">push_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">push_wait_until</phrase><phrase role="special">()</phrase></code> will not block until the number of values in the channel becomes equal to <code><phrase role="identifier">capacity</phrase></code>. The channel can hold only <code><phrase role="identifier">capacity</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase></code> elements, otherwise it is considered to be full. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_close_bridgehead"> <phrase id="buffered_channel_close"/> <link linkend="buffered_channel_close">Member function <code>close</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">close</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Deactivates the channel. No values can be put after calling <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>. Fibers blocked in <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_until</phrase><phrase role="special">()</phrase></code> will return <code><phrase role="identifier">closed</phrase></code>. Fibers blocked in <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase></code> will receive an exception. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code> is like closing a pipe. It informs waiting consumers that no more values will arrive. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_push_bridgehead"> <phrase id="buffered_channel_push"/> <link linkend="buffered_channel_push">Member function <code>push</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If channel is closed, returns <code><phrase role="identifier">closed</phrase></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_until</phrase><phrase role="special">()</phrase></code> and returns <code><phrase role="identifier">success</phrase></code>. If the channel is full, the fiber is blocked. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_try_push_bridgehead"> <phrase id="buffered_channel_try_push"/> <link linkend="buffered_channel_try_push">Member function <code>try_push</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If channel is closed, returns <code><phrase role="identifier">closed</phrase></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_until</phrase><phrase role="special">()</phrase></code> and returns <code><phrase role="identifier">success</phrase></code>. If the channel is full, it doesn't block and returns <code><phrase role="identifier">full</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_pop_bridgehead"> <phrase id="buffered_channel_pop"/> <link linkend="buffered_channel_pop">Member function <code>pop</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value) or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_value_pop_bridgehead"> <phrase id="buffered_channel_value_pop"/> <link linkend="buffered_channel_value_pop">Member function <code>value_pop</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">value_type</phrase> <phrase role="identifier">value_pop</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (which throws an exception). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is closed or by copy- or move-operations. </para> </listitem> </varlistentry> <varlistentry> <term>Error conditions:</term> <listitem> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">errc</phrase><phrase role="special">::</phrase><phrase role="identifier">operation_not_permitted</phrase></code> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_try_pop_bridgehead"> <phrase id="buffered_channel_try_pop"/> <link linkend="buffered_channel_try_pop">Member function <code>try_pop</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">try_pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If channel is empty, returns <code><phrase role="identifier">empty</phrase></code>. If channel is closed, returns <code><phrase role="identifier">closed</phrase></code>. Otherwise it returns <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains the dequeued value. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_pop_wait_for_bridgehead"> <phrase id="buffered_channel_pop_wait_for"/> <link linkend="buffered_channel_pop_wait_for">Member function <code>pop_wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Accepts <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase></code> and internally computes a timeout time as (system time + <code><phrase role="identifier">timeout_duration</phrase></code>). If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value), or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>), or the system time reaches the computed timeout time (return value <code><phrase role="identifier">timeout</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions or by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="buffered_channel_pop_wait_until_bridgehead"> <phrase id="buffered_channel_pop_wait_until"/> <link linkend="buffered_channel_pop_wait_until">Member function <code>pop_wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Accepts a <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase></code>. If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value), or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>), or the system time reaches the passed <code><phrase role="identifier">time_point</phrase></code> (return value <code><phrase role="identifier">timeout</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions or by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.channels.buffered_channel.h1"> <phrase id="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"/><link linkend="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code><phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase></code></link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> Returns a range-iterator (input-iterator). </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.channels.buffered_channel.h2"> <phrase id="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"/><link linkend="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code><phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase></code></link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> Returns an end range-iterator (input-iterator). </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.synchronization.channels.unbuffered_channel"> <title><link linkend="fiber.synchronization.channels.unbuffered_channel">Unbuffered Channel</link></title> <para> <emphasis role="bold">Boost.Fiber</emphasis> provides template <code><phrase role="identifier">unbuffered_channel</phrase></code> suitable to synchonize fibers (running on same or different threads) via synchronous message passing. A fiber waiting to consume an value will block until the value is produced. If a fiber attempts to send a value through an unbuffered channel and no fiber is waiting to receive the value, the channel will block the sending fiber. </para> <para> The unbuffered channel acts as an <code><phrase role="identifier">rendezvous</phrase> <phrase role="identifier">point</phrase></code>. </para> <programlisting><phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">send</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="number">5</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">i</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">recv</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;received &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="identifier">chan</phrase><phrase role="special">{</phrase> <phrase role="number">1</phrase> <phrase role="special">};</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f1</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="identifier">send</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f2</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="identifier">recv</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> <phrase role="identifier">f2</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> Range-for syntax is supported: </para> <programlisting><phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">foo</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">5</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">8</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase> <phrase role="identifier">chan</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">bar</phrase><phrase role="special">(</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase> <phrase role="special">:</phrase> <phrase role="identifier">chan</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">value</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="class_unbuffered_channel_bridgehead"> <phrase id="class_unbuffered_channel"/> <link linkend="class_unbuffered_channel">Template <code>unbuffered_channel&lt;&gt;</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">unbuffered_channel</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">unbuffered_channel</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">value_type</phrase><phrase role="special">;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">iterator</phrase><phrase role="special">;</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">();</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">unbuffered_channel</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">unbuffered_channel</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">close</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">value_type</phrase> <phrase role="identifier">value_pop</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">);</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.channels.unbuffered_channel.h0"> <phrase id="fiber.synchronization.channels.unbuffered_channel.constructor"/><link linkend="fiber.synchronization.channels.unbuffered_channel.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="identifier">unbuffered_channel</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The constructor constructs an object of class <code><phrase role="identifier">unbuffered_channel</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_close_bridgehead"> <phrase id="unbuffered_channel_close"/> <link linkend="unbuffered_channel_close">Member function <code>close</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">close</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Deactivates the channel. No values can be put after calling <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>. Fibers blocked in <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_until</phrase><phrase role="special">()</phrase></code> will return <code><phrase role="identifier">closed</phrase></code>. Fibers blocked in <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase></code> will receive an exception. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code> is like closing a pipe. It informs waiting consumers that no more values will arrive. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_push_bridgehead"> <phrase id="unbuffered_channel_push"/> <link linkend="unbuffered_channel_push">Member function <code>push</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If channel is closed, returns <code><phrase role="identifier">closed</phrase></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase></code>, <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_for</phrase><phrase role="special">()</phrase></code> or <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop_wait_until</phrase><phrase role="special">()</phrase></code> and returns <code><phrase role="identifier">success</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_pop_bridgehead"> <phrase id="unbuffered_channel_pop"/> <link linkend="unbuffered_channel_pop">Member function <code>pop</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value) or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions thrown by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_value_pop_bridgehead"> <phrase id="unbuffered_channel_value_pop"/> <link linkend="unbuffered_channel_value_pop">Member function <code>value_pop</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">value_type</phrase> <phrase role="identifier">value_pop</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (which throws an exception). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> is closed or by copy- or move-operations. </para> </listitem> </varlistentry> <varlistentry> <term>Error conditions:</term> <listitem> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">errc</phrase><phrase role="special">::</phrase><phrase role="identifier">operation_not_permitted</phrase></code> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_pop_wait_for_bridgehead"> <phrase id="unbuffered_channel_pop_wait_for"/> <link linkend="unbuffered_channel_pop_wait_for">Member function <code>pop_wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Accepts <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase></code> and internally computes a timeout time as (system time + <code><phrase role="identifier">timeout_duration</phrase></code>). If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value), or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>), or the system time reaches the computed timeout time (return value <code><phrase role="identifier">timeout</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions or by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="unbuffered_channel_pop_wait_until_bridgehead"> <phrase id="unbuffered_channel_pop_wait_until"/> <link linkend="unbuffered_channel_pop_wait_until">Member function <code>pop_wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_op_status</phrase> <phrase role="identifier">pop_wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">va</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Accepts a <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase></code>. If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code>ed (return value <code><phrase role="identifier">success</phrase></code> and <code><phrase role="identifier">va</phrase></code> contains dequeued value), or the channel gets <code><phrase role="identifier">close</phrase><phrase role="special">()</phrase></code>d (return value <code><phrase role="identifier">closed</phrase></code>), or the system time reaches the passed <code><phrase role="identifier">time_point</phrase></code> (return value <code><phrase role="identifier">timeout</phrase></code>). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> timeout-related exceptions or by copy- or move-operations. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.channels.unbuffered_channel.h1"> <phrase id="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"/><link linkend="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code><phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase></code></link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> Returns a range-iterator (input-iterator). </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.channels.unbuffered_channel.h2"> <phrase id="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"/><link linkend="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code><phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase></code></link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">(</phrase> <phrase role="identifier">unbuffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> Returns an end range-iterator (input-iterator). </para> </listitem> </varlistentry> </variablelist> </section> </section> <section id="fiber.synchronization.futures"> <title><link linkend="fiber.synchronization.futures">Futures</link></title> <bridgehead renderas="sect4" id="fiber.synchronization.futures.h0"> <phrase id="fiber.synchronization.futures.overview"/><link linkend="fiber.synchronization.futures.overview">Overview</link> </bridgehead> <para> The futures library provides a means of handling asynchronous future values, whether those values are generated by another fiber, or on a single fiber in response to external stimuli, or on-demand. </para> <para> This is done through the provision of four class templates: <link linkend="class_future"><code>future&lt;&gt;</code></link> and <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> which are used to retrieve the asynchronous results, and <link linkend="class_promise"><code>promise&lt;&gt;</code></link> and <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> which are used to generate the asynchronous results. </para> <para> An instance of <link linkend="class_future"><code>future&lt;&gt;</code></link> holds the one and only reference to a result. Ownership can be transferred between instances using the move constructor or move-assignment operator, but at most one instance holds a reference to a given asynchronous result. When the result is ready, it is returned from <link linkend="future_get"><code>future::get()</code></link> by rvalue-reference to allow the result to be moved or copied as appropriate for the type. </para> <para> On the other hand, many instances of <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> may reference the same result. Instances can be freely copied and assigned, and <link linkend="shared_future_get"><code>shared_future::get()</code></link> returns a <code><phrase role="keyword">const</phrase></code> reference so that multiple calls to <link linkend="shared_future_get"><code>shared_future::get()</code></link> are safe. You can move an instance of <link linkend="class_future"><code>future&lt;&gt;</code></link> into an instance of <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link>, thus transferring ownership of the associated asynchronous result, but not vice-versa. </para> <para> <link linkend="fibers_async"><code>fibers::async()</code></link> is a simple way of running asynchronous tasks. A call to <code><phrase role="identifier">async</phrase><phrase role="special">()</phrase></code> spawns a fiber and returns a <link linkend="class_future"><code>future&lt;&gt;</code></link> that will deliver the result of the fiber function. </para> <bridgehead renderas="sect4" id="fiber.synchronization.futures.h1"> <phrase id="fiber.synchronization.futures.creating_asynchronous_values"/><link linkend="fiber.synchronization.futures.creating_asynchronous_values">Creating asynchronous values</link> </bridgehead> <para> You can set the value in a future with either a <link linkend="class_promise"><code>promise&lt;&gt;</code></link> or a <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link>. A <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> is a callable object with <code><phrase role="keyword">void</phrase></code> return that wraps a function or callable object returning the specified type. When the <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> is invoked, it invokes the contained function in turn, and populates a future with the contained function's return value. This is an answer to the perennial question: <quote>How do I return a value from a fiber?</quote> Package the function you wish to run as a <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> and pass the packaged task to the fiber constructor. The future retrieved from the packaged task can then be used to obtain the return value. If the function throws an exception, that is stored in the future in place of the return value. </para> <programlisting><phrase role="keyword">int</phrase> <phrase role="identifier">calculate_the_answer_to_life_the_universe_and_everything</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="number">42</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">int</phrase><phrase role="special">()&gt;</phrase> <phrase role="identifier">pt</phrase><phrase role="special">(</phrase><phrase role="identifier">calculate_the_answer_to_life_the_universe_and_everything</phrase><phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">int</phrase><phrase role="special">&gt;</phrase> <phrase role="identifier">fi</phrase><phrase role="special">=</phrase><phrase role="identifier">pt</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">pt</phrase><phrase role="special">)).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="comment">// launch task on a fiber</phrase> <phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="comment">// wait for it to finish</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">is_ready</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">has_value</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">has_exception</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()==</phrase><phrase role="number">42</phrase><phrase role="special">);</phrase> </programlisting> <para> A <link linkend="class_promise"><code>promise&lt;&gt;</code></link> is a bit more low level: it just provides explicit functions to store a value or an exception in the associated future. A promise can therefore be used where the value might come from more than one possible source. </para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">int</phrase><phrase role="special">&gt;</phrase> <phrase role="identifier">pi</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">int</phrase><phrase role="special">&gt;</phrase> <phrase role="identifier">fi</phrase><phrase role="special">;</phrase> <phrase role="identifier">fi</phrase><phrase role="special">=</phrase><phrase role="identifier">pi</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> <phrase role="identifier">pi</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase><phrase role="number">42</phrase><phrase role="special">);</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">is_ready</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">has_value</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">has_exception</phrase><phrase role="special">());</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">fi</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()==</phrase><phrase role="number">42</phrase><phrase role="special">);</phrase> </programlisting> <section id="fiber.synchronization.futures.future"> <title><link linkend="fiber.synchronization.futures.future">Future</link></title> <para> A future provides a mechanism to access the result of an asynchronous operation. </para> <anchor id="shared_state"/> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h0"> <phrase id="fiber.synchronization.futures.future.shared_state"/><link linkend="fiber.synchronization.futures.future.shared_state">shared state</link> </bridgehead> <para> Behind a <link linkend="class_promise"><code>promise&lt;&gt;</code></link> and its <link linkend="class_future"><code>future&lt;&gt;</code></link> lies an unspecified object called their <emphasis>shared state</emphasis>. The shared state is what will actually hold the async result (or the exception). </para> <para> The shared state is instantiated along with the <link linkend="class_promise"><code>promise&lt;&gt;</code></link>. </para> <para> Aside from its originating <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code>, a <link linkend="class_future"><code>future&lt;&gt;</code></link> holds a unique reference to a particular shared state. However, multiple <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> instances can reference the same underlying shared state. </para> <para> As <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> and <link linkend="fibers_async"><code>fibers::async()</code></link> are implemented using <link linkend="class_promise"><code>promise&lt;&gt;</code></link>, discussions of shared state apply to them as well. </para> <anchor id="class_future_status"/> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h1"> <phrase id="fiber.synchronization.futures.future.enumeration__code__phrase_role__identifier__future_status__phrase___code_"/><link linkend="fiber.synchronization.futures.future.enumeration__code__phrase_role__identifier__future_status__phrase___code_">Enumeration <code><phrase role="identifier">future_status</phrase></code></link> </bridgehead> <para> Timed wait-operations (<link linkend="future_wait_for"><code>future::wait_for()</code></link> and <link linkend="future_wait_until"><code>future::wait_until()</code></link>) return the state of the future. </para> <programlisting><phrase role="keyword">enum</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">future_status</phrase> <phrase role="special">{</phrase> <phrase role="identifier">ready</phrase><phrase role="special">,</phrase> <phrase role="identifier">timeout</phrase><phrase role="special">,</phrase> <phrase role="identifier">deferred</phrase> <phrase role="comment">// not supported yet</phrase> <phrase role="special">};</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h2"> <phrase id="fiber.synchronization.futures.future._code__phrase_role__identifier__ready__phrase___code_"/><link linkend="fiber.synchronization.futures.future._code__phrase_role__identifier__ready__phrase___code_"><code><phrase role="identifier">ready</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The <link linkend="shared_state">shared state</link> is ready. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h3"> <phrase id="fiber.synchronization.futures.future._code__phrase_role__identifier__timeout__phrase___code_"/><link linkend="fiber.synchronization.futures.future._code__phrase_role__identifier__timeout__phrase___code_"><code><phrase role="identifier">timeout</phrase></code></link> </bridgehead> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The <link linkend="shared_state">shared state</link> did not become ready before timeout has passed. </para> </listitem> </varlistentry> </variablelist> <note> <para> Deferred futures are not supported. </para> </note> <para> <bridgehead renderas="sect4" id="class_future_bridgehead"> <phrase id="class_future"/> <link linkend="class_future">Template <code>future&lt;&gt;</code></link> </bridgehead> </para> <para> A <link linkend="class_future"><code>future&lt;&gt;</code></link> contains a <link linkend="shared_state">shared state</link> which is not shared with any other future. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">future</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">future</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">~</phrase><phrase role="identifier">future</phrase><phrase role="special">();</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">share</phrase><phrase role="special">();</phrase> <phrase role="identifier">R</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of generic future template</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of future&lt; R &amp; &gt; template specialization</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of future&lt; void &gt; template specialization</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">get_exception_ptr</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h4"> <phrase id="fiber.synchronization.futures.future.default_constructor"/><link linkend="fiber.synchronization.futures.future.default_constructor">Default constructor</link> </bridgehead> <programlisting><phrase role="identifier">future</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a future with no <link linkend="shared_state">shared state</link>. After construction <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h5"> <phrase id="fiber.synchronization.futures.future.move_constructor"/><link linkend="fiber.synchronization.futures.future.move_constructor">Move constructor</link> </bridgehead> <programlisting><phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs a future with the <link linkend="shared_state">shared state</link> of other. After construction <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h6"> <phrase id="fiber.synchronization.futures.future.destructor"/><link linkend="fiber.synchronization.futures.future.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">future</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys the future; ownership is abandoned. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code>~future()</code> does <emphasis>not</emphasis> block the calling fiber. </para> </listitem> </varlistentry> </variablelist> <para> Consider a sequence such as: </para> <orderedlist> <listitem> <simpara> instantiate <link linkend="class_promise"><code>promise&lt;&gt;</code></link> </simpara> </listitem> <listitem> <simpara> obtain its <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> via <link linkend="promise_get_future"><code>promise::get_future()</code></link> </simpara> </listitem> <listitem> <simpara> launch <link linkend="class_fiber"><code>fiber</code></link>, capturing <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code> </simpara> </listitem> <listitem> <simpara> destroy <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> </simpara> </listitem> <listitem> <simpara> call <link linkend="promise_set_value"><code>promise::set_value()</code></link> </simpara> </listitem> </orderedlist> <para> The final <code><phrase role="identifier">set_value</phrase><phrase role="special">()</phrase></code> call succeeds, but the value is silently discarded: no additional <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> can be obtained from that <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code>. </para> <para> <bridgehead renderas="sect4" id="future_operator_assign_bridgehead"> <phrase id="future_operator_assign"/> <link linkend="future_operator_assign">Member function <code>operator=</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Moves the <link linkend="shared_state">shared state</link> of other to <code><phrase role="keyword">this</phrase></code>. After the assignment, <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_valid_bridgehead"> <phrase id="future_valid"/> <link linkend="future_valid">Member function <code>valid</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Returns <code><phrase role="keyword">true</phrase></code> if future contains a <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_share_bridgehead"> <phrase id="future_share"/> <link linkend="future_share">Member function <code>share</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">shared_future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">share</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Move the state to a <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> a <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> containing the <link linkend="shared_state">shared state</link> formerly belonging to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_get_bridgehead"> <phrase id="future_get"/> <link linkend="future_get">Member function <code>get</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">R</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of generic future template</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of future&lt; R &amp; &gt; template specialization</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of future&lt; void &gt; template specialization</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="keyword">true</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. If <link linkend="promise_set_value"><code>promise::set_value()</code></link> is called, returns the value. If <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, throws the indicated exception. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>, <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">broken_promise</phrase></code>. Any exception passed to <code><phrase role="identifier">promise</phrase><phrase role="special">::</phrase><phrase role="identifier">set_exception</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_get_exception_ptr_bridgehead"> <phrase id="future_get_exception_ptr"/> <link linkend="future_get_exception_ptr">Member function <code>get_exception_ptr</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">get_exception_ptr</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="keyword">true</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. If <code><phrase role="identifier">set_value</phrase><phrase role="special">()</phrase></code> is called, returns a default-constructed <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>. If <code><phrase role="identifier">set_exception</phrase><phrase role="special">()</phrase></code> is called, returns the passed <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase></code> does <emphasis>not</emphasis> invalidate the <code>future</code>. After calling <code><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase></code>, you may still call <link linkend="future_get"><code>future::get()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_wait_bridgehead"> <phrase id="future_wait"/> <link linkend="future_wait">Member function <code>wait</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_wait_for_bridgehead"> <phrase id="future_wait_for"/> <link linkend="future_wait_for">Templated member function <code>wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, or <code><phrase role="identifier">timeout_duration</phrase></code> has passed. </para> </listitem> </varlistentry> <varlistentry> <term>Result:</term> <listitem> <para> A <code><phrase role="identifier">future_status</phrase></code> is returned indicating the reason for returning. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code> or timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="future_wait_until_bridgehead"> <phrase id="future_wait_until"/> <link linkend="future_wait_until">Templated member function <code>wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, or <code><phrase role="identifier">timeout_time</phrase></code> has passed. </para> </listitem> </varlistentry> <varlistentry> <term>Result:</term> <listitem> <para> A <code><phrase role="identifier">future_status</phrase></code> is returned indicating the reason for returning. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code> or timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_shared_future_bridgehead"> <phrase id="class_shared_future"/> <link linkend="class_shared_future">Template <code>shared_future&lt;&gt;</code></link> </bridgehead> </para> <para> A <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> contains a <link linkend="shared_state">shared state</link> which might be shared with other <link linkend="class_shared_future"><code>shared_future&lt;&gt;</code></link> instances. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">~</phrase><phrase role="identifier">shared_future</phrase><phrase role="special">();</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">R</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of generic shared_future template</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of shared_future&lt; R &amp; &gt; template specialization</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of shared_future&lt; void &gt; template specialization</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">get_exception_ptr</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h7"> <phrase id="fiber.synchronization.futures.future.default_constructor0"/><link linkend="fiber.synchronization.futures.future.default_constructor0">Default constructor</link> </bridgehead> <programlisting><phrase role="identifier">shared_future</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a shared_future with no <link linkend="shared_state">shared state</link>. After construction <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h8"> <phrase id="fiber.synchronization.futures.future.move_constructor0"/><link linkend="fiber.synchronization.futures.future.move_constructor0">Move constructor</link> </bridgehead> <programlisting><phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs a shared_future with the <link linkend="shared_state">shared state</link> of other. After construction <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h9"> <phrase id="fiber.synchronization.futures.future.copy_constructor"/><link linkend="fiber.synchronization.futures.future.copy_constructor">Copy constructor</link> </bridgehead> <programlisting><phrase role="identifier">shared_future</phrase><phrase role="special">(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs a shared_future with the <link linkend="shared_state">shared state</link> of other. After construction <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> is unchanged. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.future.h10"> <phrase id="fiber.synchronization.futures.future.destructor0"/><link linkend="fiber.synchronization.futures.future.destructor0">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">shared_future</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys the shared_future; ownership is abandoned if not shared. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code>~shared_future()</code> does <emphasis>not</emphasis> block the calling fiber. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_operator_assign_bridgehead"> <phrase id="shared_future_operator_assign"/> <link linkend="shared_future_operator_assign">Member function <code>operator=</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">shared_future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Moves or copies the <link linkend="shared_state">shared state</link> of other to <code><phrase role="keyword">this</phrase></code>. After the assignment, the state of <code><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> depends on which overload was invoked: unchanged for the overload accepting <code><phrase role="identifier">shared_future</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase></code>, otherwise <code><phrase role="keyword">false</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_valid_bridgehead"> <phrase id="shared_future_valid"/> <link linkend="shared_future_valid">Member function <code>valid</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Returns <code><phrase role="keyword">true</phrase></code> if shared_future contains a <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_get_bridgehead"> <phrase id="shared_future_get"/> <link linkend="shared_future_get">Member function <code>get</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">R</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of generic shared_future template</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of shared_future&lt; R &amp; &gt; template specialization</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of shared_future&lt; void &gt; template specialization</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="keyword">true</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. If <link linkend="promise_set_value"><code>promise::set_value()</code></link> is called, returns the value. If <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, throws the indicated exception. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">false</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>, <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">broken_promise</phrase></code>. Any exception passed to <code><phrase role="identifier">promise</phrase><phrase role="special">::</phrase><phrase role="identifier">set_exception</phrase><phrase role="special">()</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_get_exception_ptr_bridgehead"> <phrase id="shared_future_get_exception_ptr"/> <link linkend="shared_future_get_exception_ptr">Member function <code>get_exception_ptr</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">get_exception_ptr</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Precondition:</term> <listitem> <para> <code><phrase role="keyword">true</phrase> <phrase role="special">==</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. If <code><phrase role="identifier">set_value</phrase><phrase role="special">()</phrase></code> is called, returns a default-constructed <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>. If <code><phrase role="identifier">set_exception</phrase><phrase role="special">()</phrase></code> is called, returns the passed <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> <code><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase></code> does <emphasis>not</emphasis> invalidate the <code>shared_future</code>. After calling <code><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase></code>, you may still call <link linkend="shared_future_get"><code>shared_future::get()</code></link>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_wait_bridgehead"> <phrase id="shared_future_wait"/> <link linkend="shared_future_wait">Member function <code>wait</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_wait_for_bridgehead"> <phrase id="shared_future_wait_for"/> <link linkend="shared_future_wait_for">Templated member function <code>wait_for</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">duration</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Rep</phrase><phrase role="special">,</phrase> <phrase role="identifier">Period</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_duration</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, or <code><phrase role="identifier">timeout_duration</phrase></code> has passed. </para> </listitem> </varlistentry> <varlistentry> <term>Result:</term> <listitem> <para> A <code><phrase role="identifier">future_status</phrase></code> is returned indicating the reason for returning. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code> or timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="shared_future_wait_until_bridgehead"> <phrase id="shared_future_wait_until"/> <link linkend="shared_future_wait_until">Templated member function <code>wait_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_status</phrase> <phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Clock</phrase><phrase role="special">,</phrase> <phrase role="identifier">Duration</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">timeout_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Waits until <link linkend="promise_set_value"><code>promise::set_value()</code></link> or <link linkend="promise_set_exception"><code>promise::set_exception()</code></link> is called, or <code><phrase role="identifier">timeout_time</phrase></code> has passed. </para> </listitem> </varlistentry> <varlistentry> <term>Result:</term> <listitem> <para> A <code><phrase role="identifier">future_status</phrase></code> is returned indicating the reason for returning. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code> or timeout-related exceptions. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fibers_async_bridgehead"> <phrase id="fibers_async"/> <link linkend="fibers_async">Non-member function <code>fibers::async()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">/</phrase><phrase role="identifier">async</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Function</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">async</phrase><phrase role="special">(</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Function</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">async</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link> <phrase role="identifier">policy</phrase><phrase role="special">,</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Function</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">async</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link> <phrase role="identifier">policy</phrase><phrase role="special">,</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Function</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">async</phrase><phrase role="special">(</phrase> <link linkend="class_launch"><code><phrase role="identifier">launch</phrase></code></link> <phrase role="identifier">policy</phrase><phrase role="special">,</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> <phrase role="special">}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Executes <code><phrase role="identifier">fn</phrase></code> in a <link linkend="class_fiber"><code>fiber</code></link> and returns an associated <link linkend="class_future"><code>future&lt;&gt;</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Result:</term> <listitem> <para> <programlisting><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Function</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay_t</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase></programlisting> representing the <link linkend="shared_state">shared state</link> associated with the asynchronous execution of <code><phrase role="identifier">fn</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> or <code><phrase role="identifier">future_error</phrase></code> if an error occurs. </para> </listitem> </varlistentry> <varlistentry> <term>Notes:</term> <listitem> <para> The overloads accepting <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink> use the passed <link linkend="stack_allocator_concept"><code><phrase role="identifier">StackAllocator</phrase></code></link> when constructing the launched <code><phrase role="identifier">fiber</phrase></code>. The overloads accepting <link linkend="class_launch"><code>launch</code></link> use the passed <code><phrase role="identifier">launch</phrase></code> when constructing the launched <code><phrase role="identifier">fiber</phrase></code>. The default <code><phrase role="identifier">launch</phrase></code> is <code><phrase role="identifier">post</phrase></code>, as for the <code><phrase role="identifier">fiber</phrase></code> constructor. </para> </listitem> </varlistentry> </variablelist> <note> <para> Deferred futures are not supported. </para> </note> </section> <section id="fiber.synchronization.futures.promise"> <title><anchor id="class_promise"/><link linkend="fiber.synchronization.futures.promise">Template <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code></link></title> <para> A <link linkend="class_promise"><code>promise&lt;&gt;</code></link> provides a mechanism to store a value (or exception) that can later be retrieved from the corresponding <link linkend="class_future"><code>future&lt;&gt;</code></link> object. <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code> and <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> communicate via their underlying <link linkend="shared_state">shared state</link>. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">/</phrase><phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">promise</phrase><phrase role="special">();</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <ulink url="http://en.cppreference.com/w/cpp/concept/Allocator"><code><phrase role="identifier">Allocator</phrase></code></ulink> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">);</phrase> <phrase role="identifier">promise</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">promise</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="special">~</phrase><phrase role="identifier">promise</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;);</phrase> <phrase role="comment">// member only of generic promise template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;&amp;);</phrase> <phrase role="comment">// member only of generic promise template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;);</phrase> <phrase role="comment">// member only of promise&lt; R &amp; &gt; template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of promise&lt; void &gt; template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">p</phrase><phrase role="special">);</phrase> <phrase role="special">};</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;,</phrase> <phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.futures.promise.h0"> <phrase id="fiber.synchronization.futures.promise.default_constructor"/><link linkend="fiber.synchronization.futures.promise.default_constructor">Default constructor</link> </bridgehead> <programlisting><phrase role="identifier">promise</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a promise with an empty <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions caused by memory allocation. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.promise.h1"> <phrase id="fiber.synchronization.futures.promise.constructor"/><link linkend="fiber.synchronization.futures.promise.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <ulink url="http://en.cppreference.com/w/cpp/concept/Allocator"><code><phrase role="identifier">Allocator</phrase></code></ulink> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a promise with an empty <link linkend="shared_state">shared state</link> by using <code><phrase role="identifier">alloc</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions caused by memory allocation. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink> </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.promise.h2"> <phrase id="fiber.synchronization.futures.promise.move_constructor"/><link linkend="fiber.synchronization.futures.promise.move_constructor">Move constructor</link> </bridgehead> <programlisting><phrase role="identifier">promise</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a promise by moving the <link linkend="shared_state">shared state</link> from <code><phrase role="identifier">other</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">other</phrase></code> contains no valid shared state. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.promise.h3"> <phrase id="fiber.synchronization.futures.promise.destructor"/><link linkend="fiber.synchronization.futures.promise.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">promise</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> and abandons the <link linkend="shared_state">shared state</link> if shared state is ready; otherwise stores <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">broken_promise</phrase></code> as if by <link linkend="promise_set_exception"><code>promise::set_exception()</code></link>: the shared state is set ready. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="promise_operator_assign_bridgehead"> <phrase id="promise_operator_assign"/> <link linkend="promise_operator_assign">Member function <code>operator=</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">promise</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Transfers the ownership of <link linkend="shared_state">shared state</link> to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">other</phrase></code> contains no valid shared state. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="promise_swap_bridgehead"> <phrase id="promise_swap"/> <link linkend="promise_swap">Member function <code>swap</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Swaps the <link linkend="shared_state">shared state</link> between other and <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="promise_get_future_bridgehead"> <phrase id="promise_get_future"/> <link linkend="promise_get_future">Member function <code>get_future</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> A <link linkend="class_future"><code>future&lt;&gt;</code></link> with the same <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">future_already_retrieved</phrase></code> or <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="promise_set_value_bridgehead"> <phrase id="promise_set_value"/> <link linkend="promise_set_value">Member function <code>set_value</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase> <phrase role="comment">// member only of generic promise template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase> <phrase role="comment">// member only of generic promise template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase> <phrase role="comment">// member only of promise&lt; R &amp; &gt; template</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_value</phrase><phrase role="special">();</phrase> <phrase role="comment">// member only of promise&lt; void &gt; template</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Store the result in the <link linkend="shared_state">shared state</link> and marks the state as ready. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">future_already_satisfied</phrase></code> or <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="promise_set_exception_bridgehead"> <phrase id="promise_set_exception"/> <link linkend="promise_set_exception">Member function <code>set_exception</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">set_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Store an exception pointer in the <link linkend="shared_state">shared state</link> and marks the state as ready. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">future_already_satisfied</phrase></code> or <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="swap_for_promise_bridgehead"> <phrase id="swap_for_promise"/> <link linkend="swap_for_promise">Non-member function <code>swap()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Same as <code><phrase role="identifier">l</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.synchronization.futures.packaged_task"> <title><anchor id="class_packaged_task"/><link linkend="fiber.synchronization.futures.packaged_task">Template <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code></link></title> <para> A <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link> wraps a callable target that returns a value so that the return value can be computed asynchronously. </para> <para> Conventional usage of <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code> is like this: </para> <orderedlist> <listitem> <simpara> Instantiate <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code> with template arguments matching the signature of the callable. Pass the callable to the <link linkend="packaged_task_packaged_task">constructor</link>. </simpara> </listitem> <listitem> <simpara> Call <link linkend="packaged_task_get_future"><code>packaged_task::get_future()</code></link> and capture the returned <link linkend="class_future"><code>future&lt;&gt;</code></link> instance. </simpara> </listitem> <listitem> <simpara> Launch a <link linkend="class_fiber"><code>fiber</code></link> to run the new <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code>, passing any arguments required by the original callable. </simpara> </listitem> <listitem> <simpara> Call <link linkend="fiber_detach"><code>fiber::detach()</code></link> on the newly-launched <code><phrase role="identifier">fiber</phrase></code>. </simpara> </listitem> <listitem> <simpara> At some later point, retrieve the result from the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code>. </simpara> </listitem> </orderedlist> <para> This is, in fact, pretty much what <link linkend="fibers_async"><code>fibers::async()</code></link> encapsulates. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">future</phrase><phrase role="special">/</phrase><phrase role="identifier">packaged_task</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">R</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase><phrase role="special">(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <ulink url="http://en.cppreference.com/w/cpp/concept/Allocator"><code><phrase role="identifier">Allocator</phrase></code></ulink> <phrase role="special">&gt;</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;);</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="special">~</phrase><phrase role="identifier">packaged_task</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">...);</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">reset</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;,</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect5" id="fiber.synchronization.futures.packaged_task.h0"> <phrase id="fiber.synchronization.futures.packaged_task.default_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_"/><link linkend="fiber.synchronization.futures.packaged_task.default_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_">Default constructor <code><phrase role="identifier">packaged_task</phrase><phrase role="special">()</phrase></code></link> </bridgehead> <programlisting><phrase role="identifier">packaged_task</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs an object of class <code><phrase role="identifier">packaged_task</phrase></code> with no <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <anchor id="packaged_task_packaged_task"/> <bridgehead renderas="sect5" id="fiber.synchronization.futures.packaged_task.h1"> <phrase id="fiber.synchronization.futures.packaged_task.templated_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_"/><link linkend="fiber.synchronization.futures.packaged_task.templated_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_">Templated constructor <code><phrase role="identifier">packaged_task</phrase><phrase role="special">()</phrase></code></link> </bridgehead> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <ulink url="http://en.cppreference.com/w/cpp/concept/Allocator"><code><phrase role="identifier">Allocator</phrase></code></ulink> <phrase role="special">&gt;</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs an object of class <code><phrase role="identifier">packaged_task</phrase></code> with a <link linkend="shared_state">shared state</link> and copies or moves the callable target <code><phrase role="identifier">fn</phrase></code> to internal storage. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exceptions caused by memory allocation. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> The signature of <code><phrase role="identifier">Fn</phrase></code> should have a return type convertible to <code><phrase role="identifier">R</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>See also:</term> <listitem> <para> <ulink url="http://en.cppreference.com/w/cpp/memory/allocator_arg_t"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase></code></ulink> </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.packaged_task.h2"> <phrase id="fiber.synchronization.futures.packaged_task.move_constructor"/><link linkend="fiber.synchronization.futures.packaged_task.move_constructor">Move constructor</link> </bridgehead> <programlisting><phrase role="identifier">packaged_task</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Creates a packaged_task by moving the <link linkend="shared_state">shared state</link> from <code><phrase role="identifier">other</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">other</phrase></code> contains no valid shared state. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect5" id="fiber.synchronization.futures.packaged_task.h3"> <phrase id="fiber.synchronization.futures.packaged_task.destructor"/><link linkend="fiber.synchronization.futures.packaged_task.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">packaged_task</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Destroys <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> and abandons the <link linkend="shared_state">shared state</link> if shared state is ready; otherwise stores <code><phrase role="identifier">future_error</phrase></code> with error condition <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">broken_promise</phrase></code> as if by <link linkend="promise_set_exception"><code>promise::set_exception()</code></link>: the shared state is set ready. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_operator_assign_bridgehead"> <phrase id="packaged_task_operator_assign"/> <link linkend="packaged_task_operator_assign">Member function <code>operator=</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Transfers the ownership of <link linkend="shared_state">shared state</link> to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="identifier">other</phrase></code> contains no valid shared state. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_swap_bridgehead"> <phrase id="packaged_task_swap"/> <link linkend="packaged_task_swap">Member function <code>swap</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Swaps the <link linkend="shared_state">shared state</link> between other and <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_valid_bridgehead"> <phrase id="packaged_task_valid"/> <link linkend="packaged_task_valid">Member function <code>valid</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Returns <code><phrase role="keyword">true</phrase></code> if <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code> contains a <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_get_future_bridgehead"> <phrase id="packaged_task_get_future"/> <link linkend="packaged_task_get_future">Member function <code>get_future</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">R</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> A <link linkend="class_future"><code>future&lt;&gt;</code></link> with the same <link linkend="shared_state">shared state</link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">future_already_retrieved</phrase></code> or <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_operator_apply_bridgehead"> <phrase id="packaged_task_operator_apply"/> <link linkend="packaged_task_operator_apply">Member function <code>operator()</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">args</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Invokes the stored callable target. Any exception thrown by the callable target <code><phrase role="identifier">fn</phrase></code> is stored in the <link linkend="shared_state">shared state</link> as if by <link linkend="promise_set_exception"><code>promise::set_exception()</code></link>. Otherwise, the value returned by <code><phrase role="identifier">fn</phrase></code> is stored in the shared state as if by <link linkend="promise_set_value"><code>promise::set_value()</code></link>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="packaged_task_reset_bridgehead"> <phrase id="packaged_task_reset"/> <link linkend="packaged_task_reset">Member function <code>reset</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">reset</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Resets the <link linkend="shared_state">shared state</link> and abandons the result of previous executions. A new shared state is constructed. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">future_error</phrase></code> with <code><phrase role="identifier">future_errc</phrase><phrase role="special">::</phrase><phrase role="identifier">no_state</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="swap_for_packaged_task_bridgehead"> <phrase id="swap_for_packaged_task"/> <link linkend="swap_for_packaged_task">Non-member function <code>swap()</code></link> </bridgehead> </para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">l</phrase><phrase role="special">,</phrase> <phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Signature</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Same as <code><phrase role="identifier">l</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase> <phrase role="identifier">r</phrase><phrase role="special">)</phrase></code>. </para> </listitem> </varlistentry> </variablelist> </section> </section> </section> <section id="fiber.fls"> <title><link linkend="fiber.fls">Fiber local storage</link></title> <bridgehead renderas="sect3" id="fiber.fls.h0"> <phrase id="fiber.fls.synopsis"/><link linkend="fiber.fls.synopsis">Synopsis</link> </bridgehead> <para> Fiber local storage allows a separate instance of a given data item for each fiber. </para> <bridgehead renderas="sect3" id="fiber.fls.h1"> <phrase id="fiber.fls.cleanup_at_fiber_exit"/><link linkend="fiber.fls.cleanup_at_fiber_exit">Cleanup at fiber exit</link> </bridgehead> <para> When a fiber exits, the objects associated with each <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> instance are destroyed. By default, the object pointed to by a pointer <code><phrase role="identifier">p</phrase></code> is destroyed by invoking <code><phrase role="keyword">delete</phrase> <phrase role="identifier">p</phrase></code>, but this can be overridden for a specific instance of <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> by providing a cleanup routine <code><phrase role="identifier">func</phrase></code> to the constructor. In this case, the object is destroyed by invoking <code><phrase role="identifier">func</phrase><phrase role="special">(</phrase><phrase role="identifier">p</phrase><phrase role="special">)</phrase></code>. The cleanup functions are called in an unspecified order. </para> <para> <bridgehead renderas="sect4" id="class_fiber_specific_ptr_bridgehead"> <phrase id="class_fiber_specific_ptr"/> <link linkend="class_fiber_specific_ptr">Class <code>fiber_specific_ptr</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">fss</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">fiber_specific_ptr</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">element_type</phrase><phrase role="special">;</phrase> <phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">();</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">(</phrase> <phrase role="keyword">void</phrase><phrase role="special">(*</phrase><phrase role="identifier">fn</phrase><phrase role="special">)(</phrase><phrase role="identifier">T</phrase><phrase role="special">*)</phrase> <phrase role="special">);</phrase> <phrase role="special">~</phrase><phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">();</phrase> <phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber_specific_ptr</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">fiber_specific_ptr</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">fiber_specific_ptr</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="keyword">operator</phrase><phrase role="special">-&gt;()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">*()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">release</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">reset</phrase><phrase role="special">(</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*);</phrase> <phrase role="special">};</phrase> <phrase role="special">}}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.fls.h2"> <phrase id="fiber.fls.constructor"/><link linkend="fiber.fls.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">();</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">(</phrase> <phrase role="keyword">void</phrase><phrase role="special">(*</phrase><phrase role="identifier">fn</phrase><phrase role="special">)(</phrase><phrase role="identifier">T</phrase><phrase role="special">*)</phrase> <phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Requires:</term> <listitem> <para> <code><phrase role="keyword">delete</phrase> <phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> is well-formed; <code><phrase role="identifier">fn</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">())</phrase></code> does not throw </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Construct a <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> object for storing a pointer to an object of type <code><phrase role="identifier">T</phrase></code> specific to each fiber. When <code><phrase role="identifier">reset</phrase><phrase role="special">()</phrase></code> is called, or the fiber exits, <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> calls <code><phrase role="identifier">fn</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">())</phrase></code>. If the no-arguments constructor is used, the default <code><phrase role="keyword">delete</phrase></code>-based cleanup function will be used to destroy the fiber-local objects. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">fiber_error</phrase></code> if an error occurs. </para> </listitem> </varlistentry> </variablelist> <bridgehead renderas="sect3" id="fiber.fls.h3"> <phrase id="fiber.fls.destructor"/><link linkend="fiber.fls.destructor">Destructor</link> </bridgehead> <programlisting><phrase role="special">~</phrase><phrase role="identifier">fiber_specific_ptr</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Requires:</term> <listitem> <para> All the fiber specific instances associated to this <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> (except maybe the one associated to this fiber) must be nullptr. </para> </listitem> </varlistentry> <varlistentry> <term>Effects:</term> <listitem> <para> Calls <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">reset</phrase><phrase role="special">()</phrase></code> to clean up the associated value for the current fiber, and destroys <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Remarks:</term> <listitem> <para> The requirement is an implementation restriction. If the destructor promised to delete instances for all fibers, the implementation would be forced to maintain a list of all the fibers having an associated specific ptr, which is against the goal of fiber specific data. In general, a <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> should outlive the fibers that use it. </para> </listitem> </varlistentry> </variablelist> <note> <para> Care needs to be taken to ensure that any fibers still running after an instance of <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> has been destroyed do not call any member functions on that instance. </para> </note> <para> <bridgehead renderas="sect4" id="fiber_specific_ptr_get_bridgehead"> <phrase id="fiber_specific_ptr_get"/> <link linkend="fiber_specific_ptr_get">Member function <code>get</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> The pointer associated with the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <note> <para> The initial value associated with an instance of <link linkend="class_fiber_specific_ptr"><code>fiber_specific_ptr</code></link> is <code><phrase role="keyword">nullptr</phrase></code> for each fiber. </para> </note> <para> <bridgehead renderas="sect4" id="fiber_specific_ptr_operator_arrow_bridgehead"> <phrase id="fiber_specific_ptr_operator_arrow"/> <link linkend="fiber_specific_ptr_operator_arrow">Member function <code>operator-&gt;</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="keyword">operator</phrase><phrase role="special">-&gt;()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Requires:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> is not <code><phrase role="keyword">nullptr</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_specific_ptr_operator_star_bridgehead"> <phrase id="fiber_specific_ptr_operator_star"/> <link linkend="fiber_specific_ptr_operator_star">Member function <code>operator*</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">T</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">*()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Requires:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> is not <code><phrase role="keyword">nullptr</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="special">*(</phrase><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">())</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_specific_ptr_release_bridgehead"> <phrase id="fiber_specific_ptr_release"/> <link linkend="fiber_specific_ptr_release">Member function <code>release</code>()</link> </bridgehead> </para> <programlisting><phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">release</phrase><phrase role="special">();</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Return <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> and store <code><phrase role="keyword">nullptr</phrase></code> as the pointer associated with the current fiber without invoking the cleanup function. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()==</phrase><phrase role="keyword">nullptr</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="fiber_specific_ptr_reset_bridgehead"> <phrase id="fiber_specific_ptr_reset"/> <link linkend="fiber_specific_ptr_reset">Member function <code>reset</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">reset</phrase><phrase role="special">(</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">new_value</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> If <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()!=</phrase><phrase role="identifier">new_value</phrase></code> and <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> is not <code><phrase role="keyword">nullptr</phrase></code>, invoke <code><phrase role="keyword">delete</phrase> <phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">fn</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">())</phrase></code> as appropriate. Store <code><phrase role="identifier">new_value</phrase></code> as the pointer associated with the current fiber. </para> </listitem> </varlistentry> <varlistentry> <term>Postcondition:</term> <listitem> <para> <code><phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get</phrase><phrase role="special">()==</phrase><phrase role="identifier">new_value</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Exception raised during cleanup of previous value. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.migration"> <title><anchor id="migration"/><link linkend="fiber.migration">Migrating fibers between threads</link></title> <bridgehead renderas="sect3" id="fiber.migration.h0"> <phrase id="fiber.migration.overview"/><link linkend="fiber.migration.overview">Overview</link> </bridgehead> <para> Each fiber owns a stack and manages its execution state, including all registers and CPU flags, the instruction pointer and the stack pointer. That means, in general, a fiber is not bound to a specific thread.<footnote id="fiber.migration.f0"> <para> The <quote>main</quote> fiber on each thread, that is, the fiber on which the thread is launched, cannot migrate to any other thread. Also <emphasis role="bold">Boost.Fiber</emphasis> implicitly creates a dispatcher fiber for each thread &mdash; this cannot migrate either. </para> </footnote><superscript>,</superscript><footnote id="fiber.migration.f1"> <para> Of course it would be problematic to migrate a fiber that relies on <link linkend="thread_local_storage">thread-local storage</link>. </para> </footnote> </para> <para> Migrating a fiber from a logical CPU with heavy workload to another logical CPU with a lighter workload might speed up the overall execution. Note that in the case of NUMA-architectures, it is not always advisable to migrate data between threads. Suppose fiber <emphasis>f</emphasis> is running on logical CPU <emphasis>cpu0</emphasis> which belongs to NUMA node <emphasis>node0</emphasis>. The data of <emphasis>f</emphasis> are allocated on the physical memory located at <emphasis>node0</emphasis>. Migrating the fiber from <emphasis>cpu0</emphasis> to another logical CPU <emphasis>cpuX</emphasis> which is part of a different NUMA node <emphasis>nodeX</emphasis> might reduce the performance of the application due to increased latency of memory access. </para> <para> Only fibers that are contained in <link linkend="class_algorithm"><code>algorithm</code></link>&#8217;s ready queue can migrate between threads. You cannot migrate a running fiber, nor one that is <link linkend="blocking"><emphasis>blocked</emphasis></link>. You cannot migrate a fiber if its <link linkend="context_is_context"><code>context::is_context()</code></link> method returns <code><phrase role="keyword">true</phrase></code> for <code><phrase role="identifier">pinned_context</phrase></code>. </para> <para> In <emphasis role="bold">Boost.Fiber</emphasis> a fiber is migrated by invoking <link linkend="context_detach"><code>context::detach()</code></link> on the thread from which the fiber migrates and <link linkend="context_attach"><code>context::attach()</code></link> on the thread to which the fiber migrates. </para> <para> Thus, fiber migration is accomplished by sharing state between instances of a user-coded <link linkend="class_algorithm"><code>algorithm</code></link> implementation running on different threads. The fiber&#8217;s original thread calls <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link>, passing the fiber&#8217;s <link linkend="class_context"><code>context</code></link><literal>*</literal>. The <code><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code> implementation calls <link linkend="context_detach"><code>context::detach()</code></link>. </para> <para> At some later point, when the same or a different thread calls <link linkend="algorithm_pick_next"><code>algorithm::pick_next()</code></link>, the <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> implementation selects a ready fiber and calls <link linkend="context_attach"><code>context::attach()</code></link> on it before returning it. </para> <para> As stated above, a <code><phrase role="identifier">context</phrase></code> for which <code><phrase role="identifier">is_context</phrase><phrase role="special">(</phrase><phrase role="identifier">pinned_context</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">true</phrase></code> must never be passed to either <link linkend="context_detach"><code>context::detach()</code></link> or <link linkend="context_attach"><code>context::attach()</code></link>. It may only be returned from <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> called by the <emphasis>same</emphasis> thread that passed that context to <code><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code>. </para> <bridgehead renderas="sect3" id="fiber.migration.h1"> <phrase id="fiber.migration.example_of_work_sharing"/><link linkend="fiber.migration.example_of_work_sharing">Example of work sharing</link> </bridgehead> <para> In the example <ulink url="../../examples/work_sharing.cpp">work_sharing.cpp</ulink> multiple worker fibers are created on the main thread. Each fiber gets a character as parameter at construction. This character is printed out ten times. Between each iteration the fiber calls <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>. That puts the fiber in the ready queue of the fiber-scheduler <emphasis>shared_ready_queue</emphasis>, running in the current thread. The next fiber ready to be executed is dequeued from the shared ready queue and resumed by <emphasis>shared_ready_queue</emphasis> running on <emphasis>any participating thread</emphasis>. </para> <para> All instances of <emphasis>shared_ready_queue</emphasis> share one global concurrent queue, used as ready queue. This mechanism shares all worker fibers between all instances of <emphasis>shared_ready_queue</emphasis>, thus between all participating threads. </para> <bridgehead renderas="sect3" id="fiber.migration.h2"> <phrase id="fiber.migration.setup_of_threads_and_fibers"/><link linkend="fiber.migration.setup_of_threads_and_fibers">Setup of threads and fibers</link> </bridgehead> <para> In <code><phrase role="identifier">main</phrase><phrase role="special">()</phrase></code> the fiber-scheduler is installed and the worker fibers and the threads are launched. </para> <para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_work</phrase> <phrase role="special">&gt;();</phrase> <co id="fiber.migration.c0" linkends="fiber.migration.c1" /> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">char</phrase> <phrase role="identifier">c</phrase> <phrase role="special">:</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">(</phrase><phrase role="string">&quot;abcdefghijklmnopqrstuvwxyz&quot;</phrase><phrase role="special">))</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c2" linkends="fiber.migration.c3" /> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">([</phrase><phrase role="identifier">c</phrase><phrase role="special">](){</phrase> <phrase role="identifier">whatevah</phrase><phrase role="special">(</phrase> <phrase role="identifier">c</phrase><phrase role="special">);</phrase> <phrase role="special">}).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">fiber_count</phrase><phrase role="special">;</phrase> <co id="fiber.migration.c4" linkends="fiber.migration.c5" /> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">thread_barrier</phrase> <phrase role="identifier">b</phrase><phrase role="special">(</phrase> <phrase role="number">4</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase> <phrase role="identifier">threads</phrase><phrase role="special">[]</phrase> <phrase role="special">=</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c6" linkends="fiber.migration.c7" /> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">thread</phrase><phrase role="special">,</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">b</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">thread</phrase><phrase role="special">,</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">b</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">thread</phrase><phrase role="special">,</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">b</phrase><phrase role="special">)</phrase> <phrase role="special">};</phrase> <phrase role="identifier">b</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c8" linkends="fiber.migration.c9" /> <phrase role="special">{</phrase> <phrase role="identifier">lock_type</phrase><co id="fiber.migration.c10" linkends="fiber.migration.c11" /> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_count</phrase><phrase role="special">);</phrase> <phrase role="identifier">cnd_count</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber_count</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">);</phrase> <co id="fiber.migration.c12" linkends="fiber.migration.c13" /> <phrase role="special">}</phrase> <co id="fiber.migration.c14" linkends="fiber.migration.c15" /> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber_count</phrase><phrase role="special">);</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">t</phrase> <phrase role="special">:</phrase> <phrase role="identifier">threads</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c16" linkends="fiber.migration.c17" /> <phrase role="identifier">t</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <calloutlist> <callout arearefs="fiber.migration.c0" id="fiber.migration.c1"> <para> Install the scheduling algorithm <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_work</phrase></code> in the main thread too, so each new fiber gets launched into the shared pool. </para> </callout> <callout arearefs="fiber.migration.c2" id="fiber.migration.c3"> <para> Launch a number of worker fibers; each worker fiber picks up a character that is passed as parameter to fiber-function <code><phrase role="identifier">whatevah</phrase></code>. Each worker fiber gets detached. </para> </callout> <callout arearefs="fiber.migration.c4" id="fiber.migration.c5"> <para> Increment fiber counter for each new fiber. </para> </callout> <callout arearefs="fiber.migration.c6" id="fiber.migration.c7"> <para> Launch a couple of threads that join the work sharing. </para> </callout> <callout arearefs="fiber.migration.c8" id="fiber.migration.c9"> <para> sync with other threads: allow them to start processing </para> </callout> <callout arearefs="fiber.migration.c10" id="fiber.migration.c11"> <para> <code><phrase role="identifier">lock_type</phrase></code> is typedef'ed as <ulink url="http://en.cppreference.com/w/cpp/thread/unique_lock"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase></code></ulink>&lt; <ulink url="http://en.cppreference.com/w/cpp/thread/mutex"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase></code></ulink> &gt; </para> </callout> <callout arearefs="fiber.migration.c12" id="fiber.migration.c13"> <para> Suspend main fiber and resume worker fibers in the meanwhile. Main fiber gets resumed (e.g returns from <code><phrase role="identifier">condition_variable_any</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>) if all worker fibers are complete. </para> </callout> <callout arearefs="fiber.migration.c14" id="fiber.migration.c15"> <para> Releasing lock of mtx_count is required before joining the threads, otherwise the other threads would be blocked inside condition_variable::wait() and would never return (deadlock). </para> </callout> <callout arearefs="fiber.migration.c16" id="fiber.migration.c17"> <para> wait for threads to terminate </para> </callout> </calloutlist> <para> The start of the threads is synchronized with a barrier. The main fiber of each thread (including main thread) is suspended until all worker fibers are complete. When the main fiber returns from <link linkend="condition_variable_wait"><code>condition_variable::wait()</code></link>, the thread terminates: the main thread joins all other threads. </para> <para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">thread_barrier</phrase> <phrase role="special">*</phrase> <phrase role="identifier">b</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">;</phrase> <phrase role="identifier">buffer</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;thread started &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">flush</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_work</phrase> <phrase role="special">&gt;();</phrase> <co id="fiber.migration.c18" linkends="fiber.migration.c19" /> <phrase role="identifier">b</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c20" linkends="fiber.migration.c21" /> <phrase role="identifier">lock_type</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_count</phrase><phrase role="special">);</phrase> <phrase role="identifier">cnd_count</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber_count</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">);</phrase> <co id="fiber.migration.c22" linkends="fiber.migration.c23" /> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="identifier">fiber_count</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <calloutlist> <callout arearefs="fiber.migration.c18" id="fiber.migration.c19"> <para> Install the scheduling algorithm <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_work</phrase></code> in order to join the work sharing. </para> </callout> <callout arearefs="fiber.migration.c20" id="fiber.migration.c21"> <para> sync with other threads: allow them to start processing </para> </callout> <callout arearefs="fiber.migration.c22" id="fiber.migration.c23"> <para> Suspend main fiber and resume worker fibers in the meanwhile. Main fiber gets resumed (e.g returns from <code><phrase role="identifier">condition_variable_any</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>) if all worker fibers are complete. </para> </callout> </calloutlist> <para> Each worker fiber executes function <code><phrase role="identifier">whatevah</phrase><phrase role="special">()</phrase></code> with character <code><phrase role="identifier">me</phrase></code> as parameter. The fiber yields in a loop and prints out a message if it was migrated to another thread. </para> <para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">whatevah</phrase><phrase role="special">(</phrase> <phrase role="keyword">char</phrase> <phrase role="identifier">me</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">try</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">my_thread</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c24" linkends="fiber.migration.c25" /> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">;</phrase> <phrase role="identifier">buffer</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;fiber &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">me</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; started on thread &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">my_thread</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="char">'\n'</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">flush</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="number">10</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c26" linkends="fiber.migration.c27" /> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c28" linkends="fiber.migration.c29" /> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">new_thread</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">get_id</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c30" linkends="fiber.migration.c31" /> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">new_thread</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">my_thread</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c32" linkends="fiber.migration.c33" /> <phrase role="identifier">my_thread</phrase> <phrase role="special">=</phrase> <phrase role="identifier">new_thread</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">;</phrase> <phrase role="identifier">buffer</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;fiber &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">me</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; switched to thread &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">my_thread</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="char">'\n'</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">flush</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">catch</phrase> <phrase role="special">(</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="identifier">lock_type</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_count</phrase><phrase role="special">);</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="special">--</phrase><phrase role="identifier">fiber_count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <co id="fiber.migration.c34" linkends="fiber.migration.c35" /> <phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="identifier">cnd_count</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">();</phrase> <co id="fiber.migration.c36" linkends="fiber.migration.c37" /> <phrase role="special">}</phrase> <phrase role="special">}</phrase> </programlisting> </para> <calloutlist> <callout arearefs="fiber.migration.c24" id="fiber.migration.c25"> <para> get ID of initial thread </para> </callout> <callout arearefs="fiber.migration.c26" id="fiber.migration.c27"> <para> loop ten times </para> </callout> <callout arearefs="fiber.migration.c28" id="fiber.migration.c29"> <para> yield to other fibers </para> </callout> <callout arearefs="fiber.migration.c30" id="fiber.migration.c31"> <para> get ID of current thread </para> </callout> <callout arearefs="fiber.migration.c32" id="fiber.migration.c33"> <para> test if fiber was migrated to another thread </para> </callout> <callout arearefs="fiber.migration.c34" id="fiber.migration.c35"> <para> Decrement fiber counter for each completed fiber. </para> </callout> <callout arearefs="fiber.migration.c36" id="fiber.migration.c37"> <para> Notify all fibers waiting on <code><phrase role="identifier">cnd_count</phrase></code>. </para> </callout> </calloutlist> <bridgehead renderas="sect3" id="fiber.migration.h3"> <phrase id="fiber.migration.scheduling_fibers"/><link linkend="fiber.migration.scheduling_fibers">Scheduling fibers</link> </bridgehead> <para> The fiber scheduler <code><phrase role="identifier">shared_ready_queue</phrase></code> is like <code><phrase role="identifier">round_robin</phrase></code>, except that it shares a common ready queue among all participating threads. A thread participates in this pool by executing <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link> before any other <emphasis role="bold">Boost.Fiber</emphasis> operation. </para> <para> The important point about the ready queue is that it&#8217;s a class static, common to all instances of shared_ready_queue. Fibers that are enqueued via <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link> (fibers that are ready to be resumed) are thus available to all threads. It is required to reserve a separate, scheduler-specific queue for the thread&#8217;s main fiber and dispatcher fibers: these may <emphasis>not</emphasis> be shared between threads! When we&#8217;re passed either of these fibers, push it there instead of in the shared queue: it would be Bad News for thread B to retrieve and attempt to execute thread A&#8217;s main fiber. </para> <para> [awakened_ws] </para> <para> When <link linkend="algorithm_pick_next"><code>algorithm::pick_next()</code></link> gets called inside one thread, a fiber is dequeued from <emphasis>rqueue_</emphasis> and will be resumed in that thread. </para> <para> [pick_next_ws] </para> <para> The source code above is found in <ulink url="../../examples/work_sharing.cpp">work_sharing.cpp</ulink>. </para> </section> <section id="fiber.callbacks"> <title><anchor id="callbacks"/><link linkend="fiber.callbacks">Integrating Fibers with Asynchronous Callbacks</link></title> <section id="fiber.callbacks.overview"> <title><link linkend="fiber.callbacks.overview">Overview</link></title> <para> One of the primary benefits of <emphasis role="bold">Boost.Fiber</emphasis> is the ability to use asynchronous operations for efficiency, while at the same time structuring the calling code <emphasis>as if</emphasis> the operations were synchronous. Asynchronous operations provide completion notification in a variety of ways, but most involve a callback function of some kind. This section discusses tactics for interfacing <emphasis role="bold">Boost.Fiber</emphasis> with an arbitrary async operation. </para> <para> For purposes of illustration, consider the following hypothetical API: </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// constructor acquires some resource that can be read and written</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">();</phrase> <phrase role="comment">// callbacks accept an int error code; 0 == success</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">errorcode</phrase><phrase role="special">;</phrase> <phrase role="comment">// write callback only needs to indicate success or failure</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">init_write</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">callback</phrase><phrase role="special">);</phrase> <phrase role="comment">// read callback needs to accept both errorcode and data</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">init_read</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">callback</phrase><phrase role="special">);</phrase> <phrase role="comment">// ... other operations ...</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> The significant points about each of <code><phrase role="identifier">init_write</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">init_read</phrase><phrase role="special">()</phrase></code> are: </para> <itemizedlist> <listitem> <simpara> The <code><phrase role="identifier">AsyncAPI</phrase></code> method only initiates the operation. It returns immediately, while the requested operation is still pending. </simpara> </listitem> <listitem> <simpara> The method accepts a callback. When the operation completes, the callback is called with relevant parameters (error code, data if applicable). </simpara> </listitem> </itemizedlist> <para> We would like to wrap these asynchronous methods in functions that appear synchronous by blocking the calling fiber until the operation completes. This lets us use the wrapper function&#8217;s return value to deliver relevant data. </para> <tip> <para> <link linkend="class_promise"><code>promise&lt;&gt;</code></link> and <link linkend="class_future"><code>future&lt;&gt;</code></link> are your friends here. </para> </tip> </section> <section id="fiber.callbacks.return_errorcode"> <title><link linkend="fiber.callbacks.return_errorcode">Return Errorcode</link></title> <para> The <code><phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">init_write</phrase><phrase role="special">()</phrase></code> callback passes only an <code><phrase role="identifier">errorcode</phrase></code>. If we simply want the blocking wrapper to return that <code><phrase role="identifier">errorcode</phrase></code>, this is an extremely straightforward use of <link linkend="class_promise"><code>promise&lt;&gt;</code></link> and <link linkend="class_future"><code>future&lt;&gt;</code></link>: </para> <para> <programlisting><phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">write_ec</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// In general, even though we block waiting for future::get() and therefore</phrase> <phrase role="comment">// won't destroy 'promise' until promise::set_value() has been called, we</phrase> <phrase role="comment">// are advised that with threads it's possible for ~promise() to be</phrase> <phrase role="comment">// entered before promise::set_value() has returned. While that shouldn't</phrase> <phrase role="comment">// happen with fibers::promise, a robust way to deal with the lifespan</phrase> <phrase role="comment">// issue is to bind 'promise' into our lambda. Since promise is move-only,</phrase> <phrase role="comment">// use initialization capture.</phrase> <phrase role="preprocessor">#if</phrase> <phrase role="special">!</phrase> <phrase role="identifier">defined</phrase><phrase role="special">(</phrase><phrase role="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase><phrase role="special">)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_write</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="special">[</phrase><phrase role="identifier">promise</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">)](</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="preprocessor">#else</phrase> <phrase role="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_write</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([](</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">,</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">placeholders</phrase><phrase role="special">::</phrase><phrase role="identifier">_1</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="preprocessor">#endif</phrase> <phrase role="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> All we have to do is: </para> <orderedlist> <listitem> <simpara> Instantiate a <code><phrase role="identifier">promise</phrase><phrase role="special">&lt;&gt;</phrase></code> of correct type. </simpara> </listitem> <listitem> <simpara> Obtain its <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code>. </simpara> </listitem> <listitem> <simpara> Arrange for the callback to call <link linkend="promise_set_value"><code>promise::set_value()</code></link>. </simpara> </listitem> <listitem> <simpara> Block on <link linkend="future_get"><code>future::get()</code></link>. </simpara> </listitem> </orderedlist> <note> <para> This tactic for resuming a pending fiber works even if the callback is called on a different thread than the one on which the initiating fiber is running. In fact, <ulink url="../../examples/adapt_callbacks.cpp">the example program&#8217;s</ulink> dummy <code><phrase role="identifier">AsyncAPI</phrase></code> implementation illustrates that: it simulates async I/O by launching a new thread that sleeps briefly and then calls the relevant callback. </para> </note> </section> <section id="fiber.callbacks.success_or_exception"> <title><link linkend="fiber.callbacks.success_or_exception">Success or Exception</link></title> <para> A wrapper more aligned with modern C++ practice would use an exception, rather than an <code><phrase role="identifier">errorcode</phrase></code>, to communicate failure to its caller. This is straightforward to code in terms of <code><phrase role="identifier">write_ec</phrase><phrase role="special">()</phrase></code>: </para> <para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">write</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase> <phrase role="special">=</phrase> <phrase role="identifier">write_ec</phrase><phrase role="special">(</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">data</phrase><phrase role="special">);</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">make_exception</phrase><phrase role="special">(</phrase><phrase role="string">&quot;write&quot;</phrase><phrase role="special">,</phrase> <phrase role="identifier">ec</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> The point is that since each fiber has its own stack, you need not repeat messy boilerplate: normal encapsulation works. </para> </section> <section id="fiber.callbacks.return_errorcode_or_data"> <title><link linkend="fiber.callbacks.return_errorcode_or_data">Return Errorcode or Data</link></title> <para> Things get a bit more interesting when the async operation&#8217;s callback passes multiple data items of interest. One approach would be to use <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">pair</phrase><phrase role="special">&lt;&gt;</phrase></code> to capture both: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">pair</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">read_ec</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">pair</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">result_pair</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">result_pair</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">result_pair</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// We promise that both 'promise' and 'future' will survive until our</phrase> <phrase role="comment">// lambda has been called.</phrase> <phrase role="preprocessor">#if</phrase> <phrase role="special">!</phrase> <phrase role="identifier">defined</phrase><phrase role="special">(</phrase><phrase role="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase><phrase role="special">)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_read</phrase><phrase role="special">([</phrase><phrase role="identifier">promise</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">)](</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">result_pair</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="preprocessor">#else</phrase> <phrase role="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_read</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([](</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">result_pair</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">,</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">result_pair</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">placeholders</phrase><phrase role="special">::</phrase><phrase role="identifier">_1</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">placeholders</phrase><phrase role="special">::</phrase><phrase role="identifier">_2</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="preprocessor">#endif</phrase> <phrase role="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Once you bundle the interesting data in <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">pair</phrase><phrase role="special">&lt;&gt;</phrase></code>, the code is effectively identical to <code><phrase role="identifier">write_ec</phrase><phrase role="special">()</phrase></code>. You can call it like this: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tie</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="identifier">read_ec</phrase><phrase role="special">(</phrase> <phrase role="identifier">api</phrase><phrase role="special">);</phrase> </programlisting> </para> </section> <section id="fiber.callbacks.data_or_exception"> <title><anchor id="Data_or_Exception"/><link linkend="fiber.callbacks.data_or_exception">Data or Exception</link></title> <para> But a more natural API for a function that obtains data is to return only the data on success, throwing an exception on error. </para> <para> As with <code><phrase role="identifier">write</phrase><phrase role="special">()</phrase></code> above, it&#8217;s certainly possible to code a <code><phrase role="identifier">read</phrase><phrase role="special">()</phrase></code> wrapper in terms of <code><phrase role="identifier">read_ec</phrase><phrase role="special">()</phrase></code>. But since a given application is unlikely to need both, let&#8217;s code <code><phrase role="identifier">read</phrase><phrase role="special">()</phrase></code> from scratch, leveraging <link linkend="promise_set_exception"><code>promise::set_exception()</code></link>: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">read</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// Both 'promise' and 'future' will survive until our lambda has been</phrase> <phrase role="comment">// called.</phrase> <phrase role="preprocessor">#if</phrase> <phrase role="special">!</phrase> <phrase role="identifier">defined</phrase><phrase role="special">(</phrase><phrase role="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase><phrase role="special">)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_read</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">promise</phrase><phrase role="special">](</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_exception_ptr</phrase><phrase role="special">(</phrase> <phrase role="identifier">make_exception</phrase><phrase role="special">(</phrase><phrase role="string">&quot;read&quot;</phrase><phrase role="special">,</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">});</phrase> <phrase role="preprocessor">#else</phrase> <phrase role="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_read</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([](</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">promise</phrase><phrase role="special">,</phrase> <phrase role="identifier">AsyncAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise</phrase><phrase role="special">.</phrase><phrase role="identifier">set_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_exception_ptr</phrase><phrase role="special">(</phrase> <phrase role="identifier">make_exception</phrase><phrase role="special">(</phrase><phrase role="string">&quot;read&quot;</phrase><phrase role="special">,</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">},</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">promise</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">placeholders</phrase><phrase role="special">::</phrase><phrase role="identifier">_1</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">placeholders</phrase><phrase role="special">::</phrase><phrase role="identifier">_2</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="preprocessor">#endif</phrase> <phrase role="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> <link linkend="future_get"><code>future::get()</code></link> will do the right thing, either returning <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase></code> or throwing an exception. </para> </section> <section id="fiber.callbacks.success_error_virtual_methods"> <title><link linkend="fiber.callbacks.success_error_virtual_methods">Success/Error Virtual Methods</link></title> <para> One classic approach to completion notification is to define an abstract base class with <code><phrase role="identifier">success</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">error</phrase><phrase role="special">()</phrase></code> methods. Code wishing to perform async I/O must derive a subclass, override each of these methods and pass the async operation a pointer to a subclass instance. The abstract base class might look like this: </para> <para> <programlisting><phrase role="comment">// every async operation receives a subclass instance of this abstract base</phrase> <phrase role="comment">// class through which to communicate its result</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">Response</phrase> <phrase role="special">{</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Response</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">ptr</phrase><phrase role="special">;</phrase> <phrase role="comment">// called if the operation succeeds</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">success</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="comment">// called if the operation fails</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">error</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPIBase</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Now the <code><phrase role="identifier">AsyncAPI</phrase></code> operation might look more like this: </para> <para> <programlisting><phrase role="comment">// derive Response subclass, instantiate, pass Response::ptr</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">init_read</phrase><phrase role="special">(</phrase> <phrase role="identifier">Response</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr</phrase><phrase role="special">);</phrase> </programlisting> </para> <para> We can address this by writing a one-size-fits-all <code><phrase role="identifier">PromiseResponse</phrase></code>: </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">PromiseResponse</phrase><phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">Response</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// called if the operation succeeds</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">success</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise_</phrase><phrase role="special">.</phrase><phrase role="identifier">set_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// called if the operation fails</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">error</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPIBase</phrase><phrase role="special">::</phrase><phrase role="identifier">errorcode</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">promise_</phrase><phrase role="special">.</phrase><phrase role="identifier">set_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_exception_ptr</phrase><phrase role="special">(</phrase> <phrase role="identifier">make_exception</phrase><phrase role="special">(</phrase><phrase role="string">&quot;read&quot;</phrase><phrase role="special">,</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">promise_</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">promise</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">promise_</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Now we can simply obtain the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> from that <code><phrase role="identifier">PromiseResponse</phrase></code> and wait on its <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">read</phrase><phrase role="special">(</phrase> <phrase role="identifier">AsyncAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Because init_read() requires a shared_ptr, we must allocate our</phrase> <phrase role="comment">// ResponsePromise on the heap, even though we know its lifespan.</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">promisep</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">PromiseResponse</phrase> <phrase role="special">&gt;()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">promisep</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// Both 'promisep' and 'future' will survive until our lambda has been</phrase> <phrase role="comment">// called.</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">init_read</phrase><phrase role="special">(</phrase> <phrase role="identifier">promisep</phrase><phrase role="special">);</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> The source code above is found in <ulink url="../../examples/adapt_callbacks.cpp">adapt_callbacks.cpp</ulink> and <ulink url="../../examples/adapt_method_calls.cpp">adapt_method_calls.cpp</ulink>. </para> </section> <section id="fiber.callbacks.then_there_s____boost_asio__"> <title><anchor id="callbacks_asio"/><link linkend="fiber.callbacks.then_there_s____boost_asio__">Then There&#8217;s <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink></link></title> <para> Since the simplest form of Boost.Asio asynchronous operation completion token is a callback function, we could apply the same tactics for Asio as for our hypothetical <code><phrase role="identifier">AsyncAPI</phrase></code> asynchronous operations. </para> <para> Fortunately we need not. Boost.Asio incorporates a mechanism<footnote id="fiber.callbacks.then_there_s____boost_asio__.f0"> <para> This mechanism has been proposed as a conventional way to allow the caller of an arbitrary async function to specify completion handling: <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4045.pdf">N4045</ulink>. </para> </footnote> by which the caller can customize the notification behavior of any async operation. Therefore we can construct a <emphasis>completion token</emphasis> which, when passed to a <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink> async operation, requests blocking for the calling fiber. </para> <para> A typical Asio async function might look something like this:<footnote id="fiber.callbacks.then_there_s____boost_asio__.f1"> <para> per <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4045.pdf">N4045</ulink> </para> </footnote> </para> <programlisting><phrase role="keyword">template</phrase> <phrase role="special">&lt;</phrase> <phrase role="special">...,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">CompletionToken</phrase> <phrase role="special">&gt;</phrase> <emphasis>deduced_return_type</emphasis> <phrase role="identifier">async_something</phrase><phrase role="special">(</phrase> <phrase role="special">...</phrase> <phrase role="special">,</phrase> <phrase role="identifier">CompletionToken</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">token</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// construct handler_type instance from CompletionToken</phrase> <phrase role="identifier">handler_type</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">CompletionToken</phrase><phrase role="special">,</phrase> <phrase role="special">...&gt;::</phrase><phrase role="identifier">type</phrase> <emphasis role="bold"><!--quickbook-escape-prefix--><code><!--quickbook-escape-postfix-->handler(token)<!--quickbook-escape-prefix--></code><!--quickbook-escape-postfix--></emphasis><phrase role="special">;</phrase> <phrase role="comment">// construct async_result instance from handler_type</phrase> <phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">decltype</phrase><phrase role="special">(</phrase><phrase role="identifier">handler</phrase><phrase role="special">)&gt;</phrase> <emphasis role="bold"><!--quickbook-escape-prefix--><code><!--quickbook-escape-postfix-->result(handler)<!--quickbook-escape-prefix--></code><!--quickbook-escape-postfix--></emphasis><phrase role="special">;</phrase> <phrase role="comment">// ... arrange to call handler on completion ...</phrase> <phrase role="comment">// ... initiate actual I/O operation ...</phrase> <phrase role="keyword">return</phrase> <emphasis role="bold"><!--quickbook-escape-prefix--><code><!--quickbook-escape-postfix-->result.get()<!--quickbook-escape-prefix--></code><!--quickbook-escape-postfix--></emphasis><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> <para> We will engage that mechanism, which is based on specializing Asio&#8217;s <code><phrase role="identifier">handler_type</phrase><phrase role="special">&lt;&gt;</phrase></code> template for the <code><phrase role="identifier">CompletionToken</phrase></code> type and the signature of the specific callback. The remainder of this discussion will refer back to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> as the Asio async function under consideration. </para> <para> The implementation described below uses lower-level facilities than <code><phrase role="identifier">promise</phrase></code> and <code><phrase role="identifier">future</phrase></code> because the <code><phrase role="identifier">promise</phrase></code> mechanism interacts badly with <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stop.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">stop</phrase><phrase role="special">()</phrase></code></ulink>. It produces <code><phrase role="identifier">broken_promise</phrase></code> exceptions. </para> <para> <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase></code> is a completion token of this kind. <code><phrase role="identifier">yield</phrase></code> is an instance of <code><phrase role="identifier">yield_t</phrase></code>: </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">yield_t</phrase><phrase role="special">()</phrase> <phrase role="special">=</phrase> <phrase role="keyword">default</phrase><phrase role="special">;</phrase> <phrase role="comment">/** * @code * static yield_t yield; * boost::system::error_code myec; * func(yield[myec]); * @endcode * @c yield[myec] returns an instance of @c yield_t whose @c ec_ points * to @c myec. The expression @c yield[myec] &quot;binds&quot; @c myec to that * (anonymous) @c yield_t instance, instructing @c func() to store any * @c error_code it might produce into @c myec rather than throwing @c * boost::system::system_error. */</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="identifier">tmp</phrase><phrase role="special">;</phrase> <phrase role="identifier">tmp</phrase><phrase role="special">.</phrase><phrase role="identifier">ec_</phrase> <phrase role="special">=</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">ec</phrase><phrase role="special">;</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">tmp</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">//private:</phrase> <phrase role="comment">// ptr to bound error_code instance if any</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">{</phrase> <phrase role="keyword">nullptr</phrase> <phrase role="special">};</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> <code><phrase role="identifier">yield_t</phrase></code> is in fact only a placeholder, a way to trigger Boost.Asio customization. It can bind a <ulink url="http://www.boost.org/doc/libs/release/libs/system/doc/reference.html#Class-error_code"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase></code></ulink> for use by the actual handler. </para> <para> <code><phrase role="identifier">yield</phrase></code> is declared as: </para> <para> <programlisting><phrase role="comment">// canonical instance</phrase> <phrase role="keyword">thread_local</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="identifier">yield</phrase><phrase role="special">{};</phrase> </programlisting> </para> <para> Asio customization is engaged by specializing <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/handler_type.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">handler_type</phrase><phrase role="special">&lt;&gt;</phrase></code></ulink> for <code><phrase role="identifier">yield_t</phrase></code>: </para> <para> <programlisting><phrase role="comment">// Handler type specialisation for fibers::asio::yield.</phrase> <phrase role="comment">// When 'yield' is passed as a completion handler which accepts only</phrase> <phrase role="comment">// error_code, use yield_handler&lt;void&gt;. yield_handler will take care of the</phrase> <phrase role="comment">// error_code one way or another.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">ReturnType</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">handler_type</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">ReturnType</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase><phrase role="special">)</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">{</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">void</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">type</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> (There are actually four different specializations in <ulink url="../../examples/asio/detail/yield.hpp">detail/yield.hpp</ulink>, one for each of the four Asio async callback signatures we expect.) </para> <para> The above directs Asio to use <code><phrase role="identifier">yield_handler</phrase></code> as the actual handler for an async operation to which <code><phrase role="identifier">yield</phrase></code> is passed. There&#8217;s a generic <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> implementation and a <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> specialization. Let&#8217;s start with the <code><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> specialization: </para> <para> <programlisting><phrase role="comment">// yield_handler&lt;void&gt; is like yield_handler&lt;T&gt; without value_. In fact it's</phrase> <phrase role="comment">// just like yield_handler_base.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">void</phrase> <phrase role="special">&gt;:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">yield_handler_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">yield_handler</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">yield_handler_base</phrase><phrase role="special">{</phrase> <phrase role="identifier">y</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="comment">// nullary completion callback</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()()</phrase> <phrase role="special">{</phrase> <phrase role="special">(</phrase> <phrase role="special">*</phrase> <phrase role="keyword">this</phrase><phrase role="special">)(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// inherit operator()(error_code) overload from base class</phrase> <phrase role="keyword">using</phrase> <phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">();</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>, having consulted the <code><phrase role="identifier">handler_type</phrase><phrase role="special">&lt;&gt;</phrase></code> traits specialization, instantiates a <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> to be passed as the actual callback for the async operation. <code><phrase role="identifier">yield_handler</phrase></code>&#8217;s constructor accepts the <code><phrase role="identifier">yield_t</phrase></code> instance (the <code><phrase role="identifier">yield</phrase></code> object passed to the async function) and passes it along to <code><phrase role="identifier">yield_handler_base</phrase></code>: </para> <para> <programlisting><phrase role="comment">// This class encapsulates common elements between yield_handler&lt;T&gt; (capturing</phrase> <phrase role="comment">// a value to return from asio async function) and yield_handler&lt;void&gt; (no</phrase> <phrase role="comment">// such value). See yield_handler&lt;T&gt; and its &lt;void&gt; specialization below. Both</phrase> <phrase role="comment">// yield_handler&lt;T&gt; and yield_handler&lt;void&gt; are passed by value through</phrase> <phrase role="comment">// various layers of asio functions. In other words, they're potentially</phrase> <phrase role="comment">// copied multiple times. So key data such as the yield_completion instance</phrase> <phrase role="comment">// must be stored in our async_result&lt;yield_handler&lt;&gt;&gt; specialization, which</phrase> <phrase role="comment">// should be instantiated only once.</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">yield_handler_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">yield_handler_base</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="comment">// capture the context* associated with the running fiber</phrase> <phrase role="identifier">ctx_</phrase><phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">active</phrase><phrase role="special">()</phrase> <phrase role="special">},</phrase> <phrase role="comment">// capture the passed yield_t</phrase> <phrase role="identifier">yt_</phrase><phrase role="special">(</phrase> <phrase role="identifier">y</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="comment">// completion callback passing only (error_code)</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">BOOST_ASSERT_MSG</phrase><phrase role="special">(</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">,</phrase> <phrase role="string">&quot;Must inject yield_completion* &quot;</phrase> <phrase role="string">&quot;before calling yield_handler_base::operator()()&quot;</phrase><phrase role="special">);</phrase> <phrase role="identifier">BOOST_ASSERT_MSG</phrase><phrase role="special">(</phrase> <phrase role="identifier">yt_</phrase><phrase role="special">.</phrase><phrase role="identifier">ec_</phrase><phrase role="special">,</phrase> <phrase role="string">&quot;Must inject boost::system::error_code* &quot;</phrase> <phrase role="string">&quot;before calling yield_handler_base::operator()()&quot;</phrase><phrase role="special">);</phrase> <phrase role="comment">// If originating fiber is busy testing state_ flag, wait until it</phrase> <phrase role="comment">// has observed (completed != state_).</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">lock_t</phrase> <phrase role="identifier">lk</phrase><phrase role="special">{</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">mtx_</phrase> <phrase role="special">};</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">state_t</phrase> <phrase role="identifier">state</phrase> <phrase role="special">=</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">state_</phrase><phrase role="special">;</phrase> <phrase role="comment">// Notify a subsequent yield_completion::wait() call that it need not</phrase> <phrase role="comment">// suspend.</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">state_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">complete</phrase><phrase role="special">;</phrase> <phrase role="comment">// set the error_code bound by yield_t</phrase> <phrase role="special">*</phrase> <phrase role="identifier">yt_</phrase><phrase role="special">.</phrase><phrase role="identifier">ec_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">ec</phrase><phrase role="special">;</phrase> <phrase role="comment">// unlock the lock that protects state_</phrase> <phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="comment">// If ctx_ is still active, e.g. because the async operation</phrase> <phrase role="comment">// immediately called its callback (this method!) before the asio</phrase> <phrase role="comment">// async function called async_result_base::get(), we must not set it</phrase> <phrase role="comment">// ready.</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">waiting</phrase> <phrase role="special">==</phrase> <phrase role="identifier">state</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// wake the fiber</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">active</phrase><phrase role="special">()-&gt;</phrase><phrase role="identifier">schedule</phrase><phrase role="special">(</phrase> <phrase role="identifier">ctx_</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">//private:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx_</phrase><phrase role="special">;</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="identifier">yt_</phrase><phrase role="special">;</phrase> <phrase role="comment">// We depend on this pointer to yield_completion, which will be injected</phrase> <phrase role="comment">// by async_result.</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr_t</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">{};</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> <code><phrase role="identifier">yield_handler_base</phrase></code> stores a copy of the <code><phrase role="identifier">yield_t</phrase></code> instance &mdash; which, as shown above, contains only an <code><phrase role="identifier">error_code</phrase><phrase role="special">*</phrase></code>. It also captures the <link linkend="class_context"><code>context</code></link>* for the currently-running fiber by calling <link linkend="context_active"><code>context::active()</code></link>. </para> <para> You will notice that <code><phrase role="identifier">yield_handler_base</phrase></code> has one more data member (<code><phrase role="identifier">ycomp_</phrase></code>) that is initialized to <code><phrase role="keyword">nullptr</phrase></code> by its constructor &mdash; though its <code><phrase role="keyword">operator</phrase><phrase role="special">()()</phrase></code> method relies on <code><phrase role="identifier">ycomp_</phrase></code> being non-null. More on this in a moment. </para> <para> Having constructed the <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> instance, <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> goes on to construct an <code><phrase role="identifier">async_result</phrase></code> specialized for the <code><phrase role="identifier">handler_type</phrase><phrase role="special">&lt;&gt;::</phrase><phrase role="identifier">type</phrase></code>: in this case, <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;&gt;</phrase></code>. It passes the <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> instance to the new <code><phrase role="identifier">async_result</phrase></code> instance. </para> <para> <programlisting><phrase role="comment">// Without the need to handle a passed value, our yield_handler&lt;void&gt;</phrase> <phrase role="comment">// specialization is just like async_result_base.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">void</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">async_result_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">type</phrase><phrase role="special">;</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">async_result</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">void</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">h</phrase><phrase role="special">):</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">async_result_base</phrase><phrase role="special">{</phrase> <phrase role="identifier">h</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Naturally that leads us straight to <code><phrase role="identifier">async_result_base</phrase></code>: </para> <para> <programlisting><phrase role="comment">// Factor out commonality between async_result&lt;yield_handler&lt;T&gt;&gt; and</phrase> <phrase role="comment">// async_result&lt;yield_handler&lt;void&gt;&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">async_result_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">async_result_base</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_handler_base</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">h</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">{</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">{}</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Inject ptr to our yield_completion instance into this</phrase> <phrase role="comment">// yield_handler&lt;&gt;.</phrase> <phrase role="identifier">h</phrase><phrase role="special">.</phrase><phrase role="identifier">ycomp_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">this</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">ycomp_</phrase><phrase role="special">;</phrase> <phrase role="comment">// if yield_t didn't bind an error_code, make yield_handler_base's</phrase> <phrase role="comment">// error_code* point to an error_code local to this object so</phrase> <phrase role="comment">// yield_handler_base::operator() can unconditionally store through</phrase> <phrase role="comment">// its error_code*</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">h</phrase><phrase role="special">.</phrase><phrase role="identifier">yt_</phrase><phrase role="special">.</phrase><phrase role="identifier">ec_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">h</phrase><phrase role="special">.</phrase><phrase role="identifier">yt_</phrase><phrase role="special">.</phrase><phrase role="identifier">ec_</phrase> <phrase role="special">=</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Unless yield_handler_base::operator() has already been called,</phrase> <phrase role="comment">// suspend the calling fiber until that call.</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="comment">// The only way our own ec_ member could have a non-default value is</phrase> <phrase role="comment">// if our yield_handler did not have a bound error_code AND the</phrase> <phrase role="comment">// completion callback passed a non-default error_code.</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">throw_exception</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">system_error</phrase><phrase role="special">{</phrase> <phrase role="identifier">ec_</phrase> <phrase role="special">}</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="comment">// If yield_t does not bind an error_code instance, store into here.</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">{};</phrase> <phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr_t</phrase> <phrase role="identifier">ycomp_</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> This is how <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="identifier">ycomp_</phrase></code> becomes non-null: <code><phrase role="identifier">async_result_base</phrase></code>&#8217;s constructor injects a pointer back to its own <code><phrase role="identifier">yield_completion</phrase></code> member. </para> <para> Recall that the canonical <code><phrase role="identifier">yield_t</phrase></code> instance <code><phrase role="identifier">yield</phrase></code> initializes its <code><phrase role="identifier">error_code</phrase><phrase role="special">*</phrase></code> member <code><phrase role="identifier">ec_</phrase></code> to <code><phrase role="keyword">nullptr</phrase></code>. If this instance is passed to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> (<code><phrase role="identifier">ec_</phrase></code> is still <code><phrase role="keyword">nullptr</phrase></code>), the copy stored in <code><phrase role="identifier">yield_handler_base</phrase></code> will likewise have null <code><phrase role="identifier">ec_</phrase></code>. <code><phrase role="identifier">async_result_base</phrase></code>&#8217;s constructor sets <code><phrase role="identifier">yield_handler_base</phrase></code>&#8217;s <code><phrase role="identifier">yield_t</phrase></code>&#8217;s <code><phrase role="identifier">ec_</phrase></code> member to point to its own <code><phrase role="identifier">error_code</phrase></code> member. </para> <para> The stage is now set. <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> initiates the actual async operation, arranging to call its <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> instance on completion. Let&#8217;s say, for the sake of argument, that the actual async operation&#8217;s callback has signature <code><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">error_code</phrase><phrase role="special">)</phrase></code>. </para> <para> But since it&#8217;s an async operation, control returns at once to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>. <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> calls <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;&gt;::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>, and will return its return value. </para> <para> <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;&gt;::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> inherits <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>. </para> <para> <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> immediately calls <code><phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>. </para> <para> <programlisting><phrase role="comment">// Bundle a completion bool flag with a spinlock to protect it.</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">yield_completion</phrase> <phrase role="special">{</phrase> <phrase role="keyword">enum</phrase> <phrase role="identifier">state_t</phrase> <phrase role="special">{</phrase> <phrase role="identifier">init</phrase><phrase role="special">,</phrase> <phrase role="identifier">waiting</phrase><phrase role="special">,</phrase> <phrase role="identifier">complete</phrase> <phrase role="special">};</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">spinlock</phrase> <phrase role="identifier">mutex_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">mutex_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lock_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">intrusive_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">yield_completion</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">ptr_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">atomic</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">use_count_</phrase><phrase role="special">{</phrase> <phrase role="number">0</phrase> <phrase role="special">};</phrase> <phrase role="identifier">mutex_t</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">{};</phrase> <phrase role="identifier">state_t</phrase> <phrase role="identifier">state_</phrase><phrase role="special">{</phrase> <phrase role="identifier">init</phrase> <phrase role="special">};</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// yield_handler_base::operator()() will set state_ `complete` and</phrase> <phrase role="comment">// attempt to wake a suspended fiber. It would be Bad if that call</phrase> <phrase role="comment">// happened between our detecting (complete != state_) and suspending.</phrase> <phrase role="identifier">lock_t</phrase> <phrase role="identifier">lk</phrase><phrase role="special">{</phrase> <phrase role="identifier">mtx_</phrase> <phrase role="special">};</phrase> <phrase role="comment">// If state_ is already set, we're done here: don't suspend.</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">complete</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">state_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">state_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">waiting</phrase><phrase role="special">;</phrase> <phrase role="comment">// suspend(unique_lock&lt;spinlock&gt;) unlocks the lock in the act of</phrase> <phrase role="comment">// resuming another fiber</phrase> <phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">::</phrase><phrase role="identifier">active</phrase><phrase role="special">()-&gt;</phrase><phrase role="identifier">suspend</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">friend</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">intrusive_ptr_add_ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_completion</phrase> <phrase role="special">*</phrase> <phrase role="identifier">yc</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="keyword">nullptr</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">yc</phrase><phrase role="special">);</phrase> <phrase role="identifier">yc</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">use_count_</phrase><phrase role="special">.</phrase><phrase role="identifier">fetch_add</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">memory_order_relaxed</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">friend</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">intrusive_ptr_release</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_completion</phrase> <phrase role="special">*</phrase> <phrase role="identifier">yc</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="keyword">nullptr</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">yc</phrase><phrase role="special">);</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">==</phrase> <phrase role="identifier">yc</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">use_count_</phrase><phrase role="special">.</phrase><phrase role="identifier">fetch_sub</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">memory_order_release</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">atomic_thread_fence</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">memory_order_acquire</phrase><phrase role="special">);</phrase> <phrase role="keyword">delete</phrase> <phrase role="identifier">yc</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Supposing that the pending async operation has not yet completed, <code><phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">completed_</phrase></code> will still be <code><phrase role="keyword">false</phrase></code>, and <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> will call <link linkend="context_suspend"><code>context::suspend()</code></link> on the currently-running fiber. </para> <para> Other fibers will now have a chance to run. </para> <para> Some time later, the async operation completes. It calls <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase></code> with an <code><phrase role="identifier">error_code</phrase></code> indicating either success or failure. We&#8217;ll consider both cases. </para> <para> <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;</phrase></code> explicitly inherits <code><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase></code> from <code><phrase role="identifier">yield_handler_base</phrase></code>. </para> <para> <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase></code> first sets <code><phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">completed_</phrase></code> <code><phrase role="keyword">true</phrase></code>. This way, if <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>&#8217;s async operation completes immediately &mdash; if <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code> is called even before <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> &mdash; the calling fiber will <emphasis>not</emphasis> suspend. </para> <para> The actual <code><phrase role="identifier">error_code</phrase></code> produced by the async operation is then stored through the stored <code><phrase role="identifier">yield_t</phrase><phrase role="special">::</phrase><phrase role="identifier">ec_</phrase></code> pointer. If <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>&#8217;s caller used (e.g.) <code><phrase role="identifier">yield</phrase><phrase role="special">[</phrase><phrase role="identifier">my_ec</phrase><phrase role="special">]</phrase></code> to bind a local <code><phrase role="identifier">error_code</phrase></code> instance, the actual <code><phrase role="identifier">error_code</phrase></code> value is stored into the caller&#8217;s variable. Otherwise, it is stored into <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">ec_</phrase></code>. </para> <para> If the stored fiber context <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="identifier">ctx_</phrase></code> is not already running, it is marked as ready to run by passing it to <link linkend="context_schedule"><code>context::schedule()</code></link>. Control then returns from <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code>: the callback is done. </para> <para> In due course, that fiber is resumed. Control returns from <link linkend="context_suspend"><code>context::suspend()</code></link> to <code><phrase role="identifier">yield_completion</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code>, which returns to <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>. </para> <itemizedlist> <listitem> <simpara> If the original caller passed <code><phrase role="identifier">yield</phrase><phrase role="special">[</phrase><phrase role="identifier">my_ec</phrase><phrase role="special">]</phrase></code> to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> to bind a local <code><phrase role="identifier">error_code</phrase></code> instance, then <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code> stored its <code><phrase role="identifier">error_code</phrase></code> to the caller&#8217;s <code><phrase role="identifier">my_ec</phrase></code> instance, leaving <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">ec_</phrase></code> initialized to success. </simpara> </listitem> <listitem> <simpara> If the original caller passed <code><phrase role="identifier">yield</phrase></code> to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> without binding a local <code><phrase role="identifier">error_code</phrase></code> variable, then <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code> stored its <code><phrase role="identifier">error_code</phrase></code> into <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">ec_</phrase></code>. If in fact that <code><phrase role="identifier">error_code</phrase></code> is success, then all is well. </simpara> </listitem> <listitem> <simpara> Otherwise &mdash; the original caller did not bind a local <code><phrase role="identifier">error_code</phrase></code> and <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code> was called with an <code><phrase role="identifier">error_code</phrase></code> indicating error &mdash; <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> throws <code><phrase role="identifier">system_error</phrase></code> with that <code><phrase role="identifier">error_code</phrase></code>. </simpara> </listitem> </itemizedlist> <para> The case in which <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>&#8217;s completion callback has signature <code><phrase role="keyword">void</phrase><phrase role="special">()</phrase></code> is similar. <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">&gt;::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()()</phrase></code> invokes the machinery above with a <quote>success</quote> <code><phrase role="identifier">error_code</phrase></code>. </para> <para> A completion callback with signature <code><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">error_code</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">)</phrase></code> (that is: in addition to <code><phrase role="identifier">error_code</phrase></code>, callback receives some data item) is handled somewhat differently. For this kind of signature, <code><phrase role="identifier">handler_type</phrase><phrase role="special">&lt;&gt;::</phrase><phrase role="identifier">type</phrase></code> specifies <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> (for <code><phrase role="identifier">T</phrase></code> other than <code><phrase role="keyword">void</phrase></code>). </para> <para> A <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> reserves a <code><phrase role="identifier">value_</phrase></code> pointer to a value of type <code><phrase role="identifier">T</phrase></code>: </para> <para> <programlisting><phrase role="comment">// asio uses handler_type&lt;completion token type, signature&gt;::type to decide</phrase> <phrase role="comment">// what to instantiate as the actual handler. Below, we specialize</phrase> <phrase role="comment">// handler_type&lt; yield_t, ... &gt; to indicate yield_handler&lt;&gt;. So when you pass</phrase> <phrase role="comment">// an instance of yield_t as an asio completion token, asio selects</phrase> <phrase role="comment">// yield_handler&lt;&gt; as the actual handler class.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">yield_handler</phrase><phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">yield_handler_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// asio passes the completion token to the handler constructor</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">yield_handler</phrase><phrase role="special">(</phrase> <phrase role="identifier">yield_t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">yield_handler_base</phrase><phrase role="special">{</phrase> <phrase role="identifier">y</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="comment">// completion callback passing only value (T)</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">t</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// just like callback passing success error_code</phrase> <phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase><phrase role="special">(),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">t</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// completion callback passing (error_code, T)</phrase> <phrase role="keyword">void</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">ec</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">t</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">BOOST_ASSERT_MSG</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_</phrase><phrase role="special">,</phrase> <phrase role="string">&quot;Must inject value ptr &quot;</phrase> <phrase role="string">&quot;before caling yield_handler&lt;T&gt;::operator()()&quot;</phrase><phrase role="special">);</phrase> <phrase role="comment">// move the value to async_result&lt;&gt; instance BEFORE waking up a</phrase> <phrase role="comment">// suspended fiber</phrase> <phrase role="special">*</phrase> <phrase role="identifier">value_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">t</phrase><phrase role="special">);</phrase> <phrase role="comment">// forward the call to base-class completion handler</phrase> <phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">//private:</phrase> <phrase role="comment">// pointer to destination for eventual value</phrase> <phrase role="comment">// this must be injected by async_result before operator()() is called</phrase> <phrase role="identifier">T</phrase> <phrase role="special">*</phrase> <phrase role="identifier">value_</phrase><phrase role="special">{</phrase> <phrase role="keyword">nullptr</phrase> <phrase role="special">};</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> This pointer is initialized to <code><phrase role="keyword">nullptr</phrase></code>. </para> <para> When <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> instantiates <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;</phrase></code>: </para> <para> <programlisting><phrase role="comment">// asio constructs an async_result&lt;&gt; instance from the yield_handler specified</phrase> <phrase role="comment">// by handler_type&lt;&gt;::type. A particular asio async method constructs the</phrase> <phrase role="comment">// yield_handler, constructs this async_result specialization from it, then</phrase> <phrase role="comment">// returns the result of calling its get() method.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">async_result_base</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// type returned by get()</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">type</phrase><phrase role="special">;</phrase> <phrase role="keyword">explicit</phrase> <phrase role="identifier">async_result</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">h</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">async_result_base</phrase><phrase role="special">{</phrase> <phrase role="identifier">h</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Inject ptr to our value_ member into yield_handler&lt;&gt;: result will</phrase> <phrase role="comment">// be stored here.</phrase> <phrase role="identifier">h</phrase><phrase role="special">.</phrase><phrase role="identifier">value_</phrase> <phrase role="special">=</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">value_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">// asio async method returns result of calling get()</phrase> <phrase role="identifier">type</phrase> <phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase><phrase role="special">::</phrase><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">value_</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="identifier">type</phrase> <phrase role="identifier">value_</phrase><phrase role="special">{};</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> this <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;&gt;</phrase></code> specialization reserves a member of type <code><phrase role="identifier">T</phrase></code> to receive the passed data item, and sets <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;::</phrase><phrase role="identifier">value_</phrase></code> to point to its own data member. </para> <para> <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;</phrase></code> overrides <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>. The override calls <code><phrase role="identifier">async_result_base</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code>, so the calling fiber suspends as described above. </para> <para> <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">error_code</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">)</phrase></code> stores its passed <code><phrase role="identifier">T</phrase></code> value into <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;::</phrase><phrase role="identifier">value_</phrase></code>. </para> <para> Then it passes control to <code><phrase role="identifier">yield_handler_base</phrase><phrase role="special">::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">error_code</phrase><phrase role="special">)</phrase></code> to deal with waking the original fiber as described above. </para> <para> When <code><phrase role="identifier">async_result</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;::</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> resumes, it returns the stored <code><phrase role="identifier">value_</phrase></code> to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code> and ultimately to <code><phrase role="identifier">async_something</phrase><phrase role="special">()</phrase></code>&#8217;s caller. </para> <para> The case of a callback signature <code><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase><phrase role="special">)</phrase></code> is handled by having <code><phrase role="identifier">yield_handler</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;::</phrase><phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">T</phrase><phrase role="special">)</phrase></code> engage the <code><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">error_code</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">)</phrase></code> machinery, passing a <quote>success</quote> <code><phrase role="identifier">error_code</phrase></code>. </para> <para> The source code above is found in <ulink url="../../examples/asio/yield.hpp">yield.hpp</ulink> and <ulink url="../../examples/asio/detail/yield.hpp">detail/yield.hpp</ulink>. </para> </section> </section> <section id="fiber.nonblocking"> <title><anchor id="nonblocking"/><link linkend="fiber.nonblocking">Integrating Fibers with Nonblocking I/O</link></title> <bridgehead renderas="sect3" id="fiber.nonblocking.h0"> <phrase id="fiber.nonblocking.overview"/><link linkend="fiber.nonblocking.overview">Overview</link> </bridgehead> <para> <emphasis>Nonblocking</emphasis> I/O is distinct from <emphasis>asynchronous</emphasis> I/O. A true async I/O operation promises to initiate the operation and notify the caller on completion, usually via some sort of callback (as described in <link linkend="callbacks">Integrating Fibers with Asynchronous Callbacks</link>). </para> <para> In contrast, a nonblocking I/O operation refuses to start at all if it would be necessary to block, returning an error code such as <ulink url="http://man7.org/linux/man-pages/man3/errno.3.html"><code><phrase role="identifier">EWOULDBLOCK</phrase></code></ulink>. The operation is performed only when it can complete immediately. In effect, the caller must repeatedly retry the operation until it stops returning <code><phrase role="identifier">EWOULDBLOCK</phrase></code>. </para> <para> In a classic event-driven program, it can be something of a headache to use nonblocking I/O. At the point where the nonblocking I/O is attempted, a return value of <code><phrase role="identifier">EWOULDBLOCK</phrase></code> requires the caller to pass control back to the main event loop, arranging to retry again on the next iteration. </para> <para> Worse, a nonblocking I/O operation might <emphasis>partially</emphasis> succeed. That means that the relevant business logic must continue receiving control on every main loop iteration until all required data have been processed: a doubly-nested loop, implemented as a callback-driven state machine. </para> <para> <emphasis role="bold">Boost.Fiber</emphasis> can simplify this problem immensely. Once you have integrated with the application's main loop as described in <link linkend="integration">Sharing a Thread with Another Main Loop</link>, waiting for the next main-loop iteration is as simple as calling <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>. </para> <bridgehead renderas="sect3" id="fiber.nonblocking.h1"> <phrase id="fiber.nonblocking.example_nonblocking_api"/><link linkend="fiber.nonblocking.example_nonblocking_api">Example Nonblocking API</link> </bridgehead> <para> For purposes of illustration, consider this API: </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">NonblockingAPI</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">NonblockingAPI</phrase><phrase role="special">();</phrase> <phrase role="comment">// nonblocking operation: may return EWOULDBLOCK</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">read</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">desired</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> <phrase role="special">};</phrase> </programlisting> </para> <bridgehead renderas="sect3" id="fiber.nonblocking.h2"> <phrase id="fiber.nonblocking.polling_for_completion"/><link linkend="fiber.nonblocking.polling_for_completion">Polling for Completion</link> </bridgehead> <para> We can build a low-level wrapper around <code><phrase role="identifier">NonblockingAPI</phrase><phrase role="special">::</phrase><phrase role="identifier">read</phrase><phrase role="special">()</phrase></code> that shields its caller from ever having to deal with <code><phrase role="identifier">EWOULDBLOCK</phrase></code>: </para> <para> <programlisting><phrase role="comment">// guaranteed not to return EWOULDBLOCK</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">read_chunk</phrase><phrase role="special">(</phrase> <phrase role="identifier">NonblockingAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">desired</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">error</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">EWOULDBLOCK</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase> <phrase role="identifier">error</phrase> <phrase role="special">=</phrase> <phrase role="identifier">api</phrase><phrase role="special">.</phrase><phrase role="identifier">read</phrase><phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">desired</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// not ready yet -- try again on the next iteration of the</phrase> <phrase role="comment">// application's main loop</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">error</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <bridgehead renderas="sect3" id="fiber.nonblocking.h3"> <phrase id="fiber.nonblocking.filling_all_desired_data"/><link linkend="fiber.nonblocking.filling_all_desired_data">Filling All Desired Data</link> </bridgehead> <para> Given <code><phrase role="identifier">read_chunk</phrase><phrase role="special">()</phrase></code>, we can straightforwardly iterate until we have all desired data: </para> <para> <programlisting><phrase role="comment">// keep reading until desired length, EOF or error</phrase> <phrase role="comment">// may return both partial data and nonzero error</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">read_desired</phrase><phrase role="special">(</phrase> <phrase role="identifier">NonblockingAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">desired</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// we're going to accumulate results into 'data'</phrase> <phrase role="identifier">data</phrase><phrase role="special">.</phrase><phrase role="identifier">clear</phrase><phrase role="special">();</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">chunk</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">error</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">data</phrase><phrase role="special">.</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">desired</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">(</phrase> <phrase role="identifier">error</phrase> <phrase role="special">=</phrase> <phrase role="identifier">read_chunk</phrase><phrase role="special">(</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">chunk</phrase><phrase role="special">,</phrase> <phrase role="identifier">desired</phrase> <phrase role="special">-</phrase> <phrase role="identifier">data</phrase><phrase role="special">.</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">data</phrase><phrase role="special">.</phrase><phrase role="identifier">append</phrase><phrase role="special">(</phrase> <phrase role="identifier">chunk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">error</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> (Of <emphasis>course</emphasis> there are more efficient ways to accumulate string data. That's not the point of this example.) </para> <bridgehead renderas="sect3" id="fiber.nonblocking.h4"> <phrase id="fiber.nonblocking.wrapping_it_up"/><link linkend="fiber.nonblocking.wrapping_it_up">Wrapping it Up</link> </bridgehead> <para> Finally, we can define a relevant exception: </para> <para> <programlisting><phrase role="comment">// exception class augmented with both partially-read data and errorcode</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">IncompleteRead</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">IncompleteRead</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">what</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">partial</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase> <phrase role="identifier">what</phrase><phrase role="special">),</phrase> <phrase role="identifier">partial_</phrase><phrase role="special">(</phrase> <phrase role="identifier">partial</phrase><phrase role="special">),</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">(</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">get_partial</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">partial_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">get_errorcode</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">partial_</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">ec_</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> and write a simple <code><phrase role="identifier">read</phrase><phrase role="special">()</phrase></code> function that either returns all desired data or throws <code><phrase role="identifier">IncompleteRead</phrase></code>: </para> <para> <programlisting><phrase role="comment">// read all desired data or throw IncompleteRead</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">read</phrase><phrase role="special">(</phrase> <phrase role="identifier">NonblockingAPI</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">desired</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">data</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">ec</phrase><phrase role="special">(</phrase> <phrase role="identifier">read_desired</phrase><phrase role="special">(</phrase> <phrase role="identifier">api</phrase><phrase role="special">,</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">desired</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// for present purposes, EOF isn't a failure</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="number">0</phrase> <phrase role="special">==</phrase> <phrase role="identifier">ec</phrase> <phrase role="special">||</phrase> <phrase role="identifier">EOF</phrase> <phrase role="special">==</phrase> <phrase role="identifier">ec</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">data</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">// oh oh, partial read</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">msg</phrase><phrase role="special">;</phrase> <phrase role="identifier">msg</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;NonblockingAPI::read() error &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">ec</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; after &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase><phrase role="special">.</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; of &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">desired</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; characters&quot;</phrase><phrase role="special">;</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">IncompleteRead</phrase><phrase role="special">(</phrase> <phrase role="identifier">msg</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">(),</phrase> <phrase role="identifier">data</phrase><phrase role="special">,</phrase> <phrase role="identifier">ec</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Once we can transparently wait for the next main-loop iteration using <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>, ordinary encapsulation Just Works. </para> <para> The source code above is found in <ulink url="../../examples/adapt_nonblocking.cpp">adapt_nonblocking.cpp</ulink>. </para> </section> <section id="fiber.when_any"> <title><anchor id="when_any"/><link linkend="fiber.when_any">when_any / when_all functionality</link></title> <bridgehead renderas="sect3" id="fiber.when_any.h0"> <phrase id="fiber.when_any.overview"/><link linkend="fiber.when_any.overview">Overview</link> </bridgehead> <para> A bit of wisdom from the early days of computing still holds true today: prefer to model program state using the instruction pointer rather than with Boolean flags. In other words, if the program must <quote>do something</quote> and then do something almost the same, but with minor changes... perhaps parts of that something should be broken out as smaller separate functions, rather than introducing flags to alter the internal behavior of a monolithic function. </para> <para> To that we would add: prefer to describe control flow using C++ native constructs such as function calls, <code><phrase role="keyword">if</phrase></code>, <code><phrase role="keyword">while</phrase></code>, <code><phrase role="keyword">for</phrase></code>, <code><phrase role="keyword">do</phrase></code> et al. rather than as chains of callbacks. </para> <para> One of the great strengths of <emphasis role="bold">Boost.Fiber</emphasis> is the flexibility it confers on the coder to restructure an application from chains of callbacks to straightforward C++ statement sequence, even when code in that fiber is in fact interleaved with code running in other fibers. </para> <para> There has been much recent discussion about the benefits of when_any and when_all functionality. When dealing with asynchronous and possibly unreliable services, these are valuable idioms. But of course when_any and when_all are closely tied to the use of chains of callbacks. </para> <para> This section presents recipes for achieving the same ends, in the context of a fiber that wants to <quote>do something</quote> when one or more other independent activities have completed. Accordingly, these are <code><phrase role="identifier">wait_something</phrase><phrase role="special">()</phrase></code> functions rather than <code><phrase role="identifier">when_something</phrase><phrase role="special">()</phrase></code> functions. The expectation is that the calling fiber asks to launch those independent activities, then waits for them, then sequentially proceeds with whatever processing depends on those results. </para> <para> The function names shown (e.g. <link linkend="wait_first_simple"><code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code></link>) are for illustrative purposes only, because all these functions have been bundled into a single source file. Presumably, if (say) <link linkend="wait_first_success"><code><phrase role="identifier">wait_first_success</phrase><phrase role="special">()</phrase></code></link> best suits your application needs, you could introduce that variant with the name <code><phrase role="identifier">wait_any</phrase><phrase role="special">()</phrase></code>. </para> <note> <para> The functions presented in this section accept variadic argument lists of task functions. Corresponding <code><phrase role="identifier">wait_something</phrase><phrase role="special">()</phrase></code> functions accepting a container of task functions are left as an exercise for the interested reader. Those should actually be simpler. Most of the complexity would arise from overloading the same name for both purposes. </para> </note> <para> All the source code for this section is found in <ulink url="../../examples/wait_stuff.cpp">wait_stuff.cpp</ulink>. </para> <bridgehead renderas="sect3" id="fiber.when_any.h1"> <phrase id="fiber.when_any.example_task_function"/><link linkend="fiber.when_any.example_task_function">Example Task Function</link> </bridgehead> <para> <anchor id="wait_sleeper"/>We found it convenient to model an asynchronous task using this function: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">sleeper_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">item</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">ms</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">thrw</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">descb</phrase><phrase role="special">,</phrase> <phrase role="identifier">funcb</phrase><phrase role="special">;</phrase> <phrase role="identifier">descb</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">item</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">desc</phrase><phrase role="special">(</phrase> <phrase role="identifier">descb</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">funcb</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; sleeper(&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">item</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;)&quot;</phrase><phrase role="special">;</phrase> <phrase role="identifier">Verbose</phrase> <phrase role="identifier">v</phrase><phrase role="special">(</phrase> <phrase role="identifier">funcb</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">sleep_for</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">milliseconds</phrase><phrase role="special">(</phrase> <phrase role="identifier">ms</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">thrw</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase> <phrase role="identifier">desc</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">item</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> with type-specific <code><phrase role="identifier">sleeper</phrase><phrase role="special">()</phrase></code> <quote>front ends</quote> for <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase></code>, <code><phrase role="keyword">double</phrase></code> and <code><phrase role="keyword">int</phrase></code>. </para> <para> <code><phrase role="identifier">Verbose</phrase></code> simply prints a message to <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase></code> on construction and destruction. </para> <para> Basically: </para> <orderedlist> <listitem> <simpara> <code><phrase role="identifier">sleeper</phrase><phrase role="special">()</phrase></code> prints a start message; </simpara> </listitem> <listitem> <simpara> sleeps for the specified number of milliseconds; </simpara> </listitem> <listitem> <simpara> if <code><phrase role="identifier">thrw</phrase></code> is passed as <code><phrase role="keyword">true</phrase></code>, throws a string description of the passed <code><phrase role="identifier">item</phrase></code>; </simpara> </listitem> <listitem> <simpara> else returns the passed <code><phrase role="identifier">item</phrase></code>. </simpara> </listitem> <listitem> <simpara> On the way out, <code><phrase role="identifier">sleeper</phrase><phrase role="special">()</phrase></code> produces a stop message. </simpara> </listitem> </orderedlist> <para> This function will feature in the example calls to the various functions presented below. </para> <section id="fiber.when_any.when_any"> <title><link linkend="fiber.when_any.when_any">when_any</link></title> <section id="fiber.when_any.when_any.when_any__simple_completion"> <title><anchor id="wait_first_simple_section"/><link linkend="fiber.when_any.when_any.when_any__simple_completion">when_any, simple completion</link></title> <para> The simplest case is when you only need to know that the first of a set of asynchronous tasks has completed &mdash; but you don't need to obtain a return value, and you're confident that they will not throw exceptions. </para> <para> <anchor id="wait_done"/>For this we introduce a <code><phrase role="identifier">Done</phrase></code> class to wrap a <code><phrase role="keyword">bool</phrase></code> variable with a <link linkend="class_condition_variable"><code>condition_variable</code></link> and a <link linkend="class_mutex"><code>mutex</code></link>: </para> <para> <programlisting><phrase role="comment">// Wrap canonical pattern for condition_variable + bool flag</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">Done</phrase> <phrase role="special">{</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase> <phrase role="identifier">cond</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="identifier">mutex</phrase><phrase role="special">;</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">ready</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Done</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">ptr</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lock</phrase><phrase role="special">(</phrase> <phrase role="identifier">mutex</phrase><phrase role="special">);</phrase> <phrase role="identifier">cond</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lock</phrase><phrase role="special">,</phrase> <phrase role="special">[</phrase><phrase role="keyword">this</phrase><phrase role="special">](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">ready</phrase><phrase role="special">;</phrase> <phrase role="special">});</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lock</phrase><phrase role="special">(</phrase> <phrase role="identifier">mutex</phrase><phrase role="special">);</phrase> <phrase role="identifier">ready</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">// release mutex</phrase> <phrase role="identifier">cond</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> The pattern we follow throughout this section is to pass a <ulink url="http://www.cplusplus.com/reference/memory/shared_ptr/"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;&gt;</phrase></code></ulink> to the relevant synchronization object to the various tasks' fiber functions. This eliminates nagging questions about the lifespan of the synchronization object relative to the last of the fibers. </para> <para> <anchor id="wait_first_simple"/><code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code> uses that tactic for <link linkend="wait_done"><code><phrase role="identifier">Done</phrase></code></link>: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_first_simple</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Use shared_ptr because each function's fiber will bind it separately,</phrase> <phrase role="comment">// and we're going to return before the last of them completes.</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">done</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Done</phrase> <phrase role="special">&gt;()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">done</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="identifier">done</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> <anchor id="wait_first_simple_impl"/><code><phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">()</phrase></code> is an ordinary recursion over the argument pack, capturing <code><phrase role="identifier">Done</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr</phrase></code> for each new fiber: </para> <para> <programlisting><phrase role="comment">// Degenerate case: when there are no functions to wait for, return</phrase> <phrase role="comment">// immediately.</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">Done</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="comment">// When there's at least one function to wait for, launch it and recur to</phrase> <phrase role="comment">// process the rest.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">Done</phrase><phrase role="special">::</phrase><phrase role="identifier">ptr</phrase> <phrase role="identifier">done</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="special">[</phrase><phrase role="identifier">done</phrase><phrase role="special">,</phrase> <phrase role="identifier">function</phrase><phrase role="special">](){</phrase> <phrase role="identifier">function</phrase><phrase role="special">();</phrase> <phrase role="identifier">done</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">notify</phrase><phrase role="special">();</phrase> <phrase role="special">}).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">done</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> The body of the fiber's lambda is extremely simple, as promised: call the function, notify <link linkend="wait_done"><code><phrase role="identifier">Done</phrase></code></link> when it returns. The first fiber to do so allows <code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code> to return &mdash; which is why it's useful to have <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">Done</phrase><phrase role="special">&gt;</phrase></code> manage the lifespan of our <code><phrase role="identifier">Done</phrase></code> object rather than declaring it as a stack variable in <code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code>. </para> <para> This is how you might call it: </para> <para> <programlisting><phrase role="identifier">wait_first_simple</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfs_long&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfs_medium&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfs_short&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> </programlisting> </para> <para> In this example, control resumes after <code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code> when <link linkend="wait_sleeper"><code><phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfs_short&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">)</phrase></code></link> completes &mdash; even though the other two <code><phrase role="identifier">sleeper</phrase><phrase role="special">()</phrase></code> fibers are still running. </para> </section> <section id="fiber.when_any.when_any.when_any__return_value"> <title><link linkend="fiber.when_any.when_any.when_any__return_value">when_any, return value</link></title> <para> It seems more useful to add the ability to capture the return value from the first of the task functions to complete. Again, we assume that none will throw an exception. </para> <para> One tactic would be to adapt our <link linkend="wait_done"><code><phrase role="identifier">Done</phrase></code></link> class to store the first of the return values, rather than a simple <code><phrase role="keyword">bool</phrase></code>. However, we choose instead to use a <link linkend="class_buffered_channel"><code>buffered_channel&lt;&gt;</code></link>. We'll only need to enqueue the first value, so we'll <link linkend="buffered_channel_close"><code>buffered_channel::close()</code></link> it once we've retrieved that value. Subsequent <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code> calls will return <code><phrase role="identifier">closed</phrase></code>. </para> <para> <anchor id="wait_first_value"/> <programlisting><phrase role="comment">// Assume that all passed functions have the same return type. The return type</phrase> <phrase role="comment">// of wait_first_value() is the return type of the first passed function. It is</phrase> <phrase role="comment">// simply invalid to pass NO functions.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">wait_first_value</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// launch all the relevant fibers</phrase> <phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// retrieve the first value</phrase> <phrase role="identifier">return_t</phrase> <phrase role="identifier">value</phrase><phrase role="special">(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// close the channel: no subsequent push() has to succeed</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> <anchor id="wait_first_value_impl"/>The meat of the <code><phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">()</phrase></code> function is as you might expect: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="special">[</phrase><phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">function</phrase><phrase role="special">](){</phrase> <phrase role="comment">// Ignore channel_op_status returned by push():</phrase> <phrase role="comment">// might be closed; we simply don't care.</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">}).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> It calls the passed function, pushes its return value and ignores the <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code> result. You might call it like this: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_first_value</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfv_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfv_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfv_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_first_value() =&gt; &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">result</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="string">&quot;wfv_first&quot;</phrase><phrase role="special">);</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_any.when_any__produce_first_outcome__whether_result_or_exception"> <title><link linkend="fiber.when_any.when_any.when_any__produce_first_outcome__whether_result_or_exception">when_any, produce first outcome, whether result or exception</link></title> <para> We may not be running in an environment in which we can guarantee no exception will be thrown by any of our task functions. In that case, the above implementations of <code><phrase role="identifier">wait_first_something</phrase><phrase role="special">()</phrase></code> would be naïve: as mentioned in <link linkend="exceptions">the section on Fiber Management</link>, an uncaught exception in one of our task fibers would cause <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase><phrase role="special">()</phrase></code> to be called. </para> <para> Let's at least ensure that such an exception would propagate to the fiber awaiting the first result. We can use <link linkend="class_future"><code>future&lt;&gt;</code></link> to transport either a return value or an exception. Therefore, we will change <link linkend="wait_first_value"><code><phrase role="identifier">wait_first_value</phrase><phrase role="special">()</phrase></code></link>'s <link linkend="class_buffered_channel"><code>buffered_channel&lt;&gt;</code></link> to hold <code><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase></code> items instead of simply <code><phrase role="identifier">T</phrase></code>. </para> <para> Once we have a <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> in hand, all we need do is call <link linkend="future_get"><code>future::get()</code></link>, which will either return the value or rethrow the exception. </para> <para> <anchor id="wait_first_outcome"/> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">wait_first_outcome</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// In this case, the value we pass through the channel is actually a</phrase> <phrase role="comment">// future -- which is already ready. future can carry either a value or an</phrase> <phrase role="comment">// exception.</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// launch all the relevant fibers</phrase> <phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// retrieve the first future</phrase> <phrase role="identifier">future_t</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// close the channel: no subsequent push() has to succeed</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="comment">// either return value or throw exception</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> So far so good &mdash; but there's a timing issue. How should we obtain the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> to <link linkend="buffered_channel_push"><code>buffered_channel::push()</code></link> on the queue? </para> <para> We could call <link linkend="fibers_async"><code>fibers::async()</code></link>. That would certainly produce a <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> for the task function. The trouble is that it would return too quickly! We only want <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> items for <emphasis>completed</emphasis> tasks on our <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;&gt;</phrase></code>. In fact, we only want the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> for the one that completes first. If each fiber launched by <code><phrase role="identifier">wait_first_outcome</phrase><phrase role="special">()</phrase></code> were to <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code> the result of calling <code><phrase role="identifier">async</phrase><phrase role="special">()</phrase></code>, the queue would only ever report the result of the leftmost task item &mdash; <emphasis>not</emphasis> the one that completes most quickly. </para> <para> Calling <link linkend="future_get"><code>future::get()</code></link> on the future returned by <code><phrase role="identifier">async</phrase><phrase role="special">()</phrase></code> wouldn't be right. You can only call <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> once per <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> instance! And if there were an exception, it would be rethrown inside the helper fiber at the producer end of the queue, rather than propagated to the consumer end. </para> <para> We could call <link linkend="future_wait"><code>future::wait()</code></link>. That would block the helper fiber until the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> became ready, at which point we could <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code> it to be retrieved by <code><phrase role="identifier">wait_first_outcome</phrase><phrase role="special">()</phrase></code>. </para> <para> That would work &mdash; but there's a simpler tactic that avoids creating an extra fiber. We can wrap the task function in a <link linkend="class_packaged_task"><code>packaged_task&lt;&gt;</code></link>. While one naturally thinks of passing a <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code> to a new fiber &mdash; that is, in fact, what <code><phrase role="identifier">async</phrase><phrase role="special">()</phrase></code> does &mdash; in this case, we're already running in the helper fiber at the producer end of the queue! We can simply <emphasis>call</emphasis> the <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code>. On return from that call, the task function has completed, meaning that the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> obtained from the <code><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;&gt;</phrase></code> is certain to be ready. At that point we can simply <code><phrase role="identifier">push</phrase><phrase role="special">()</phrase></code> it to the queue. </para> <para> <anchor id="wait_first_outcome_impl"/> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">CHANP</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">CHANP</phrase> <phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="comment">// Use std::bind() here for C++11 compatibility. C++11 lambda capture</phrase> <phrase role="comment">// can't move a move-only Fn type, but bind() can. Let bind() move the</phrase> <phrase role="comment">// channel pointer and the function into the bound object, passing</phrase> <phrase role="comment">// references into the lambda.</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="special">[](</phrase> <phrase role="identifier">CHANP</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Instantiate a packaged_task to capture any exception thrown by</phrase> <phrase role="comment">// function.</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">packaged_task</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">task</phrase><phrase role="special">(</phrase> <phrase role="identifier">function</phrase><phrase role="special">);</phrase> <phrase role="comment">// Immediately run this packaged_task on same fiber. We want</phrase> <phrase role="comment">// function() to have completed BEFORE we push the future.</phrase> <phrase role="identifier">task</phrase><phrase role="special">();</phrase> <phrase role="comment">// Pass the corresponding future to consumer. Ignore</phrase> <phrase role="comment">// channel_op_status returned by push(): might be closed; we</phrase> <phrase role="comment">// simply don't care.</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase> <phrase role="identifier">task</phrase><phrase role="special">.</phrase><phrase role="identifier">get_future</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">)).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Calling it might look like this: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_first_outcome</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfos_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfos_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfos_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_first_outcome(success) =&gt; &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">result</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="string">&quot;wfos_first&quot;</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">thrown</phrase><phrase role="special">;</phrase> <phrase role="keyword">try</phrase> <phrase role="special">{</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_first_outcome</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfof_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">,</phrase> <phrase role="keyword">true</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfof_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfof_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="special">}</phrase> <phrase role="keyword">catch</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">e</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">thrown</phrase> <phrase role="special">=</phrase> <phrase role="identifier">e</phrase><phrase role="special">.</phrase><phrase role="identifier">what</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_first_outcome(fail) threw '&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">thrown</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;'&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">thrown</phrase> <phrase role="special">==</phrase> <phrase role="string">&quot;wfof_first&quot;</phrase><phrase role="special">);</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_any.when_any__produce_first_success"> <title><link linkend="fiber.when_any.when_any.when_any__produce_first_success">when_any, produce first success</link></title> <para> One scenario for <quote>when_any</quote> functionality is when we're redundantly contacting some number of possibly-unreliable web services. Not only might they be slow &mdash; any one of them might produce a failure rather than the desired result. </para> <para> In such a case, <link linkend="wait_first_outcome"><code><phrase role="identifier">wait_first_outcome</phrase><phrase role="special">()</phrase></code></link> isn't the right approach. If one of the services produces an error quickly, while another follows up with a real answer, we don't want to prefer the error just because it arrived first! </para> <para> Given the <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase></code> we already constructed for <code><phrase role="identifier">wait_first_outcome</phrase><phrase role="special">()</phrase></code>, though, we can readily recast the interface function to deliver the first <emphasis>successful</emphasis> result. </para> <para> That does beg the question: what if <emphasis>all</emphasis> the task functions throw an exception? In that case we'd probably better know about it. </para> <para> <anchor id="exception_list"/>The <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4407.html#parallel.exceptions.synopsis">C++ Parallelism Draft Technical Specification</ulink> proposes a <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_list</phrase></code> exception capable of delivering a collection of <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>s. Until that becomes universally available, let's fake up an <code><phrase role="identifier">exception_list</phrase></code> of our own: </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">exception_list</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">exception_list</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">what</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase> <phrase role="identifier">what</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">bundle_t</phrase><phrase role="special">;</phrase> <phrase role="comment">// N4407 proposed std::exception_list API</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">bundle_t</phrase><phrase role="special">::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">iterator</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">bundle_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">bundle_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">bundle_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="comment">// extension to populate</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">add</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">ep</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">bundle_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">ep</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="identifier">bundle_t</phrase> <phrase role="identifier">bundle_</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Now we can build <code><phrase role="identifier">wait_first_success</phrase><phrase role="special">()</phrase></code>, using <link linkend="wait_first_outcome_impl"><code><phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">()</phrase></code></link>. </para> <para> Instead of retrieving only the first <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> from the queue, we must now loop over <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> items. Of course we must limit that iteration! If we launch only <code><phrase role="identifier">count</phrase></code> producer fibers, the <code><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">+</phrase><phrase role="number">1</phrase><phrase role="special">)</phrase></code><superscript>st</superscript> <link linkend="buffered_channel_pop"><code>buffered_channel::pop()</code></link> call would block forever. </para> <para> Given a ready <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code>, we can distinguish failure by calling <link linkend="future_get_exception_ptr"><code>future::get_exception_ptr()</code></link>. If the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> in fact contains a result rather than an exception, <code><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase></code> returns <code><phrase role="keyword">nullptr</phrase></code>. In that case, we can confidently call <link linkend="future_get"><code>future::get()</code></link> to return that result to our caller. </para> <para> If the <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code> is <emphasis>not</emphasis> <code><phrase role="keyword">nullptr</phrase></code>, though, we collect it into our pending <code><phrase role="identifier">exception_list</phrase></code> and loop back for the next <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code> from the queue. </para> <para> If we fall out of the loop &mdash; if every single task fiber threw an exception &mdash; we throw the <code><phrase role="identifier">exception_list</phrase></code> exception into which we've been collecting those <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>s. </para> <para> <anchor id="wait_first_success"/> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">wait_first_success</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// In this case, the value we pass through the channel is actually a</phrase> <phrase role="comment">// future -- which is already ready. future can carry either a value or an</phrase> <phrase role="comment">// exception.</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// launch all the relevant fibers</phrase> <phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// instantiate exception_list, just in case</phrase> <phrase role="identifier">exception_list</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wait_first_success() produced only errors&quot;</phrase><phrase role="special">);</phrase> <phrase role="comment">// retrieve up to 'count' results -- but stop there!</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// retrieve the next future</phrase> <phrase role="identifier">future_t</phrase> <phrase role="identifier">future</phrase><phrase role="special">(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// retrieve exception_ptr if any</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">error</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// if no error, then yay, return value</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">error</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// close the channel: no subsequent push() has to succeed</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="comment">// show caller the value we got</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="comment">// error is non-null: collect</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">.</phrase><phrase role="identifier">add</phrase><phrase role="special">(</phrase> <phrase role="identifier">error</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// We only arrive here when every passed function threw an exception.</phrase> <phrase role="comment">// Throw our collection to inform caller.</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> A call might look like this: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_first_success</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfss_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">,</phrase> <phrase role="keyword">true</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfss_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfss_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_first_success(success) =&gt; &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">result</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="string">&quot;wfss_second&quot;</phrase><phrase role="special">);</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_any.when_any__heterogeneous_types"> <title><link linkend="fiber.when_any.when_any.when_any__heterogeneous_types">when_any, heterogeneous types</link></title> <para> We would be remiss to ignore the case in which the various task functions have distinct return types. That means that the value returned by the first of them might have any one of those types. We can express that with <ulink url="http://www.boost.org/doc/libs/release/doc/html/variant.html">Boost.Variant</ulink>. </para> <para> To keep the example simple, we'll revert to pretending that none of them can throw an exception. That makes <code><phrase role="identifier">wait_first_value_het</phrase><phrase role="special">()</phrase></code> strongly resemble <link linkend="wait_first_value"><code><phrase role="identifier">wait_first_value</phrase><phrase role="special">()</phrase></code></link>. We can actually reuse <link linkend="wait_first_value_impl"><code><phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">()</phrase></code></link>, merely passing <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">variant</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T0</phrase><phrase role="special">,</phrase> <phrase role="identifier">T1</phrase><phrase role="special">,</phrase> <phrase role="special">...&gt;</phrase></code> as the queue's value type rather than the common <code><phrase role="identifier">T</phrase></code>! </para> <para> Naturally this could be extended to use <link linkend="wait_first_success"><code><phrase role="identifier">wait_first_success</phrase><phrase role="special">()</phrase></code></link> semantics instead. </para> <para> <programlisting><phrase role="comment">// No need to break out the first Fn for interface function: let the compiler</phrase> <phrase role="comment">// complain if empty.</phrase> <phrase role="comment">// Our functions have different return types, and we might have to return any</phrase> <phrase role="comment">// of them. Use a variant, expanding std::result_of&lt;Fn()&gt;::type for each Fn in</phrase> <phrase role="comment">// parameter pack.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">variant</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">...</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_first_value_het</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Use buffered_channel&lt;boost::variant&lt;T1, T2, ...&gt;&gt;; see remarks above.</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">variant</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">...</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// launch all the relevant fibers</phrase> <phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// retrieve the first value</phrase> <phrase role="identifier">return_t</phrase> <phrase role="identifier">value</phrase><phrase role="special">(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">value_pop</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// close the channel: no subsequent push() has to succeed</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> It might be called like this: </para> <para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">variant</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">,</phrase> <phrase role="keyword">double</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_first_value_het</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wfvh_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="number">3.14</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="number">17</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_first_value_het() =&gt; &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">result</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">17</phrase><phrase role="special">);</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_any.when_any__a_dubious_alternative"> <title><link linkend="fiber.when_any.when_any.when_any__a_dubious_alternative">when_any, a dubious alternative</link></title> <para> Certain topics in C++ can arouse strong passions, and exceptions are no exception. We cannot resist mentioning &mdash; for purely informational purposes &mdash; that when you need only the <emphasis>first</emphasis> result from some number of concurrently-running fibers, it would be possible to pass a <literal>shared_ptr&lt;<link linkend="class_promise"><code>promise&lt;&gt;</code></link>&gt;</literal> to the participating fibers, then cause the initiating fiber to call <link linkend="future_get"><code>future::get()</code></link> on its <link linkend="class_future"><code>future&lt;&gt;</code></link>. The first fiber to call <link linkend="promise_set_value"><code>promise::set_value()</code></link> on that shared <code><phrase role="identifier">promise</phrase></code> will succeed; subsequent <code><phrase role="identifier">set_value</phrase><phrase role="special">()</phrase></code> calls on the same <code><phrase role="identifier">promise</phrase></code> instance will throw <code><phrase role="identifier">future_error</phrase></code>. </para> <para> Use this information at your own discretion. Beware the dark side. </para> </section> </section> <section id="fiber.when_any.when_all_functionality"> <title><link linkend="fiber.when_any.when_all_functionality">when_all functionality</link></title> <section id="fiber.when_any.when_all_functionality.when_all__simple_completion"> <title><link linkend="fiber.when_any.when_all_functionality.when_all__simple_completion">when_all, simple completion</link></title> <para> For the case in which we must wait for <emphasis>all</emphasis> task functions to complete &mdash; but we don't need results (or expect exceptions) from any of them &mdash; we can write <code><phrase role="identifier">wait_all_simple</phrase><phrase role="special">()</phrase></code> that looks remarkably like <link linkend="wait_first_simple"><code><phrase role="identifier">wait_first_simple</phrase><phrase role="special">()</phrase></code></link>. The difference is that instead of our <link linkend="wait_done"><code><phrase role="identifier">Done</phrase></code></link> class, we instantiate a <link linkend="class_barrier"><code>barrier</code></link> and call its <link linkend="barrier_wait"><code>barrier::wait()</code></link>. </para> <para> We initialize the <code><phrase role="identifier">barrier</phrase></code> with <code><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">+</phrase><phrase role="number">1</phrase><phrase role="special">)</phrase></code> because we are launching <code><phrase role="identifier">count</phrase></code> fibers, plus the <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> call within <code><phrase role="identifier">wait_all_simple</phrase><phrase role="special">()</phrase></code> itself. </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_all_simple</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// Initialize a barrier(count+1) because we'll immediately wait on it. We</phrase> <phrase role="comment">// don't want to wake up until 'count' more fibers wait on it. Even though</phrase> <phrase role="comment">// we'll stick around until the last of them completes, use shared_ptr</phrase> <phrase role="comment">// anyway because it's easier to be confident about lifespan issues.</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">barrier</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">wait_all_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> As stated above, the only difference between <code><phrase role="identifier">wait_all_simple_impl</phrase><phrase role="special">()</phrase></code> and <link linkend="wait_first_simple_impl"><code><phrase role="identifier">wait_first_simple_impl</phrase><phrase role="special">()</phrase></code></link> is that the former calls <code><phrase role="identifier">barrier</phrase><phrase role="special">::</phrase><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> rather than <code><phrase role="identifier">Done</phrase><phrase role="special">::</phrase><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code>: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_all_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">barrier</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase> <phrase role="special">[](</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">barrier</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">decay</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> <phrase role="identifier">function</phrase><phrase role="special">();</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">wait</phrase><phrase role="special">();</phrase> <phrase role="special">},</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">)).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="identifier">wait_all_simple_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">barrier</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> You might call it like this: </para> <para> <programlisting><phrase role="identifier">wait_all_simple</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;was_long&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;was_medium&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;was_short&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> </programlisting> </para> <para> Control will not return from the <code><phrase role="identifier">wait_all_simple</phrase><phrase role="special">()</phrase></code> call until the last of its task functions has completed. </para> </section> <section id="fiber.when_any.when_all_functionality.when_all__return_values"> <title><link linkend="fiber.when_any.when_all_functionality.when_all__return_values">when_all, return values</link></title> <para> As soon as we want to collect return values from all the task functions, we can see right away how to reuse <link linkend="wait_first_value"><code><phrase role="identifier">wait_first_value</phrase><phrase role="special">()</phrase></code></link>'s queue&lt;T&gt; for the purpose. All we have to do is avoid closing it after the first value! </para> <para> But in fact, collecting multiple values raises an interesting question: do we <emphasis>really</emphasis> want to wait until the slowest of them has arrived? Wouldn't we rather process each result as soon as it becomes available? </para> <para> Fortunately we can present both APIs. Let's define <code><phrase role="identifier">wait_all_values_source</phrase><phrase role="special">()</phrase></code> to return <code><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;</phrase></code>. </para> <para> <anchor id="wait_all_values"/>Given <code><phrase role="identifier">wait_all_values_source</phrase><phrase role="special">()</phrase></code>, it's straightforward to implement <code><phrase role="identifier">wait_all_values</phrase><phrase role="special">()</phrase></code>: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_all_values</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">vector_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">vector_t</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase> <phrase role="comment">// get channel</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_values_source</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// fill results vector</phrase> <phrase role="identifier">return_t</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// return vector to caller</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> It might be called like this: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">values</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_values</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wav_late&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wav_middle&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wav_early&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> </programlisting> </para> <para> As you can see from the loop in <code><phrase role="identifier">wait_all_values</phrase><phrase role="special">()</phrase></code>, instead of requiring its caller to count values, we define <code><phrase role="identifier">wait_all_values_source</phrase><phrase role="special">()</phrase></code> to <link linkend="buffered_channel_close"><code>buffered_channel::close()</code></link> the queue when done. But how do we do that? Each producer fiber is independent. It has no idea whether it is the last one to <link linkend="buffered_channel_push"><code>buffered_channel::push()</code></link> a value. </para> <para> <anchor id="wait_nqueue"/>We can address that problem with a counting façade for the <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;&gt;</phrase></code>. In fact, our façade need only support the producer end of the queue. </para> <para> [wait_nqueue] </para> <para> <anchor id="wait_all_values_source"/>Armed with <code><phrase role="identifier">nqueue</phrase><phrase role="special">&lt;&gt;</phrase></code>, we can implement <code><phrase role="identifier">wait_all_values_source</phrase><phrase role="special">()</phrase></code>. It starts just like <link linkend="wait_first_value"><code><phrase role="identifier">wait_first_value</phrase><phrase role="special">()</phrase></code></link>. The difference is that we wrap the <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> with an <code><phrase role="identifier">nqueue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> to pass to the producer fibers. </para> <para> Then, of course, instead of popping the first value, closing the queue and returning it, we simply return the <code><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">queue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;&gt;</phrase></code>. </para> <para> <programlisting><phrase role="comment">// Return a shared_ptr&lt;buffered_channel&lt;T&gt;&gt; from which the caller can</phrase> <phrase role="comment">// retrieve each new result as it arrives, until 'closed'.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_all_values_source</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="comment">// make the channel</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// and make an nchannel facade to close it after 'count' items</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">ncp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">nchannel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// pass that nchannel facade to all the relevant fibers</phrase> <phrase role="identifier">wait_all_values_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">ncp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// then return the channel for consumer</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> For example: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_values_source</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wavs_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wavs_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wavs_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_all_values_source() =&gt; '&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">value</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;'&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> <anchor id="wait_all_values_impl"/><code><phrase role="identifier">wait_all_values_impl</phrase><phrase role="special">()</phrase></code> really is just like <link linkend="wait_first_value_impl"><code><phrase role="identifier">wait_first_value_impl</phrase><phrase role="special">()</phrase></code></link> except for the use of <code><phrase role="identifier">nqueue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> rather than <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code>: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">wait_all_values_impl</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">nchannel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="special">[</phrase><phrase role="identifier">chan</phrase><phrase role="special">,</phrase> <phrase role="identifier">function</phrase><phrase role="special">](){</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="identifier">function</phrase><phrase role="special">());</phrase> <phrase role="special">}).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_all_functionality.when_all_until_first_exception"> <title><link linkend="fiber.when_any.when_all_functionality.when_all_until_first_exception">when_all until first exception</link></title> <para> Naturally, just as with <link linkend="wait_first_outcome"><code><phrase role="identifier">wait_first_outcome</phrase><phrase role="special">()</phrase></code></link>, we can elaborate <link linkend="wait_all_values"><code><phrase role="identifier">wait_all_values</phrase><phrase role="special">()</phrase></code></link> and <link linkend="wait_all_values_source"><code><phrase role="identifier">wait_all_values_source</phrase><phrase role="special">()</phrase></code></link> by passing <code><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase></code> instead of plain <code><phrase role="identifier">T</phrase></code>. </para> <para> <anchor id="wait_all_until_error"/><code><phrase role="identifier">wait_all_until_error</phrase><phrase role="special">()</phrase></code> pops that <code><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase></code> and calls its <link linkend="future_get"><code>future::get()</code></link>: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_all_until_error</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">vector_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">vector_t</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase> <phrase role="comment">// get channel</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">(</phrase> <phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// fill results vector</phrase> <phrase role="identifier">future_t</phrase> <phrase role="identifier">future</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="comment">// return vector to caller</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> For example: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">thrown</phrase><phrase role="special">;</phrase> <phrase role="keyword">try</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">values</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_until_error</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;waue_late&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;waue_middle&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">,</phrase> <phrase role="keyword">true</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;waue_early&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="special">}</phrase> <phrase role="keyword">catch</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">e</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">thrown</phrase> <phrase role="special">=</phrase> <phrase role="identifier">e</phrase><phrase role="special">.</phrase><phrase role="identifier">what</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_all_until_error(fail) threw '&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">thrown</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;'&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> </programlisting> </para> <para> <anchor id="wait_all_until_error_source"/>Naturally this complicates the API for <code><phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">()</phrase></code>. The caller must both retrieve a <code><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">T</phrase> <phrase role="special">&gt;</phrase></code> and call its <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> method. It would, of course, be possible to return a façade over the consumer end of the queue that would implicitly perform the <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> and return a simple <code><phrase role="identifier">T</phrase></code> (or throw). </para> <para> The implementation is just as you would expect. Notice, however, that we can reuse <link linkend="wait_first_outcome_impl"><code><phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">()</phrase></code></link>, passing the <code><phrase role="identifier">nqueue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code> rather than <code><phrase role="identifier">queue</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">T</phrase><phrase role="special">&gt;</phrase></code>. </para> <para> <programlisting><phrase role="comment">// Return a shared_ptr&lt;buffered_channel&lt;future&lt;T&gt;&gt;&gt; from which the caller can</phrase> <phrase role="comment">// get() each new result as it arrives, until 'closed'.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">channel_t</phrase><phrase role="special">;</phrase> <phrase role="comment">// make the channel</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">channel_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="number">64</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// and make an nchannel facade to close it after 'count' items</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">ncp</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">nchannel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// pass that nchannel facade to all the relevant fibers</phrase> <phrase role="identifier">wait_first_outcome_impl</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">ncp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="comment">// then return the channel for consumer</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">chanp</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> For example: </para> <para> <programlisting><phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wauess_third&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wauess_second&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wauess_first&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">future_t</phrase> <phrase role="identifier">future</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">value</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_all_until_error_source(success) =&gt; '&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">value</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;'&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> </section> <section id="fiber.when_any.when_all_functionality.wait_all__collecting_all_exceptions"> <title><link linkend="fiber.when_any.when_all_functionality.wait_all__collecting_all_exceptions">wait_all, collecting all exceptions</link></title> <para> <anchor id="wait_all_collect_errors"/>Given <link linkend="wait_all_until_error_source"><code><phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">()</phrase></code></link>, it might be more reasonable to make a <code><phrase role="identifier">wait_all_</phrase><phrase role="special">...()</phrase></code> that collects <emphasis>all</emphasis> errors instead of presenting only the first: </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">wait_all_collect_errors</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">function</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">count</phrase><phrase role="special">(</phrase> <phrase role="number">1</phrase> <phrase role="special">+</phrase> <phrase role="keyword">sizeof</phrase> <phrase role="special">...</phrase> <phrase role="special">(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">result_of</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">()</phrase> <phrase role="special">&gt;::</phrase><phrase role="identifier">type</phrase> <phrase role="identifier">return_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">future</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">future_t</phrase><phrase role="special">;</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">return_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">vector_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">vector_t</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase> <phrase role="identifier">exception_list</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wait_all_collect_errors() exceptions&quot;</phrase><phrase role="special">);</phrase> <phrase role="comment">// get channel</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">future_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">chan</phrase><phrase role="special">(</phrase> <phrase role="identifier">wait_all_until_error_source</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">function</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="comment">// fill results and/or exceptions vectors</phrase> <phrase role="identifier">future_t</phrase> <phrase role="identifier">future</phrase><phrase role="special">;</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">success</phrase> <phrase role="special">==</phrase> <phrase role="identifier">chan</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception_ptr</phrase> <phrase role="identifier">exp</phrase> <phrase role="special">=</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get_exception_ptr</phrase><phrase role="special">();</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">exp</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">results</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">future</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">.</phrase><phrase role="identifier">add</phrase><phrase role="special">(</phrase> <phrase role="identifier">exp</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">// if there were any exceptions, throw</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">exceptions</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">// no exceptions: return vector to caller</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">results</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> The implementation is a simple variation on <link linkend="wait_first_success"><code><phrase role="identifier">wait_first_success</phrase><phrase role="special">()</phrase></code></link>, using the same <link linkend="exception_list"><code><phrase role="identifier">exception_list</phrase></code></link> exception class. </para> </section> <section id="fiber.when_any.when_all_functionality.when_all__heterogeneous_types"> <title><link linkend="fiber.when_any.when_all_functionality.when_all__heterogeneous_types">when_all, heterogeneous types</link></title> <para> But what about the case when we must wait for all results of different types? </para> <para> We can present an API that is frankly quite cool. Consider a sample struct: </para> <para> <programlisting><phrase role="keyword">struct</phrase> <phrase role="identifier">Data</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">str</phrase><phrase role="special">;</phrase> <phrase role="keyword">double</phrase> <phrase role="identifier">inexact</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">exact</phrase><phrase role="special">;</phrase> <phrase role="keyword">friend</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostream</phrase><phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostream</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">out</phrase><phrase role="special">,</phrase> <phrase role="identifier">Data</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">data</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> Let's fill its members from task functions all running concurrently: </para> <para> <programlisting><phrase role="identifier">Data</phrase> <phrase role="identifier">data</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_members</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Data</phrase> <phrase role="special">&gt;(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wams_left&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="number">3.14</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="number">17</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_all_members&lt;Data&gt;(success) =&gt; &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> </programlisting> </para> <para> Note that for this case, we abandon the notion of capturing the earliest result first, and so on: we must fill exactly the passed struct in left-to-right order. </para> <para> That permits a beautifully simple implementation: </para> <para> <programlisting><phrase role="comment">// Explicitly pass Result. This can be any type capable of being initialized</phrase> <phrase role="comment">// from the results of the passed functions, such as a struct.</phrase> <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Result</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">Result</phrase> <phrase role="identifier">wait_all_members</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Run each of the passed functions on a separate fiber, passing all their</phrase> <phrase role="comment">// futures to helper function for processing.</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">wait_all_members_get</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Result</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">async</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">Fns</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">functions</phrase><phrase role="special">)</phrase> <phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Result</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="special">...</phrase> <phrase role="identifier">Futures</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">Result</phrase> <phrase role="identifier">wait_all_members_get</phrase><phrase role="special">(</phrase> <phrase role="identifier">Futures</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="special">...</phrase> <phrase role="identifier">futures</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Fetch the results from the passed futures into Result's initializer</phrase> <phrase role="comment">// list. It's true that the get() calls here will block the implicit</phrase> <phrase role="comment">// iteration over futures -- but that doesn't matter because we won't be</phrase> <phrase role="comment">// done until the slowest of them finishes anyway. As results are</phrase> <phrase role="comment">// processed in argument-list order rather than order of completion, the</phrase> <phrase role="comment">// leftmost get() to throw an exception will cause that exception to</phrase> <phrase role="comment">// propagate to the caller.</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">Result</phrase><phrase role="special">{</phrase> <phrase role="identifier">futures</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">...</phrase> <phrase role="special">};</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> It is tempting to try to implement <code><phrase role="identifier">wait_all_members</phrase><phrase role="special">()</phrase></code> as a one-liner like this: </para> <programlisting><phrase role="keyword">return</phrase> <phrase role="identifier">Result</phrase><phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">async</phrase><phrase role="special">(</phrase><phrase role="identifier">functions</phrase><phrase role="special">).</phrase><phrase role="identifier">get</phrase><phrase role="special">()...</phrase> <phrase role="special">};</phrase> </programlisting> <para> The trouble with this tactic is that it would serialize all the task functions. The runtime makes a single pass through <code><phrase role="identifier">functions</phrase></code>, calling <link linkend="fibers_async"><code>fibers::async()</code></link> for each and then immediately calling <link linkend="future_get"><code>future::get()</code></link> on its returned <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code>. That blocks the implicit loop. The above is almost equivalent to writing: </para> <programlisting><phrase role="keyword">return</phrase> <phrase role="identifier">Result</phrase><phrase role="special">{</phrase> <phrase role="identifier">functions</phrase><phrase role="special">()...</phrase> <phrase role="special">};</phrase> </programlisting> <para> in which, of course, there is no concurrency at all. </para> <para> Passing the argument pack through a function-call boundary (<code><phrase role="identifier">wait_all_members_get</phrase><phrase role="special">()</phrase></code>) forces the runtime to make <emphasis>two</emphasis> passes: one in <code><phrase role="identifier">wait_all_members</phrase><phrase role="special">()</phrase></code> to collect the <code><phrase role="identifier">future</phrase><phrase role="special">&lt;&gt;</phrase></code>s from all the <code><phrase role="identifier">async</phrase><phrase role="special">()</phrase></code> calls, the second in <code><phrase role="identifier">wait_all_members_get</phrase><phrase role="special">()</phrase></code> to fetch each of the results. </para> <para> As noted in comments, within the <code><phrase role="identifier">wait_all_members_get</phrase><phrase role="special">()</phrase></code> parameter pack expansion pass, the blocking behavior of <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> becomes irrelevant. Along the way, we will hit the <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> for the slowest task function; after that every subsequent <code><phrase role="identifier">get</phrase><phrase role="special">()</phrase></code> will complete in trivial time. </para> <para> By the way, we could also use this same API to fill a vector or other collection: </para> <para> <programlisting><phrase role="comment">// If we don't care about obtaining results as soon as they arrive, and we</phrase> <phrase role="comment">// prefer a result vector in passed argument order rather than completion</phrase> <phrase role="comment">// order, wait_all_members() is another possible implementation of</phrase> <phrase role="comment">// wait_all_until_error().</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">strings</phrase> <phrase role="special">=</phrase> <phrase role="identifier">wait_all_members</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;(</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wamv_left&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">150</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wamv_middle&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">100</phrase><phrase role="special">);</phrase> <phrase role="special">},</phrase> <phrase role="special">[](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">sleeper</phrase><phrase role="special">(</phrase><phrase role="string">&quot;wamv_right&quot;</phrase><phrase role="special">,</phrase> <phrase role="number">50</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;wait_all_members&lt;vector&gt;() =&gt;&quot;</phrase><phrase role="special">;</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">str</phrase> <phrase role="special">:</phrase> <phrase role="identifier">strings</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; '&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">str</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;'&quot;</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> </programlisting> </para> </section> </section> </section> <section id="fiber.integration"> <title><anchor id="integration"/><link linkend="fiber.integration">Sharing a Thread with Another Main Loop</link></title> <section id="fiber.integration.overview"> <title><link linkend="fiber.integration.overview">Overview</link></title> <para> As always with cooperative concurrency, it is important not to let any one fiber monopolize the processor too long: that could <quote>starve</quote> other ready fibers. This section discusses a couple of solutions. </para> </section> <section id="fiber.integration.event_driven_program"> <title><link linkend="fiber.integration.event_driven_program">Event-Driven Program</link></title> <para> Consider a classic event-driven program, organized around a main loop that fetches and dispatches incoming I/O events. You are introducing <emphasis role="bold">Boost.Fiber</emphasis> because certain asynchronous I/O sequences are logically sequential, and for those you want to write and maintain code that looks and acts sequential. </para> <para> You are launching fibers on the application&#8217;s main thread because certain of their actions will affect its user interface, and the application&#8217;s UI framework permits UI operations only on the main thread. Or perhaps those fibers need access to main-thread data, and it would be too expensive in runtime (or development time) to robustly defend every such data item with thread synchronization primitives. </para> <para> You must ensure that the application&#8217;s main loop <emphasis>itself</emphasis> doesn&#8217;t monopolize the processor: that the fibers it launches will get the CPU cycles they need. </para> <para> The solution is the same as for any fiber that might claim the CPU for an extended time: introduce calls to <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>. The most straightforward approach is to call <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> on every iteration of your existing main loop. In effect, this unifies the application&#8217;s main loop with <emphasis role="bold">Boost.Fiber</emphasis>&#8217;s internal main loop. <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> allows the fiber manager to run any fibers that have become ready since the previous iteration of the application&#8217;s main loop. When these fibers have had a turn, control passes to the thread&#8217;s main fiber, which returns from <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> and resumes the application&#8217;s main loop. </para> </section> <section id="fiber.integration.embedded_main_loop"> <title><anchor id="embedded_main_loop"/><link linkend="fiber.integration.embedded_main_loop">Embedded Main Loop</link></title> <para> More challenging is when the application&#8217;s main loop is embedded in some other library or framework. Such an application will typically, after performing all necessary setup, pass control to some form of <code><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code> function from which control does not return until application shutdown. </para> <para> A <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink> program might call <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code></ulink> in this way. </para> <para> In general, the trick is to arrange to pass control to <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link> frequently. You could use an <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/high_resolution_timer.html">Asio timer</ulink> for that purpose. You could instantiate the timer, arranging to call a handler function when the timer expires. The handler function could call <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code>, then reset the timer and arrange to wake up again on its next expiration. </para> <para> Since, in this thought experiment, we always pass control to the fiber manager via <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code>, the calling fiber is never blocked. Therefore there is always at least one ready fiber. Therefore the fiber manager never calls <link linkend="algorithm_suspend_until"><code>algorithm::suspend_until()</code></link>. </para> <para> Using <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/post.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code></ulink> instead of setting a timer for some nonzero interval would be unfriendly to other threads. When all I/O is pending and all fibers are blocked, the io_service and the fiber manager would simply spin the CPU, passing control back and forth to each other. Using a timer allows tuning the responsiveness of this thread relative to others. </para> </section> <section id="fiber.integration.deeper_dive_into___boost_asio__"> <title><link linkend="fiber.integration.deeper_dive_into___boost_asio__">Deeper Dive into <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink></link></title> <para> By now the alert reader is thinking: but surely, with Asio in particular, we ought to be able to do much better than periodic polling pings! </para> <para> This turns out to be surprisingly tricky. We present a possible approach in <ulink url="../../examples/asio/round_robin.hpp"><code><phrase role="identifier">examples</phrase><phrase role="special">/</phrase><phrase role="identifier">asio</phrase><phrase role="special">/</phrase><phrase role="identifier">round_robin</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase></code></ulink>. </para> <para> One consequence of using <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink> is that you must always let Asio suspend the running thread. Since Asio is aware of pending I/O requests, it can arrange to suspend the thread in such a way that the OS will wake it on I/O completion. No one else has sufficient knowledge. </para> <para> So the fiber scheduler must depend on Asio for suspension and resumption. It requires Asio handler calls to wake it. </para> <para> One dismaying implication is that we cannot support multiple threads calling <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code></ulink> on the same <code><phrase role="identifier">io_service</phrase></code> instance. The reason is that Asio provides no way to constrain a particular handler to be called only on a specified thread. A fiber scheduler instance is locked to a particular thread: that instance cannot manage any other thread&#8217;s fibers. Yet if we allow multiple threads to call <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code> on the same <code><phrase role="identifier">io_service</phrase></code> instance, a fiber scheduler which needs to sleep can have no guarantee that it will reawaken in a timely manner. It can set an Asio timer, as described above &mdash; but that timer&#8217;s handler may well execute on a different thread! </para> <para> Another implication is that since an Asio-aware fiber scheduler (not to mention <link linkend="callbacks_asio"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase></code></link>) depends on handler calls from the <code><phrase role="identifier">io_service</phrase></code>, it is the application&#8217;s responsibility to ensure that <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stop.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">stop</phrase><phrase role="special">()</phrase></code></ulink> is not called until every fiber has terminated. </para> <para> It is easier to reason about the behavior of the presented <code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code> scheduler if we require that after initial setup, the thread&#8217;s main fiber is the fiber that calls <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code>, so let&#8217;s impose that requirement. </para> <para> Naturally, the first thing we must do on each thread using a custom fiber scheduler is call <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link>. However, since <code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code> requires an <code><phrase role="identifier">io_service</phrase></code> instance, we must first declare that. </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">io_svc</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_shared</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase> <phrase role="special">&gt;();</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">);</phrase> </programlisting> </para> <para> <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code> instantiates <code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code>, which naturally calls its constructor: </para> <para> <programlisting><phrase role="identifier">round_robin</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">(</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">),</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">(</phrase> <phrase role="special">*</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// We use add_service() very deliberately. This will throw</phrase> <phrase role="comment">// service_already_exists if you pass the same io_service instance to</phrase> <phrase role="comment">// more than one round_robin instance.</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">add_service</phrase><phrase role="special">(</phrase> <phrase role="special">*</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">,</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">service</phrase><phrase role="special">(</phrase> <phrase role="special">*</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">post</phrase><phrase role="special">([</phrase><phrase role="keyword">this</phrase><phrase role="special">]()</phrase> <phrase role="keyword">mutable</phrase> <phrase role="special">{</phrase> </programlisting> </para> <para> <code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code> binds the passed <code><phrase role="identifier">io_service</phrase></code> pointer and initializes a <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/steady_timer.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_timer</phrase></code></ulink>: </para> <para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">shared_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_timer</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">;</phrase> </programlisting> </para> <para> Then it calls <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/add_service.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">add_service</phrase><phrase role="special">()</phrase></code></ulink> with a nested <code><phrase role="identifier">service</phrase></code> struct: </para> <para> <programlisting><phrase role="keyword">struct</phrase> <phrase role="identifier">service</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">service</phrase> <phrase role="special">{</phrase> <phrase role="keyword">static</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_ptr</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">work</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">work_</phrase><phrase role="special">;</phrase> <phrase role="identifier">service</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">service</phrase><phrase role="special">(</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">),</phrase> <phrase role="identifier">work_</phrase><phrase role="special">{</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">work</phrase><phrase role="special">(</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">)</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">service</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase> <phrase role="identifier">service</phrase><phrase role="special">(</phrase> <phrase role="identifier">service</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">service</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">service</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">shutdown_service</phrase><phrase role="special">()</phrase> <phrase role="identifier">override</phrase> <phrase role="identifier">final</phrase> <phrase role="special">{</phrase> <phrase role="identifier">work_</phrase><phrase role="special">.</phrase><phrase role="identifier">reset</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">};</phrase> </programlisting> </para> <para> ... [asio_rr_service_bottom] </para> <para> The <code><phrase role="identifier">service</phrase></code> struct has a couple of roles. </para> <para> Its foremost role is to manage a <literal>std::unique_ptr&lt;<ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service__work.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">work</phrase></code></ulink>&gt;</literal>. We want the <code><phrase role="identifier">io_service</phrase></code> instance to continue its main loop even when there is no pending Asio I/O. </para> <para> But when <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service__service/shutdown_service.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">service</phrase><phrase role="special">::</phrase><phrase role="identifier">shutdown_service</phrase><phrase role="special">()</phrase></code></ulink> is called, we discard the <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">work</phrase></code> instance so the <code><phrase role="identifier">io_service</phrase></code> can shut down properly. </para> <para> Its other purpose is to <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/post.html"><code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code></ulink> a lambda (not yet shown). Let&#8217;s walk further through the example program before coming back to explain that lambda. </para> <para> The <code><phrase role="identifier">service</phrase></code> constructor returns to <code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code>&#8217;s constructor, which returns to <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code>, which returns to the application code. </para> <para> Once it has called <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code>, the application may now launch some number of fibers: </para> <para> <programlisting><phrase role="comment">// server</phrase> <phrase role="identifier">tcp</phrase><phrase role="special">::</phrase><phrase role="identifier">acceptor</phrase> <phrase role="identifier">a</phrase><phrase role="special">(</phrase> <phrase role="special">*</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">,</phrase> <phrase role="identifier">tcp</phrase><phrase role="special">::</phrase><phrase role="identifier">endpoint</phrase><phrase role="special">(</phrase> <phrase role="identifier">tcp</phrase><phrase role="special">::</phrase><phrase role="identifier">v4</phrase><phrase role="special">(),</phrase> <phrase role="number">9999</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">server</phrase><phrase role="special">,</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">a</phrase><phrase role="special">)</phrase> <phrase role="special">).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="comment">// client</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">iterations</phrase> <phrase role="special">=</phrase> <phrase role="number">2</phrase><phrase role="special">;</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">clients</phrase> <phrase role="special">=</phrase> <phrase role="number">3</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">barrier</phrase> <phrase role="identifier">b</phrase><phrase role="special">(</phrase> <phrase role="identifier">clients</phrase><phrase role="special">);</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">clients</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">client</phrase><phrase role="special">,</phrase> <phrase role="identifier">io_svc</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">a</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase> <phrase role="identifier">b</phrase><phrase role="special">),</phrase> <phrase role="identifier">iterations</phrase><phrase role="special">).</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Since we don&#8217;t specify a <link linkend="class_launch"><code>launch</code></link>, these fibers are ready to run, but have not yet been entered. </para> <para> Having set everything up, the application calls <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code></ulink>: </para> <para> <programlisting><phrase role="identifier">io_svc</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">run</phrase><phrase role="special">();</phrase> </programlisting> </para> <para> Now what? </para> <para> Because this <code><phrase role="identifier">io_service</phrase></code> instance owns an <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">work</phrase></code> instance, <code><phrase role="identifier">run</phrase><phrase role="special">()</phrase></code> does not immediately return. But &mdash; none of the fibers that will perform actual work has even been entered yet! </para> <para> Without that initial <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> call in <code><phrase role="identifier">service</phrase></code>&#8217;s constructor, <emphasis>nothing</emphasis> would happen. The application would hang right here. </para> <para> So, what should the <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> handler execute? Simply <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>? </para> <para> That would be a promising start. But we have no guarantee that any of the other fibers will initiate any Asio operations to keep the ball rolling. For all we know, every other fiber could reach a similar <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> call first. Control would return to the <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> handler, which would return to Asio, and... the application would hang. </para> <para> The <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> handler could <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> itself again. But as discussed in <link linkend="embedded_main_loop">the previous section</link>, once there are actual I/O operations in flight &mdash; once we reach a state in which no fiber is ready &mdash; that would cause the thread to spin. </para> <para> We could, of course, set an Asio timer &mdash; again as <link linkend="embedded_main_loop">previously discussed</link>. But in this <quote>deeper dive,</quote> we&#8217;re trying to do a little better. </para> <para> The key to doing better is that since we&#8217;re in a fiber, we can run an actual loop &mdash; not just a chain of callbacks. We can wait for <quote>something to happen</quote> by calling <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run_one.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code></ulink> &mdash; or we can execute already-queued Asio handlers by calling <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/poll.html"><code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">poll</phrase><phrase role="special">()</phrase></code></ulink>. </para> <para> Here&#8217;s the body of the lambda passed to the <code><phrase role="identifier">post</phrase><phrase role="special">()</phrase></code> call. </para> <para> <programlisting> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">stopped</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// run all pending handlers in round_robin</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">poll</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="comment">// block this fiber till all pending (ready) fibers are processed</phrase> <phrase role="comment">// == round_robin::suspend_until() has been called</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">);</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase> <phrase role="comment">// run one handler inside io_service</phrase> <phrase role="comment">// if no handler available, block this thread</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">io_svc_</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">break</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> We want this loop to exit once the <code><phrase role="identifier">io_service</phrase></code> instance has been <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stopped.html"><code><phrase role="identifier">stopped</phrase><phrase role="special">()</phrase></code></ulink>. </para> <para> As long as there are ready fibers, we interleave running ready Asio handlers with running ready fibers. </para> <para> If there are no ready fibers, we wait by calling <code><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code>. Once any Asio handler has been called &mdash; no matter which &mdash; <code><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code> returns. That handler may have transitioned some fiber to ready state, so we loop back to check again. </para> <para> (We won&#8217;t describe <code><phrase role="identifier">awakened</phrase><phrase role="special">()</phrase></code>, <code><phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase></code> or <code><phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase></code>, as these are just like <link linkend="round_robin_awakened"><code>round_robin::awakened()</code></link>, <link linkend="round_robin_pick_next"><code>round_robin::pick_next()</code></link> and <link linkend="round_robin_has_ready_fibers"><code>round_robin::has_ready_fibers()</code></link>.) </para> <para> That leaves <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code>. </para> <para> Doubtless you have been asking yourself: why are we calling <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code> in the lambda loop? Why not call it in <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code>, whose very API was designed for just such a purpose? </para> <para> Under normal circumstances, when the fiber manager finds no ready fibers, it calls <link linkend="algorithm_suspend_until"><code>algorithm::suspend_until()</code></link>. Why test <code><phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase></code> in the lambda loop? Why not leverage the normal mechanism? </para> <para> The answer is: it matters who&#8217;s asking. </para> <para> Consider the lambda loop shown above. The only <emphasis role="bold">Boost.Fiber</emphasis> APIs it engages are <code><phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase></code> and <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>. <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> does not <emphasis>block</emphasis> the calling fiber: the calling fiber does not become unready. It is immediately passed back to <link linkend="algorithm_awakened"><code>algorithm::awakened()</code></link>, to be resumed in its turn when all other ready fibers have had a chance to run. In other words: during a <code><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> call, <emphasis>there is always at least one ready fiber.</emphasis> </para> <para> As long as this lambda loop is still running, the fiber manager does not call <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> because it always has a fiber ready to run. </para> <para> However, the lambda loop <emphasis>itself</emphasis> can detect the case when no <emphasis>other</emphasis> fibers are ready to run: the running fiber is not <emphasis>ready</emphasis> but <emphasis>running.</emphasis> </para> <para> That said, <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> and <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> are in fact called during orderly shutdown processing, so let&#8217;s try a plausible implementation. </para> <para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Set a timer so at least one handler will eventually fire, causing</phrase> <phrase role="comment">// run_one() to eventually return.</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">::</phrase><phrase role="identifier">max</phrase><phrase role="special">)()</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Each expires_at(time_point) call cancels any previous pending</phrase> <phrase role="comment">// call. We could inadvertently spin like this:</phrase> <phrase role="comment">// dispatcher calls suspend_until() with earliest wake time</phrase> <phrase role="comment">// suspend_until() sets suspend_timer_</phrase> <phrase role="comment">// lambda loop calls run_one()</phrase> <phrase role="comment">// some other asio handler runs before timer expires</phrase> <phrase role="comment">// run_one() returns to lambda loop</phrase> <phrase role="comment">// lambda loop yields to dispatcher</phrase> <phrase role="comment">// dispatcher finds no ready fibers</phrase> <phrase role="comment">// dispatcher calls suspend_until() with SAME wake time</phrase> <phrase role="comment">// suspend_until() sets suspend_timer_ to same time, canceling</phrase> <phrase role="comment">// previous async_wait()</phrase> <phrase role="comment">// lambda loop calls run_one()</phrase> <phrase role="comment">// asio calls suspend_timer_ handler with operation_aborted</phrase> <phrase role="comment">// run_one() returns to lambda loop... etc. etc.</phrase> <phrase role="comment">// So only actually set the timer when we're passed a DIFFERENT</phrase> <phrase role="comment">// abs_time value.</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">.</phrase><phrase role="identifier">expires_at</phrase><phrase role="special">(</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">);</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">.</phrase><phrase role="identifier">async_wait</phrase><phrase role="special">([](</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;){</phrase> <phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase> <phrase role="special">}</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_one</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> As you might expect, <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> sets an <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/steady_timer.html"><code><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_timer</phrase></code></ulink> to <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/expires_at.html"><code><phrase role="identifier">expires_at</phrase><phrase role="special">()</phrase></code></ulink> the passed <ulink url="http://en.cppreference.com/w/cpp/chrono/steady_clock"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase></code></ulink>. Usually. </para> <para> As indicated in comments, we avoid setting <code><phrase role="identifier">suspend_timer_</phrase></code> multiple times to the <emphasis>same</emphasis> <code><phrase role="identifier">time_point</phrase></code> value since every <code><phrase role="identifier">expires_at</phrase><phrase role="special">()</phrase></code> call cancels any previous <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/async_wait.html"><code><phrase role="identifier">async_wait</phrase><phrase role="special">()</phrase></code></ulink> call. There is a chance that we could spin. Reaching <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> means the fiber manager intends to yield the processor to Asio. Cancelling the previous <code><phrase role="identifier">async_wait</phrase><phrase role="special">()</phrase></code> call would fire its handler, causing <code><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code> to return, potentially causing the fiber manager to call <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> again with the same <code><phrase role="identifier">time_point</phrase></code> value... </para> <para> Given that we suspend the thread by calling <code><phrase role="identifier">io_service</phrase><phrase role="special">::</phrase><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code>, what&#8217;s important is that our <code><phrase role="identifier">async_wait</phrase><phrase role="special">()</phrase></code> call will cause a handler to run, which will cause <code><phrase role="identifier">run_one</phrase><phrase role="special">()</phrase></code> to return. It&#8217;s not so important specifically what that handler does. </para> <para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Something has happened that should wake one or more fibers BEFORE</phrase> <phrase role="comment">// suspend_timer_ expires. Reset the timer to cause it to fire</phrase> <phrase role="comment">// immediately, causing the run_one() call to return. In theory we</phrase> <phrase role="comment">// could use cancel() because we don't care whether suspend_timer_'s</phrase> <phrase role="comment">// handler is called with operation_aborted or success. However --</phrase> <phrase role="comment">// cancel() doesn't change the expiration time, and we use</phrase> <phrase role="comment">// suspend_timer_'s expiration time to decide whether it's already</phrase> <phrase role="comment">// set. If suspend_until() set some specific wake time, then notify()</phrase> <phrase role="comment">// canceled it, then suspend_until() was called again with the same</phrase> <phrase role="comment">// wake time, it would match suspend_timer_'s expiration time and we'd</phrase> <phrase role="comment">// refrain from setting the timer. So instead of simply calling</phrase> <phrase role="comment">// cancel(), reset the timer, which cancels the pending sleep AND sets</phrase> <phrase role="comment">// a new expiration time. This will cause us to spin the loop twice --</phrase> <phrase role="comment">// once for the operation_aborted handler, once for timer expiration</phrase> <phrase role="comment">// -- but that shouldn't be a big problem.</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">.</phrase><phrase role="identifier">async_wait</phrase><phrase role="special">([](</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">system</phrase><phrase role="special">::</phrase><phrase role="identifier">error_code</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;){</phrase> <phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase> <phrase role="identifier">suspend_timer_</phrase><phrase role="special">.</phrase><phrase role="identifier">expires_at</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">now</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Since an <code><phrase role="identifier">expires_at</phrase><phrase role="special">()</phrase></code> call cancels any previous <code><phrase role="identifier">async_wait</phrase><phrase role="special">()</phrase></code> call, we can make <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> simply call <code><phrase role="identifier">steady_timer</phrase><phrase role="special">::</phrase><phrase role="identifier">expires_at</phrase><phrase role="special">()</phrase></code>. That should cause the <code><phrase role="identifier">io_service</phrase></code> to call the <code><phrase role="identifier">async_wait</phrase><phrase role="special">()</phrase></code> handler with <code><phrase role="identifier">operation_aborted</phrase></code>. </para> <para> The comments in <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> explain why we call <code><phrase role="identifier">expires_at</phrase><phrase role="special">()</phrase></code> rather than <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/cancel.html"><code><phrase role="identifier">cancel</phrase><phrase role="special">()</phrase></code></ulink>. </para> <para> This <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">round_robin</phrase></code> implementation is used in <ulink url="../../examples/asio/autoecho.cpp"><code><phrase role="identifier">examples</phrase><phrase role="special">/</phrase><phrase role="identifier">asio</phrase><phrase role="special">/</phrase><phrase role="identifier">autoecho</phrase><phrase role="special">.</phrase><phrase role="identifier">cpp</phrase></code></ulink>. </para> <para> It seems possible that you could put together a more elegant Fiber / Asio integration. But as noted at the outset: it&#8217;s tricky. </para> </section> </section> <section id="fiber.speculation"> <title><anchor id="speculation"/><link linkend="fiber.speculation">Specualtive execution</link></title> <bridgehead renderas="sect3" id="fiber.speculation.h0"> <phrase id="fiber.speculation.hardware_transactional_memory"/><link linkend="fiber.speculation.hardware_transactional_memory">Hardware transactional memory</link> </bridgehead> <para> With help of hardware transactional memory multiple logical processors execute a critical region speculatively, e.g. without explicit synchronization.<sbr/> If the transactional execution completes successfully, then all memory operations performed within the transactional region are commited without any inter-thread serialization.<sbr/> When the optimistic execution fails, the processor aborts the transaction and discards all performed modifications.<sbr/> In non-transactional code a single lock serializes the access to a critical region. With a transactional memory, multiple logical processor start a transaction and update the memory (the data) inside the ciritical region. Unless some logical processors try to update the same data, the transactions would always succeed. </para> <bridgehead renderas="sect3" id="fiber.speculation.h1"> <phrase id="fiber.speculation.intel_transactional_synchronisation_extensions__tsx_"/><link linkend="fiber.speculation.intel_transactional_synchronisation_extensions__tsx_">Intel Transactional Synchronisation Extensions (TSX)</link> </bridgehead> <para> TSX is Intel's implementation of hardware transactional memory in modern Intel processors<footnote id="fiber.speculation.f0"> <para> intel.com: <ulink url="https://software.intel.com/en-us/node/695149">Intel Transactional Synchronization Extensions</ulink> </para> </footnote>.<sbr/> In TSX the hardware keeps track of which cachelines have been read from and which have been written to in a transaction. The cache-line size (64-byte) and the n-way set associative cache determine the maximum size of memory in a transaction. For instance if a transaction modifies 9 cache-lines at a processor with a 8-way set associative cache, the transaction will always be aborted. </para> <note> <para> TXS is enabled if property <code><phrase role="identifier">htm</phrase><phrase role="special">=</phrase><phrase role="identifier">tsx</phrase></code> is specified at b2 command-line and <code><phrase role="identifier">BOOST_USE_TSX</phrase></code> is applied to the compiler. </para> </note> <note> <para> A TSX-transaction will be aborted if the floating point state is modified inside a critical region. As a consequence floating point operations, e.g. store/load of floating point related registers during a fiber (context) switch are disabled. </para> </note> <important> <para> TSX can not be used together with MSVC at this time! </para> </important> <para> Boost.Fiber uses TSX-enabled spinlocks to protect critical regions (see section <link linkend="tuning">Tuning</link>). </para> </section> <section id="fiber.numa"> <title><anchor id="numa"/><link linkend="fiber.numa">NUMA</link></title> <para> Modern micro-processors contain integrated memory controllers that are connected via channels to the memory. Accessing the memory can be organized in two kinds:<sbr/> Uniform Memory Access (UMA) and Non-Uniform Memory Access (NUMA). </para> <para> In contrast to UMA, that provides a centralized pool of memory (and thus does not scale after a certain number of processors), a NUMA architecture divides the memory into local and remote memory relative to the micro-processor.<sbr/> Local memory is directly attached to the processor's integrated memory controller. Memory connected to the memory controller of another micro-processor (multi-socket systems) is considered as remote memory. If a memory controller access remote memory it has to traverse the interconnect<footnote id="fiber.numa.f0"> <para> On x86 the interconnection is implemented by Intel's Quick Path Interconnect (QPI) and AMD's HyperTransport. </para> </footnote> and connect to the remote memory controller.<sbr/> Thus accessing remote memory adds additional latency overhead to local memory access. Because of the different memory locations, a NUMA-system experiences <emphasis>non-uniform</emphasis> memory access time.<sbr/> As a consequence the best performance is achieved by keeping the memory access local. </para> <para> <inlinemediaobject><imageobject><imagedata align="center" fileref="../../../../libs/fiber/doc/NUMA.png"></imagedata></imageobject> <textobject> <phrase>NUMA</phrase> </textobject> </inlinemediaobject> </para> <bridgehead renderas="sect3" id="fiber.numa.h0"> <phrase id="fiber.numa.numa_support_in_boost_fiber"/><link linkend="fiber.numa.numa_support_in_boost_fiber">NUMA support in Boost.Fiber</link> </bridgehead> <para> Because only a subset of the NUMA-functionality is exposed by several operating systems, Boost.Fiber provides only a minimalistic NUMA API. </para> <important> <para> In order to enable NUMA support, b2 property <code><phrase role="identifier">numa</phrase><phrase role="special">=</phrase><phrase role="identifier">on</phrase></code> must be specified and linked against additional library <code><phrase role="identifier">libboost_fiber_numa</phrase><phrase role="special">.</phrase><phrase role="identifier">so</phrase></code>. </para> </important> <important> <para> MinGW using pthread implementation is not supported on Windows. </para> </important> <table frame="all" id="fiber.numa.supported_functionality_operating_systems"> <title>Supported functionality/operating systems</title> <tgroup cols="7"> <thead> <row> <entry> </entry> <entry> <para> AIX </para> </entry> <entry> <para> FreeBSD </para> </entry> <entry> <para> HP/UX </para> </entry> <entry> <para> Linux </para> </entry> <entry> <para> Solaris </para> </entry> <entry> <para> Windows </para> </entry> </row> </thead> <tbody> <row> <entry> <para> pin thread </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> </row> <row> <entry> <para> logical CPUs/NUMA nodes </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> + </para> </entry> <entry> <para> +<footnote id="fiber.numa.f1"> <para> Windows organizes logical cpus in groups of 64; boost.fiber maps {group-id,cpud-id} to a scalar equivalent to cpu ID of Linux (64 * group ID + cpu ID). </para> </footnote> </para> </entry> </row> <row> <entry> <para> NUMA node distance </para> </entry> <entry> <para> - </para> </entry> <entry> <para> - </para> </entry> <entry> <para> - </para> </entry> <entry> <para> + </para> </entry> <entry> <para> - </para> </entry> <entry> <para> - </para> </entry> </row> <row> <entry> <para> tested on </para> </entry> <entry> <para> AIX 7.2 </para> </entry> <entry> <para> FreeBSD 11 </para> </entry> <entry> <para> - </para> </entry> <entry> <para> Arch Linux (4.10.13) </para> </entry> <entry> <para> OpenIndiana HIPSTER </para> </entry> <entry> <para> Windows 10 </para> </entry> </row> </tbody> </tgroup> </table> <para> In order to keep the memory access local as possible, the NUMA topology must be evaluated. </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">topo</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">topology</phrase><phrase role="special">();</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">n</phrase> <phrase role="special">:</phrase> <phrase role="identifier">topo</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;node: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">n</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; | &quot;</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;cpus: &quot;</phrase><phrase role="special">;</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">cpu_id</phrase> <phrase role="special">:</phrase> <phrase role="identifier">n</phrase><phrase role="special">.</phrase><phrase role="identifier">logical_cpus</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">cpu_id</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;| distance: &quot;</phrase><phrase role="special">;</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">d</phrase> <phrase role="special">:</phrase> <phrase role="identifier">n</phrase><phrase role="special">.</phrase><phrase role="identifier">distance</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">d</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;done&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">output</phrase><phrase role="special">:</phrase> <phrase role="identifier">node</phrase><phrase role="special">:</phrase> <phrase role="number">0</phrase> <phrase role="special">|</phrase> <phrase role="identifier">cpus</phrase><phrase role="special">:</phrase> <phrase role="number">0</phrase> <phrase role="number">1</phrase> <phrase role="number">2</phrase> <phrase role="number">3</phrase> <phrase role="number">4</phrase> <phrase role="number">5</phrase> <phrase role="number">6</phrase> <phrase role="number">7</phrase> <phrase role="number">16</phrase> <phrase role="number">17</phrase> <phrase role="number">18</phrase> <phrase role="number">19</phrase> <phrase role="number">20</phrase> <phrase role="number">21</phrase> <phrase role="number">22</phrase> <phrase role="number">23</phrase> <phrase role="special">|</phrase> <phrase role="identifier">distance</phrase><phrase role="special">:</phrase> <phrase role="number">10</phrase> <phrase role="number">21</phrase> <phrase role="identifier">node</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase> <phrase role="special">|</phrase> <phrase role="identifier">cpus</phrase><phrase role="special">:</phrase> <phrase role="number">8</phrase> <phrase role="number">9</phrase> <phrase role="number">10</phrase> <phrase role="number">11</phrase> <phrase role="number">12</phrase> <phrase role="number">13</phrase> <phrase role="number">14</phrase> <phrase role="number">15</phrase> <phrase role="number">24</phrase> <phrase role="number">25</phrase> <phrase role="number">26</phrase> <phrase role="number">27</phrase> <phrase role="number">28</phrase> <phrase role="number">29</phrase> <phrase role="number">30</phrase> <phrase role="number">31</phrase> <phrase role="special">|</phrase> <phrase role="identifier">distance</phrase><phrase role="special">:</phrase> <phrase role="number">21</phrase> <phrase role="number">10</phrase> <phrase role="identifier">done</phrase> </programlisting> <para> The example shows that the systems consits out of 2 NUMA-nodes, to each NUMA-node belong 16 logical cpus. The distance measures the costs to access the memory of another NUMA-node. A NUMA-node has always a distance <code><phrase role="number">10</phrase></code> to itself (lowest possible value).<sbr/> The position in the array corresponds with the NUMA-node ID. </para> <para> Some work-loads benefit from pinning threads to a logical cpus. For instance scheduling algorithm <link linkend="class_numa_work_stealing"><code>numa::work_stealing</code></link> pins the thread that runs the fiber scheduler to a logical cpu. This prevents the operating system scheduler to move the thread to another logical cpu that might run other fiber scheduler(s) or migrating the thread to a logical cpu part of another NUMA-node. </para> <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">node_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">topo</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// thread registers itself at work-stealing scheduler</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">node_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">topo</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> <phrase role="comment">// evaluate the NUMA topology</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">topo</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">topology</phrase><phrase role="special">();</phrase> <phrase role="comment">// start-thread runs on NUMA-node `0`</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">node</phrase> <phrase role="special">=</phrase> <phrase role="identifier">topo</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">];</phrase> <phrase role="comment">// start-thread is pinnded to first cpu ID in the list of logical cpus of NUMA-node `0`</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">start_cpu_id</phrase> <phrase role="special">=</phrase> <phrase role="special">*</phrase> <phrase role="identifier">node</phrase><phrase role="special">.</phrase><phrase role="identifier">logical_cpus</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase> <phrase role="comment">// start worker-threads first</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">threads</phrase><phrase role="special">;</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">auto</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">node</phrase> <phrase role="special">:</phrase> <phrase role="identifier">topo</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase> <phrase role="special">:</phrase> <phrase role="identifier">node</phrase><phrase role="special">.</phrase><phrase role="identifier">logical_cpus</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// exclude start-thread</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">start_cpu_id</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// spawn thread</phrase> <phrase role="identifier">threads</phrase><phrase role="special">.</phrase><phrase role="identifier">emplace_back</phrase><phrase role="special">(</phrase> <phrase role="identifier">thread</phrase><phrase role="special">,</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">node</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase> <phrase role="identifier">topo</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">// start-thread registers itself on work-stealing scheduler</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">start_cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">node</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="identifier">topo</phrase><phrase role="special">);</phrase> <phrase role="special">...</phrase> </programlisting> <para> The example evaluates the NUMA topology with <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">topology</phrase><phrase role="special">()</phrase></code> and spawns for each logical cpu a thread. Each spawned thread installs the NUMA-aware work-stealing scheduler. The scheduler pins the thread to the logical cpu that was specified at construction.<sbr/> If the local queue of one thread runs out of ready fibers, the thread tries to steal a ready fiber from another thread running at logical cpu that belong to the same NUMA-node (local memory access). If no fiber could be stolen, the thread tries to steal fibers from logical cpus part of other NUMA-nodes (remote memory access). </para> <bridgehead renderas="sect3" id="fiber.numa.h1"> <phrase id="fiber.numa.synopsis"/><link linkend="fiber.numa.synopsis">Synopsis</link> </bridgehead> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">pin_thread</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">topology</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">node</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">logical_cpus</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">distance</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">topology</phrase><phrase role="special">();</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">pin_thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase><phrase role="special">);</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">pin_thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">native_handle_type</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> <phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">work_stealing</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">;</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="class_numa_node_bridgehead"> <phrase id="class_numa_node"/> <link linkend="class_numa_node">Class <code>numa::node</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">topology</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">node</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">logical_cpus</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">distance</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;,</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="numa_node_id_bridgehead"> <phrase id="numa_node_id"/> <link linkend="numa_node_id">Data member <code>id</code></link> </bridgehead> </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> ID of the NUMA-node </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_node_logical_cpus_bridgehead"> <phrase id="numa_node_logical_cpus"/> <link linkend="numa_node_logical_cpus">Data member <code>logical_cpus</code></link> </bridgehead> </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">logical_cpus</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> set of logical cpu IDs belonging to the NUMA-node </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_node_distance_bridgehead"> <phrase id="numa_node_distance"/> <link linkend="numa_node_distance">Data member <code>distance</code></link> </bridgehead> </para> <programlisting><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">distance</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> The distance between NUMA-nodes describe the cots of accessing the remote memory. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> A NUMA-node has a distance of <code><phrase role="number">10</phrase></code> to itself, remote NUMA-nodes have a distance &gt; <code><phrase role="number">10</phrase></code>. The index in the array corresponds to the ID <code><phrase role="identifier">id</phrase></code> of the NUMA-node. At the moment only Linux returns the correct distances, for all other operating systems remote NUMA-nodes get a default value of <code><phrase role="number">20</phrase></code>. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_node_operator_less_bridgehead"> <phrase id="numa_node_operator_less"/> <link linkend="numa_node_operator_less">Member function <code>operator&lt;</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">lhs</phrase><phrase role="special">,</phrase> <phrase role="identifier">node</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">rhs</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if <code><phrase role="identifier">lhs</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">rhs</phrase></code> is true and the implementation-defined total order of <code><phrase role="identifier">node</phrase><phrase role="special">::</phrase><phrase role="identifier">id</phrase></code> values places <code><phrase role="identifier">lhs</phrase></code> before <code><phrase role="identifier">rhs</phrase></code>, false otherwise. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_topology_bridgehead"> <phrase id="numa_topology"/> <link linkend="numa_topology">Non-member function <code>numa::topology()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">topology</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">topology</phrase><phrase role="special">();</phrase> <phrase role="special">}}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Evaluates the NUMA topology. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> a vector of NUMA-nodes describing the NUMA architecture of the system (each element represents a NUMA-node). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">system_error</phrase></code> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_pin_thread_bridgehead"> <phrase id="numa_pin_thread"/> <link linkend="numa_pin_thread">Non-member function <code>numa::pin_thread()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">pin_thread</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">pin_thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">);</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">pin_thread</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">::</phrase><phrase role="identifier">native_handle_type</phrase> <phrase role="identifier">h</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> First version pins <code><phrase role="keyword">this</phrase> <phrase role="identifier">thread</phrase></code> to the logical cpu with ID <code><phrase role="identifier">cpu_id</phrase></code>, e.g. the operating system scheduler will not migrate the thread to another logical cpu. The second variant pins the thread with the native ID <code><phrase role="identifier">h</phrase></code> to logical cpu with ID <code><phrase role="identifier">cpu_id</phrase></code>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">system_error</phrase></code> </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="class_numa_work_stealing_bridgehead"> <phrase id="class_numa_work_stealing"/> <link linkend="class_numa_work_stealing">Class <code>numa::work_stealing</code></link> </bridgehead> </para> <para> This class implements <link linkend="class_algorithm"><code>algorithm</code></link>; the thread running this scheduler is pinned to the given logical cpu. If the local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers that run on logical cpus that belong to the same NUMA-node (local memory access).<sbr/> If no ready fibers can be stolen from the local NUMA-node, the algorithm selects schedulers running on other NUMA-nodes (remote memory access).<sbr/> The victim scheduler (from which a ready fiber is stolen) is selected at random. </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">numa</phrase><phrase role="special">/</phrase><phrase role="identifier">algo</phrase><phrase role="special">/</phrase><phrase role="identifier">work_stealing</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">numa</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">algo</phrase> <phrase role="special">{</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">algorithm</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">node_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">topo</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">suspend</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">);</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase> <phrase role="identifier">work_stealing</phrase> <phrase role="special">&amp;&amp;)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> <phrase role="special">}}}}</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.numa.h2"> <phrase id="fiber.numa.constructor"/><link linkend="fiber.numa.constructor">Constructor</link> </bridgehead> <programlisting><phrase role="identifier">work_stealing</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">cpu_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uint32_t</phrase> <phrase role="identifier">node_id</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">numa</phrase><phrase role="special">::</phrase><phrase role="identifier">node</phrase> <phrase role="special">&gt;</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">topo</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">suspend</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">);</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Constructs work-stealing scheduling algorithm. The thread is pinned to logical cpu with ID <code><phrase role="identifier">cpu_id</phrase></code>. If local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers using <code><phrase role="identifier">topology</phrase></code> (represents the NUMA-topology of the system). </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> <code><phrase role="identifier">system_error</phrase></code> </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> If <code><phrase role="identifier">suspend</phrase></code> is set to <code><phrase role="keyword">true</phrase></code>, then the scheduler suspends if no ready fiber could be stolen. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote (other thread or fiber scheduler). </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_work_stealing_awakened_bridgehead"> <phrase id="numa_work_stealing_awakened"/> <link linkend="numa_work_stealing_awakened">Member function <code>awakened</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Enqueues fiber <code><phrase role="identifier">f</phrase></code> onto the shared ready queue. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_work_stealing_pick_next_bridgehead"> <phrase id="numa_work_stealing_pick_next"/> <link linkend="numa_work_stealing_pick_next">Member function <code>pick_next</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> the fiber at the head of the ready queue, or <code><phrase role="keyword">nullptr</phrase></code> if the queue is empty. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> <varlistentry> <term>Note:</term> <listitem> <para> Placing ready fibers onto the tail of the sahred queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_work_stealing_has_ready_fibers_bridgehead"> <phrase id="numa_work_stealing_has_ready_fibers"/> <link linkend="numa_work_stealing_has_ready_fibers">Member function <code>has_ready_fibers</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Returns:</term> <listitem> <para> <code><phrase role="keyword">true</phrase></code> if scheduler has fibers ready to run. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_work_stealing_suspend_until_bridgehead"> <phrase id="numa_work_stealing_suspend_until"/> <link linkend="numa_work_stealing_suspend_until">Member function <code>suspend_until</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">abs_time</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Informs <code><phrase role="identifier">work_stealing</phrase></code> that no ready fiber will be available until time-point <code><phrase role="identifier">abs_time</phrase></code>. This implementation blocks in <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">wait_until</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> <para> <bridgehead renderas="sect4" id="numa_work_stealing_notify_bridgehead"> <phrase id="numa_work_stealing_notify"/> <link linkend="numa_work_stealing_notify">Member function <code>notify</code>()</link> </bridgehead> </para> <programlisting><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Wake up a pending call to <link linkend="work_stealing_suspend_until"><code>work_stealing::suspend_until()</code></link>, some fibers might be ready. This implementation wakes <code><phrase role="identifier">suspend_until</phrase><phrase role="special">()</phrase></code> via <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase><phrase role="special">::</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">()</phrase></code></ulink>. </para> </listitem> </varlistentry> <varlistentry> <term>Throws:</term> <listitem> <para> Nothing. </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.gpu_computing"> <title><link linkend="fiber.gpu_computing">GPU computing</link></title> <section id="fiber.gpu_computing.cuda"> <title><anchor id="cuda"/><link linkend="fiber.gpu_computing.cuda">CUDA</link></title> <para> <ulink url="http://developer.nvidia.com/cuda-zone/">CUDA (Compute Unified Device Architecture)</ulink> is a platform for parallel computing on NVIDIA GPUs. The application programming interface of CUDA gives access to GPU's instruction set and computation resources (Execution of compute kernels). </para> <bridgehead renderas="sect4" id="fiber.gpu_computing.cuda.h0"> <phrase id="fiber.gpu_computing.cuda.synchronization_with_cuda_streams"/><link linkend="fiber.gpu_computing.cuda.synchronization_with_cuda_streams">Synchronization with CUDA streams</link> </bridgehead> <para> CUDA operation such as compute kernels or memory transfer (between host and device) can be grouped/queued by CUDA streams. are executed on the GPUs. Boost.Fiber enables a fiber to sleep (suspend) till a CUDA stream has completed its operations. This enables applications to run other fibers on the CPU without the need to spawn an additional OS-threads. And resume the fiber when the CUDA streams has finished. </para> <programlisting><phrase role="identifier">__global__</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">kernel</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">a</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">c</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx</phrase> <phrase role="special">=</phrase> <phrase role="identifier">threadIdx</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="identifier">blockIdx</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase> <phrase role="special">*</phrase> <phrase role="identifier">blockDim</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">idx</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx1</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">idx</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">%</phrase> <phrase role="number">256</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx2</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">idx</phrase> <phrase role="special">+</phrase> <phrase role="number">2</phrase><phrase role="special">)</phrase> <phrase role="special">%</phrase> <phrase role="number">256</phrase><phrase role="special">;</phrase> <phrase role="keyword">float</phrase> <phrase role="identifier">as</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx2</phrase><phrase role="special">])</phrase> <phrase role="special">/</phrase> <phrase role="number">3.0f</phrase><phrase role="special">;</phrase> <phrase role="keyword">float</phrase> <phrase role="identifier">bs</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx2</phrase><phrase role="special">])</phrase> <phrase role="special">/</phrase> <phrase role="number">3.0f</phrase><phrase role="special">;</phrase> <phrase role="identifier">c</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">as</phrase> <phrase role="special">+</phrase> <phrase role="identifier">bs</phrase><phrase role="special">)</phrase> <phrase role="special">/</phrase> <phrase role="number">2</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">done</phrase><phrase role="special">]{</phrase> <phrase role="identifier">cudaStream_t</phrase> <phrase role="identifier">stream</phrase><phrase role="special">;</phrase> <phrase role="identifier">cudaStreamCreate</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="number">1024</phrase> <phrase role="special">*</phrase> <phrase role="number">1024</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">=</phrase> <phrase role="number">20</phrase> <phrase role="special">*</phrase> <phrase role="identifier">size</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">;</phrase> <phrase role="identifier">cudaHostAlloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaHostAllocDefault</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaHostAlloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaHostAllocDefault</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaHostAlloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaHostAllocDefault</phrase><phrase role="special">);</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">;</phrase> <phrase role="identifier">cudaMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">cudaMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">cudaMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">minstd_rand</phrase> <phrase role="identifier">generator</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uniform_int_distribution</phrase><phrase role="special">&lt;&gt;</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">,</phrase> <phrase role="number">6</phrase><phrase role="special">);</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">full_size</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase> <phrase role="identifier">generator</phrase><phrase role="special">);</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase> <phrase role="identifier">generator</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">full_size</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">cudaMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">host_a</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaMemcpyHostToDevice</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">host_b</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaMemcpyHostToDevice</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="identifier">kernel</phrase><phrase role="special">&lt;&lt;&lt;</phrase> <phrase role="identifier">size</phrase> <phrase role="special">/</phrase> <phrase role="number">256</phrase><phrase role="special">,</phrase> <phrase role="number">256</phrase><phrase role="special">,</phrase> <phrase role="number">0</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase> <phrase role="special">&gt;&gt;&gt;(</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_c</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">cudaMemcpyDeviceToHost</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">cuda</phrase><phrase role="special">::</phrase><phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="comment">// suspend fiber till CUDA stream has finished</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase> <phrase role="special">==</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">&lt;</phrase> <phrase role="number">0</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="identifier">cudaSuccess</phrase> <phrase role="special">==</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">&lt;</phrase> <phrase role="number">1</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: GPU computation finished&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">cudaFreeHost</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaFreeHost</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaFreeHost</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">cudaStreamDestroy</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">f</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.gpu_computing.cuda.h1"> <phrase id="fiber.gpu_computing.cuda.synopsis"/><link linkend="fiber.gpu_computing.cuda.synopsis">Synopsis</link> </bridgehead> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">cuda</phrase><phrase role="special">/</phrase><phrase role="identifier">waitfor</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">cuda</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">cudaStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">cudaError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">cudaStream_t</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">cudaStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">cudaError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">cudaStream_t</phrase> <phrase role="special">...</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="cuda_waitfor_bridgehead"> <phrase id="cuda_waitfor"/> <link linkend="cuda_waitfor">Non-member function <code>cuda::waitfor()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">cuda</phrase><phrase role="special">/</phrase><phrase role="identifier">waitfor</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">cuda</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">cudaStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">cudaError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">cudaStream_t</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">cudaStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">cudaError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">cudaStream_t</phrase> <phrase role="special">...</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Suspends active fiber till CUDA stream has finished its operations. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> tuple of stream reference and the CUDA stream status </para> </listitem> </varlistentry> </variablelist> </section> <section id="fiber.gpu_computing.hip"> <title><anchor id="hip"/><link linkend="fiber.gpu_computing.hip">ROCm/HIP</link></title> <para> <ulink url="http://github.com/ROCm-Developer-Tools/HIP/tree/roc-1.6.0/">HIP</ulink> is part of the <ulink url="http://rocm.github.io/">ROC (Radeon Open Compute)</ulink> platform for parallel computing on AMD and NVIDIA GPUs. The application programming interface of HIP gives access to GPU's instruction set and computation resources (Execution of compute kernels). </para> <bridgehead renderas="sect4" id="fiber.gpu_computing.hip.h0"> <phrase id="fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams"/><link linkend="fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams">Synchronization with ROCm/HIP streams</link> </bridgehead> <para> HIP operation such as compute kernels or memory transfer (between host and device) can be grouped/queued by HIP streams. are executed on the GPUs. Boost.Fiber enables a fiber to sleep (suspend) till a HIP stream has completed its operations. This enables applications to run other fibers on the CPU without the need to spawn an additional OS-threads. And resume the fiber when the HIP streams has finished. </para> <programlisting><phrase role="identifier">__global__</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">kernel</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">a</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">c</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx</phrase> <phrase role="special">=</phrase> <phrase role="identifier">threadIdx</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="identifier">blockIdx</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase> <phrase role="special">*</phrase> <phrase role="identifier">blockDim</phrase><phrase role="special">.</phrase><phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">idx</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx1</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">idx</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">%</phrase> <phrase role="number">256</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">idx2</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">idx</phrase> <phrase role="special">+</phrase> <phrase role="number">2</phrase><phrase role="special">)</phrase> <phrase role="special">%</phrase> <phrase role="number">256</phrase><phrase role="special">;</phrase> <phrase role="keyword">float</phrase> <phrase role="identifier">as</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="identifier">idx2</phrase><phrase role="special">])</phrase> <phrase role="special">/</phrase> <phrase role="number">3.0f</phrase><phrase role="special">;</phrase> <phrase role="keyword">float</phrase> <phrase role="identifier">bs</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">b</phrase><phrase role="special">[</phrase><phrase role="identifier">idx2</phrase><phrase role="special">])</phrase> <phrase role="special">/</phrase> <phrase role="number">3.0f</phrase><phrase role="special">;</phrase> <phrase role="identifier">c</phrase><phrase role="special">[</phrase><phrase role="identifier">idx</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">as</phrase> <phrase role="special">+</phrase> <phrase role="identifier">bs</phrase><phrase role="special">)</phrase> <phrase role="special">/</phrase> <phrase role="number">2</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">done</phrase><phrase role="special">]{</phrase> <phrase role="identifier">hipStream_t</phrase> <phrase role="identifier">stream</phrase><phrase role="special">;</phrase> <phrase role="identifier">hipStreamCreate</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="number">1024</phrase> <phrase role="special">*</phrase> <phrase role="number">1024</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">=</phrase> <phrase role="number">20</phrase> <phrase role="special">*</phrase> <phrase role="identifier">size</phrase><phrase role="special">;</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">;</phrase> <phrase role="identifier">hipHostMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipHostMallocDefault</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipHostMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipHostMallocDefault</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipHostMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">full_size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipHostMallocDefault</phrase><phrase role="special">);</phrase> <phrase role="keyword">int</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">;</phrase> <phrase role="identifier">hipMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">hipMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">hipMalloc</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">minstd_rand</phrase> <phrase role="identifier">generator</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uniform_int_distribution</phrase><phrase role="special">&lt;&gt;</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">,</phrase> <phrase role="number">6</phrase><phrase role="special">);</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">full_size</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase> <phrase role="identifier">generator</phrase><phrase role="special">);</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">distribution</phrase><phrase role="special">(</phrase> <phrase role="identifier">generator</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">full_size</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">size</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">hipMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">host_a</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipMemcpyHostToDevice</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">host_b</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipMemcpyHostToDevice</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipLaunchKernel</phrase><phrase role="special">(</phrase><phrase role="identifier">kernel</phrase><phrase role="special">,</phrase> <phrase role="identifier">dim3</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase> <phrase role="special">/</phrase> <phrase role="number">256</phrase><phrase role="special">),</phrase> <phrase role="identifier">dim3</phrase><phrase role="special">(</phrase><phrase role="number">256</phrase><phrase role="special">),</phrase> <phrase role="number">0</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipMemcpyAsync</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_c</phrase> <phrase role="special">+</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase> <phrase role="special">*</phrase> <phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase><phrase role="special">),</phrase> <phrase role="identifier">hipMemcpyDeviceToHost</phrase><phrase role="special">,</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">hip</phrase><phrase role="special">::</phrase><phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="comment">// suspend fiber till HIP stream has finished</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase> <phrase role="special">==</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">&lt;</phrase> <phrase role="number">0</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">BOOST_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="identifier">hipSuccess</phrase> <phrase role="special">==</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">get</phrase><phrase role="special">&lt;</phrase> <phrase role="number">1</phrase> <phrase role="special">&gt;(</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: GPU computation finished&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase> <phrase role="identifier">hipHostFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_a</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipHostFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_b</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipHostFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">host_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_a</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_b</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipFree</phrase><phrase role="special">(</phrase> <phrase role="identifier">dev_c</phrase><phrase role="special">);</phrase> <phrase role="identifier">hipStreamDestroy</phrase><phrase role="special">(</phrase> <phrase role="identifier">stream</phrase><phrase role="special">);</phrase> <phrase role="special">});</phrase> <phrase role="identifier">f</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <bridgehead renderas="sect4" id="fiber.gpu_computing.hip.h1"> <phrase id="fiber.gpu_computing.hip.synopsis"/><link linkend="fiber.gpu_computing.hip.synopsis">Synopsis</link> </bridgehead> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">hip</phrase><phrase role="special">/</phrase><phrase role="identifier">waitfor</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">hip</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">hipStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">hipError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">hipStream_t</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">hipStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">hipError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">hipStream_t</phrase> <phrase role="special">...</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> </programlisting> <para> <bridgehead renderas="sect4" id="hip_waitfor_bridgehead"> <phrase id="hip_waitfor"/> <link linkend="hip_waitfor">Non-member function <code>hip::waitfor()</code></link> </bridgehead> </para> <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">/</phrase><phrase role="identifier">hip</phrase><phrase role="special">/</phrase><phrase role="identifier">waitfor</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">fibers</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">hip</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">hipStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">hipError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">hipStream_t</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">tuple</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">hipStream_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">hipError_t</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">waitfor_all</phrase><phrase role="special">(</phrase> <phrase role="identifier">hipStream_t</phrase> <phrase role="special">...</phrase> <phrase role="identifier">st</phrase><phrase role="special">);</phrase> <phrase role="special">}}}</phrase> </programlisting> <variablelist> <title></title> <varlistentry> <term>Effects:</term> <listitem> <para> Suspends active fiber till HIP stream has finished its operations. </para> </listitem> </varlistentry> <varlistentry> <term>Returns:</term> <listitem> <para> tuple of stream reference and the HIP stream status </para> </listitem> </varlistentry> </variablelist> </section> </section> <section id="fiber.worker"> <title><anchor id="worker"/><link linkend="fiber.worker">Running with worker threads</link></title> <bridgehead renderas="sect3" id="fiber.worker.h0"> <phrase id="fiber.worker.keep_workers_running"/><link linkend="fiber.worker.keep_workers_running">Keep workers running</link> </bridgehead> <para> If a worker thread is used but no fiber is created or parts of the framework (like <link linkend="this_fiber_yield"><code>this_fiber::yield()</code></link>) are touched, then no fiber scheduler is instantiated. </para> <programlisting><phrase role="keyword">auto</phrase> <phrase role="identifier">worker</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="special">[]{</phrase> <phrase role="comment">// fiber scheduler not instantiated</phrase> <phrase role="special">});</phrase> <phrase role="identifier">worker</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> If <emphasis>use_scheduling_algorithm&lt;&gt;()</emphasis> is invoked, the fiber scheduler is created. If the worker thread simply returns, destroys the scheduler and terminates. </para> <programlisting><phrase role="keyword">auto</phrase> <phrase role="identifier">worker</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="special">[]{</phrase> <phrase role="comment">// fiber scheduler created</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">my_fiber_scheduler</phrase><phrase role="special">&gt;();</phrase> <phrase role="special">});</phrase> <phrase role="identifier">worker</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> In order to keep the worker thread running, the fiber associated with the thread stack (which is called <quote>main</quote> fiber) is blocked. For instance the <quote>main</quote> fiber might wait on a <link linkend="class_condition_variable"><code>condition_variable</code></link>. For a gracefully shutdown <link linkend="class_condition_variable"><code>condition_variable</code></link> is signalled and the <quote>main</quote> fiber returns. The scheduler gets destructed if all fibers of the worker thread have been terminated. </para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable_any</phrase> <phrase role="identifier">cv</phrase><phrase role="special">;</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">worker</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="special">[&amp;</phrase><phrase role="identifier">mtx</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">cv</phrase><phrase role="special">]{</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="comment">// suspend till signalled</phrase> <phrase role="identifier">cv</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase><phrase role="identifier">mtx</phrase><phrase role="special">);</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase> <phrase role="comment">// signal termination</phrase> <phrase role="identifier">cv</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">();</phrase> <phrase role="identifier">worker</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <bridgehead renderas="sect3" id="fiber.worker.h1"> <phrase id="fiber.worker.processing_tasks"/><link linkend="fiber.worker.processing_tasks">Processing tasks</link> </bridgehead> <para> Tasks can be transferred via channels. The worker thread runs a pool of fibers that dequeue and executed tasks from the channel. The termination is signalled via closing the channel. </para> <programlisting><phrase role="keyword">using</phrase> <phrase role="identifier">task</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">()&gt;;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">buffered_channel</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">task</phrase><phrase role="special">&gt;</phrase> <phrase role="identifier">ch</phrase><phrase role="special">{</phrase><phrase role="number">1024</phrase><phrase role="special">};</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">worker</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="special">[&amp;</phrase><phrase role="identifier">ch</phrase><phrase role="special">]{</phrase> <phrase role="comment">// create pool of fibers</phrase> <phrase role="keyword">for</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase><phrase role="special">&lt;</phrase><phrase role="number">10</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">{</phrase> <phrase role="special">[&amp;</phrase><phrase role="identifier">ch</phrase><phrase role="special">]{</phrase> <phrase role="identifier">task</phrase> <phrase role="identifier">tsk</phrase><phrase role="special">;</phrase> <phrase role="comment">// dequeue and process tasks</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">closed</phrase><phrase role="special">!=</phrase><phrase role="identifier">ch</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">tsk</phrase><phrase role="special">)){</phrase> <phrase role="identifier">tsk</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">}}.</phrase><phrase role="identifier">detach</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="identifier">task</phrase> <phrase role="identifier">tsk</phrase><phrase role="special">;</phrase> <phrase role="comment">// dequeue and process tasks</phrase> <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">channel_op_status</phrase><phrase role="special">::</phrase><phrase role="identifier">closed</phrase><phrase role="special">!=</phrase><phrase role="identifier">ch</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">tsk</phrase><phrase role="special">)){</phrase> <phrase role="identifier">tsk</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">});</phrase> <phrase role="comment">// feed channel with tasks</phrase> <phrase role="identifier">ch</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">([]{</phrase> <phrase role="special">...</phrase> <phrase role="special">});</phrase> <phrase role="special">...</phrase> <phrase role="comment">// signal termination</phrase> <phrase role="identifier">ch</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="identifier">worker</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <para> An alternative is to use a work-stealing scheduler. This kind of scheduling algorithm a worker thread steals fibers from the ready-queue of other worker threads if its own ready-queue is empty. </para> <note> <para> Wait till all worker threads have registered the work-stealing scheduling algorithm. </para> </note> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable_any</phrase> <phrase role="identifier">cv</phrase><phrase role="special">;</phrase> <phrase role="comment">// start wotrker-thread first</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">worker</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase><phrase role="special">(</phrase> <phrase role="special">[&amp;</phrase><phrase role="identifier">mtx</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">cv</phrase><phrase role="special">]{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase><phrase role="special">&gt;(</phrase><phrase role="number">2</phrase><phrase role="special">);</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">.</phrase><phrase role="identifier">lock</phrase><phrase role="special">();</phrase> <phrase role="comment">// suspend main-fiber from the worker thread</phrase> <phrase role="identifier">cv</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase><phrase role="identifier">mtx</phrase><phrase role="special">);</phrase> <phrase role="identifier">mtx</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">work_stealing</phrase><phrase role="special">&gt;(</phrase><phrase role="number">2</phrase><phrase role="special">);</phrase> <phrase role="comment">// create fibers with tasks</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">{[]{</phrase> <phrase role="special">...</phrase> <phrase role="special">}};</phrase> <phrase role="special">...</phrase> <phrase role="comment">// signal termination</phrase> <phrase role="identifier">cv</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">();</phrase> <phrase role="identifier">worker</phrase><phrase role="special">.</phrase><phrase role="identifier">join</phrase><phrase role="special">();</phrase> </programlisting> <important> <para> Because the TIB (thread information block on Windows) is not fully described in the MSDN, it might be possible that not all required TIB-parts are swapped. Using WinFiber implementation might be an alternative (see documentation about <ulink url="http://www.boost.org/doc/libs/1_65_1/libs/context/doc/html/context/cc/implementations__fcontext_t__ucontext_t_and_winfiber.html"><emphasis>implementations fcontext_t, ucontext_t and WinFiber of boost.context</emphasis></ulink>). </para> </important> </section> <section id="fiber.performance"> <title><link linkend="fiber.performance">Performance</link></title> <para> Performance measurements were taken using <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">highresolution_clock</phrase></code>, with overhead corrections. The code was compiled with gcc-6.3.1, using build options: variant = release, optimization = speed. Tests were executed on dual Intel XEON E5 2620v4 2.2GHz, 16C/32T, 64GB RAM, running Linux (x86_64). </para> <para> Measurements headed 1C/1T were run in a single-threaded process. </para> <para> The <ulink url="https://github.com/atemerev/skynet">microbenchmark <emphasis>syknet</emphasis></ulink> from Alexander Temerev was ported and used for performance measurements. At the root the test spawns 10 threads-of-execution (ToE), e.g. actor/goroutine/fiber etc.. Each spawned ToE spawns additional 10 ToEs ... until <emphasis role="bold">1,000,000</emphasis> ToEs are created. ToEs return back their ordinal numbers (0 ... 999,999), which are summed on the previous level and sent back upstream, until reaching the root. The test was run 10-20 times, producing a range of values for each measurement. </para> <table frame="all" id="fiber.performance.time_per_actor_erlang_process_goroutine__other_languages___average_over_1_000_000_"> <title>time per actor/erlang process/goroutine (other languages) (average over 1,000,000)</title> <tgroup cols="3"> <thead> <row> <entry> <para> Haskell | stack-1.4.0/ghc-8.0.1 </para> </entry> <entry> <para> Go | go1.8.1 </para> </entry> <entry> <para> Erlang | erts-8.3 </para> </entry> </row> </thead> <tbody> <row> <entry> <para> 0.05 &#xb5;s - 0.06 &#xb5;s </para> </entry> <entry> <para> 0.42 &#xb5;s - 0.49 &#xb5;s </para> </entry> <entry> <para> 0.63 &#xb5;s - 0.73 &#xb5;s </para> </entry> </row> </tbody> </tgroup> </table> <para> Pthreads are created with a stack size of 8kB while <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase></code>'s use the system default (1MB - 2MB). The microbenchmark could <emphasis role="bold">not</emphasis> be <emphasis role="bold">run</emphasis> with 1,000,000 threads because of <emphasis role="bold">resource exhaustion</emphasis> (pthread and std::thread). Instead the test runs only at <emphasis role="bold">10,000</emphasis> threads. </para> <table frame="all" id="fiber.performance.time_per_thread__average_over_10_000___unable_to_spawn_1_000_000_threads_"> <title>time per thread (average over 10,000 - unable to spawn 1,000,000 threads)</title> <tgroup cols="3"> <thead> <row> <entry> <para> pthread </para> </entry> <entry> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">thread</phrase></code> </para> </entry> <entry> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">async</phrase></code> </para> </entry> </row> </thead> <tbody> <row> <entry> <para> 54 &#xb5;s - 73 &#xb5;s </para> </entry> <entry> <para> 52 &#xb5;s - 73 &#xb5;s </para> </entry> <entry> <para> 106 &#xb5;s - 122 &#xb5;s </para> </entry> </row> </tbody> </tgroup> </table> <para> The test utilizes 16 cores with Symmetric MultiThreading enabled (32 logical CPUs). The fiber stacks are allocated by <link linkend="class_fixedsize_stack"><code>fixedsize_stack</code></link>. </para> <para> As the benchmark shows, the memory allocation algorithm is significant for performance in a multithreaded environment. The tests use glibc&#8217;s memory allocation algorithm (based on ptmalloc2) as well as Google&#8217;s <ulink url="http://goog-perftools.sourceforge.net/doc/tcmalloc.html">TCmalloc</ulink> (via linkflags=&quot;-ltcmalloc&quot;).<footnote id="fiber.performance.f0"> <para> Tais B. Ferreira, Rivalino Matias, Autran Macedo, Lucio B. Araujo <quote>An Experimental Study on Memory Allocators in Multicore and Multithreaded Applications</quote>, PDCAT &#8217;11 Proceedings of the 2011 12th International Conference on Parallel and Distributed Computing, Applications and Technologies, pages 92-98 </para> </footnote> </para> <para> In the <link linkend="class_work_stealing"><code>work_stealing</code></link> scheduling algorithm, each thread has its own local queue. Fibers that are ready to run are pushed to and popped from the local queue. If the queue runs out of ready fibers, fibers are stolen from the local queues of other participating threads. </para> <table frame="all" id="fiber.performance.time_per_fiber__average_over_1_000_000_"> <title>time per fiber (average over 1.000.000)</title> <tgroup cols="2"> <thead> <row> <entry> <para> fiber (16C/32T, work stealing, tcmalloc) </para> </entry> <entry> <para> fiber (1C/1T, round robin, tcmalloc) </para> </entry> </row> </thead> <tbody> <row> <entry> <para> 0.05 &#xb5;s - 0.09 &#xb5;s </para> </entry> <entry> <para> 1.69 &#xb5;s - 1.79 &#xb5;s </para> </entry> </row> </tbody> </tgroup> </table> </section> <section id="fiber.tuning"> <title><anchor id="tuning"/><link linkend="fiber.tuning">Tuning</link></title> <bridgehead renderas="sect3" id="fiber.tuning.h0"> <phrase id="fiber.tuning.disable_synchronization"/><link linkend="fiber.tuning.disable_synchronization">Disable synchronization</link> </bridgehead> <para> With <link linkend="cross_thread_sync"><code><phrase role="identifier">BOOST_FIBERS_NO_ATOMICS</phrase></code></link> defined at the compiler&#8217;s command line, synchronization between fibers (in different threads) is disabled. This is acceptable if the application is single threaded and/or fibers are not synchronized between threads. </para> <bridgehead renderas="sect3" id="fiber.tuning.h1"> <phrase id="fiber.tuning.memory_allocation"/><link linkend="fiber.tuning.memory_allocation">Memory allocation</link> </bridgehead> <para> Memory allocation algorithm is significant for performance in a multithreaded environment, especially for <emphasis role="bold">Boost.Fiber</emphasis> where fiber stacks are allocated on the heap. The default user-level memory allocator (UMA) of glibc is ptmalloc2 but it can be replaced by another UMA that fit better for the concret work-load For instance Google&#8217;s <ulink url="http://goog-perftools.sourceforge.net/doc/tcmalloc.html">TCmalloc</ulink> enables a better performance at the <emphasis>skynet</emphasis> microbenchmark than glibc&#8217;s default memory allocator. </para> <bridgehead renderas="sect3" id="fiber.tuning.h2"> <phrase id="fiber.tuning.scheduling_strategies"/><link linkend="fiber.tuning.scheduling_strategies">Scheduling strategies</link> </bridgehead> <para> The fibers in a thread are coordinated by a fiber manager. Fibers trade control cooperatively, rather than preemptively. Depending on the work-load several strategies of scheduling the fibers are possible <footnote id="fiber.tuning.f0"> <para> 1024cores.net: <ulink url="http://www.1024cores.net/home/scalable-architecture/task-scheduling-strategies">Task Scheduling Strategies</ulink> </para> </footnote> that can be implmented on behalf of <link linkend="class_algorithm"><code>algorithm</code></link>. </para> <itemizedlist> <listitem> <simpara> work-stealing: ready fibers are hold in a local queue, when the fiber-scheduler's local queue runs out of ready fibers, it randomly selects another fiber-scheduler and tries to steal a ready fiber from the victim (implemented in <link linkend="class_work_stealing"><code>work_stealing</code></link> and <link linkend="class_numa_work_stealing"><code>numa::work_stealing</code></link>) </simpara> </listitem> <listitem> <simpara> work-requesting: ready fibers are hold in a local queue, when the fiber-scheduler's local queue runs out of ready fibers, it randomly selects another fiber-scheduler and requests for a ready fibers, the victim fiber-scheduler sends a ready-fiber back </simpara> </listitem> <listitem> <simpara> work-sharing: ready fibers are hold in a global queue, fiber-scheduler concurrently push and pop ready fibers to/from the global queue (implemented in <link linkend="class_shared_work"><code>shared_work</code></link>) </simpara> </listitem> <listitem> <simpara> work-distribution: fibers that became ready are proactivly distributed to idle fiber-schedulers or fiber-schedulers with low load </simpara> </listitem> <listitem> <simpara> work-balancing: a dedicated (helper) fiber-scheduler periodically collects informations about all fiber-scheduler running in other threads and re-distributes ready fibers among them </simpara> </listitem> </itemizedlist> <bridgehead renderas="sect3" id="fiber.tuning.h3"> <phrase id="fiber.tuning.ttas_locks"/><link linkend="fiber.tuning.ttas_locks">TTAS locks</link> </bridgehead> <para> Boost.Fiber uses internally spinlocks to protect critical regions if fibers running on different threads interact. Spinlocks are implemented as TTAS (test-test-and-set) locks, i.e. the spinlock tests the lock before calling an atomic exchange. This strategy helps to reduce the cache line invalidations triggered by acquiring/releasing the lock. </para> <bridgehead renderas="sect3" id="fiber.tuning.h4"> <phrase id="fiber.tuning.spin_wait_loop"/><link linkend="fiber.tuning.spin_wait_loop">Spin-wait loop</link> </bridgehead> <para> A lock is considered under contention, if a thread repeatedly fails to acquire the lock because some other thread was faster. Waiting for a short time lets other threads finish before trying to enter the critical section again. While busy waiting on the lock, relaxing the CPU (via pause/yield mnemonic) gives the CPU a hint that the code is in a spin-wait loop. </para> <itemizedlist> <listitem> <simpara> prevents expensive pipeline flushes (speculatively executed load and compare instructions are not pushed to pipeline) </simpara> </listitem> <listitem> <simpara> another hardware thread (simultaneous multithreading) can get time slice </simpara> </listitem> <listitem> <simpara> it does delay a few CPU cycles, but this is necessary to prevent starvation </simpara> </listitem> </itemizedlist> <para> It is obvious that this strategy is useless on single core systems because the lock can only released if the thread gives up its time slice in order to let other threads run. The macro BOOST_FIBERS_SPIN_SINGLE_CORE replaces the CPU hints (pause/yield mnemonic) by informing the operating system (via <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread_yield</phrase><phrase role="special">()</phrase></code>) that the thread gives up its time slice and the operating system switches to another thread. </para> <bridgehead renderas="sect3" id="fiber.tuning.h5"> <phrase id="fiber.tuning.exponential_back_off"/><link linkend="fiber.tuning.exponential_back_off">Exponential back-off</link> </bridgehead> <para> The macro BOOST_FIBERS_RETRY_THRESHOLD determines how many times the CPU iterates in the spin-wait loop before yielding the thread or blocking in futex-wait. The spinlock tracks how many times the thread failed to acquire the lock. The higher the contention, the longer the thread should back-off. A <quote>Binary Exponential Backoff</quote> algorithm together with a randomized contention window is utilized for this purpose. BOOST_FIBERS_CONTENTION_WINDOW_THRESHOLD determines the upper limit of the contention window (expressed as the exponent for basis of two). </para> <bridgehead renderas="sect3" id="fiber.tuning.h6"> <phrase id="fiber.tuning.speculative_execution__hardware_transactional_memory_"/><link linkend="fiber.tuning.speculative_execution__hardware_transactional_memory_">Speculative execution (hardware transactional memory)</link> </bridgehead> <para> Boost.Fiber uses spinlocks to protect critical regions that can be used together with transactional memory (see section <link linkend="speculation">Speculative execution</link>). </para> <note> <para> TXS is enabled if property <code><phrase role="identifier">htm</phrase><phrase role="special">=</phrase><phrase role="identifier">tsx</phrase></code> is specified at b2 command-line and <code><phrase role="identifier">BOOST_USE_TSX</phrase></code> is applied to the compiler. </para> </note> <note> <para> A TSX-transaction will be aborted if the floating point state is modified inside a critical region. As a consequence floating point operations, e.g. tore/load of floating point related registers during a fiber (context) switch are disabled. </para> </note> <bridgehead renderas="sect3" id="fiber.tuning.h7"> <phrase id="fiber.tuning.numa_systems"/><link linkend="fiber.tuning.numa_systems">NUMA systems</link> </bridgehead> <para> Modern multi-socket systems are usually designed as <link linkend="numa">NUMA systems</link>. A suitable fiber scheduler like <link linkend="class_numa_work_stealing"><code>numa::work_stealing</code></link> reduces remote memory access (latence). </para> <bridgehead renderas="sect3" id="fiber.tuning.h8"> <phrase id="fiber.tuning.parameters"/><link linkend="fiber.tuning.parameters">Parameters</link> </bridgehead> <table frame="all" id="fiber.tuning.parameters_that_migh_be_defiend_at_compiler_s_command_line"> <title>Parameters that migh be defiend at compiler's command line</title> <tgroup cols="3"> <thead> <row> <entry> <para> Parameter </para> </entry> <entry> <para> Default value </para> </entry> <entry> <para> Effect on Boost.Fiber </para> </entry> </row> </thead> <tbody> <row> <entry> <para> BOOST_FIBERS_NO_ATOMICS </para> </entry> <entry> <para> - </para> </entry> <entry> <para> no multithreading support, all atomics removed, no synchronization between fibers running in different threads </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_STD_MUTEX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase></code> used inside spinlock </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS </para> </entry> <entry> <para> + </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, adaptive retries while busy waiting </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_FUTEX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, suspend on futex after certain number of retries </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries, suspend on futex certain amount of retries </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS + BOOST_USE_TSX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap and speculative execution (Intel TSX required) </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE + BOOST_USE_TSX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, adaptive retries while busy waiting and speculative execution (Intel TSX required) </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_FUTEX + BOOST_USE_TSX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, suspend on futex after certain number of retries and speculative execution (Intel TSX required) </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX + BOOST_USE_TSX </para> </entry> <entry> <para> - </para> </entry> <entry> <para> spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries, suspend on futex certain amount of retries and speculative execution (Intel TSX required) </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPIN_SINGLE_CORE </para> </entry> <entry> <para> - </para> </entry> <entry> <para> on single core machines with multiple threads, yield thread (<code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code>) after collisions </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_RETRY_THRESHOLD </para> </entry> <entry> <para> 64 </para> </entry> <entry> <para> max number of retries while busy spinning, the use fallback </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_CONTENTION_WINDOW_THRESHOLD </para> </entry> <entry> <para> 16 </para> </entry> <entry> <para> max size of collisions window, expressed as exponent for the basis of two </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPIN_BEFORE_SLEEP0 </para> </entry> <entry> <para> 32 </para> </entry> <entry> <para> max number of retries that relax the processor before the thread sleeps for 0s </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPIN_BEFORE_YIELD </para> </entry> <entry> <para> 64 </para> </entry> <entry> <para> max number of retries where the thread sleeps for 0s before yield thread (<code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code>) </para> </entry> </row> </tbody> </tgroup> </table> </section> <section id="fiber.custom"> <title><anchor id="custom"/><link linkend="fiber.custom">Customization</link></title> <bridgehead renderas="sect3" id="fiber.custom.h0"> <phrase id="fiber.custom.overview"/><link linkend="fiber.custom.overview">Overview</link> </bridgehead> <para> As noted in the <link linkend="scheduling">Scheduling</link> section, by default <emphasis role="bold">Boost.Fiber</emphasis> uses its own <link linkend="class_round_robin"><code>round_robin</code></link> scheduler for each thread. To control the way <emphasis role="bold">Boost.Fiber</emphasis> schedules ready fibers on a particular thread, in general you must follow several steps. This section discusses those steps, whereas <link linkend="scheduling">Scheduling</link> serves as a reference for the classes involved. </para> <para> The library's fiber manager keeps track of suspended (blocked) fibers. Only when a fiber becomes ready to run is it passed to the scheduler. Of course, if there are fewer than two ready fibers, the scheduler's job is trivial. Only when there are two or more ready fibers does the particular scheduler implementation start to influence the overall sequence of fiber execution. </para> <para> In this section we illustrate a simple custom scheduler that honors an integer fiber priority. We will implement it such that a fiber with higher priority is preferred over a fiber with lower priority. Any fibers with equal priority values are serviced on a round-robin basis. </para> <para> The full source code for the examples below is found in <ulink url="../../examples/priority.cpp">priority.cpp</ulink>. </para> <bridgehead renderas="sect3" id="fiber.custom.h1"> <phrase id="fiber.custom.custom_property_class"/><link linkend="fiber.custom.custom_property_class">Custom Property Class</link> </bridgehead> <para> The first essential point is that we must associate an integer priority with each fiber.<footnote id="fiber.custom.f0"> <para> A previous version of the Fiber library implicitly tracked an int priority for each fiber, even though the default scheduler ignored it. This has been dropped, since the library now supports arbitrary scheduler-specific fiber properties. </para> </footnote> </para> <para> One might suggest deriving a custom <link linkend="class_fiber"><code>fiber</code></link> subclass to store such properties. There are a couple of reasons for the present mechanism. </para> <orderedlist> <listitem> <simpara> <emphasis role="bold">Boost.Fiber</emphasis> provides a number of different ways to launch a fiber. (Consider <link linkend="fibers_async"><code>fibers::async()</code></link>.) Higher-level libraries might introduce additional such wrapper functions. A custom scheduler must associate its custom properties with <emphasis>every</emphasis> fiber in the thread, not only the ones explicitly launched by instantiating a custom <code><phrase role="identifier">fiber</phrase></code> subclass. </simpara> </listitem> <listitem> <simpara> Consider a large existing program that launches fibers in many different places in the code. We discover a need to introduce a custom scheduler for a particular thread. If supporting that scheduler's custom properties required a particular <code><phrase role="identifier">fiber</phrase></code> subclass, we would have to hunt down and modify every place that launches a fiber on that thread. </simpara> </listitem> <listitem> <simpara> The <link linkend="class_fiber"><code>fiber</code></link> class is actually just a handle to internal <link linkend="class_context"><code>context</code></link> data. A subclass of <code><phrase role="identifier">fiber</phrase></code> would not add data to <code><phrase role="identifier">context</phrase></code>. </simpara> </listitem> </orderedlist> <para> The present mechanism allows you to <quote>drop in</quote> a custom scheduler with its attendant custom properties <emphasis>without</emphasis> altering the rest of your application. </para> <para> Instead of deriving a custom scheduler fiber properties subclass from <link linkend="class_fiber"><code>fiber</code></link>, you must instead derive it from <link linkend="class_fiber_properties"><code>fiber_properties</code></link>. </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber_properties</phrase> <phrase role="special">{</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">priority_props</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">):</phrase> <phrase role="identifier">fiber_properties</phrase><phrase role="special">(</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">),</phrase> <co id="fiber.custom.c0" linkends="fiber.custom.c1" /> <phrase role="identifier">priority_</phrase><phrase role="special">(</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">get_priority</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">priority_</phrase><phrase role="special">;</phrase> <co id="fiber.custom.c2" linkends="fiber.custom.c3" /> <phrase role="special">}</phrase> <phrase role="comment">// Call this method to alter priority, because we must notify</phrase> <phrase role="comment">// priority_scheduler of any change.</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set_priority</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">p</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <co id="fiber.custom.c4" linkends="fiber.custom.c5" /> <phrase role="comment">// Of course, it's only worth reshuffling the queue and all if we're</phrase> <phrase role="comment">// actually changing the priority.</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">p</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">priority_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">priority_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">p</phrase><phrase role="special">;</phrase> <phrase role="identifier">notify</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">// The fiber name of course is solely for purposes of this example</phrase> <phrase role="comment">// program; it has nothing to do with implementing scheduler priority.</phrase> <phrase role="comment">// This is a public data member -- not requiring set/get access methods --</phrase> <phrase role="comment">// because we need not inform the scheduler of any change.</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">name</phrase><phrase role="special">;</phrase> <co id="fiber.custom.c6" linkends="fiber.custom.c7" /> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">priority_</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase> </programlisting> </para> <calloutlist> <callout arearefs="fiber.custom.c0" id="fiber.custom.c1"> <para> Your subclass constructor must accept a <literal><link linkend="class_context"><code>context</code></link>*</literal> and pass it to the <code><phrase role="identifier">fiber_properties</phrase></code> constructor. </para> </callout> <callout arearefs="fiber.custom.c2" id="fiber.custom.c3"> <para> Provide read access methods at your own discretion. </para> </callout> <callout arearefs="fiber.custom.c4" id="fiber.custom.c5"> <para> It's important to call <code><phrase role="identifier">notify</phrase><phrase role="special">()</phrase></code> on any change in a property that can affect the scheduler's behavior. Therefore, such modifications should only be performed through an access method. </para> </callout> <callout arearefs="fiber.custom.c6" id="fiber.custom.c7"> <para> A property that does not affect the scheduler does not need access methods. </para> </callout> </calloutlist> <bridgehead renderas="sect3" id="fiber.custom.h2"> <phrase id="fiber.custom.custom_scheduler_class"/><link linkend="fiber.custom.custom_scheduler_class">Custom Scheduler Class</link> </bridgehead> <para> Now we can derive a custom scheduler from <link linkend="class_algorithm_with_properties"><code>algorithm_with_properties&lt;&gt;</code></link>, specifying our custom property class <code><phrase role="identifier">priority_props</phrase></code> as the template parameter. </para> <para> <programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">priority_scheduler</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">algo</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm_with_properties</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&gt;</phrase> <phrase role="special">{</phrase> <phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">scheduler</phrase><phrase role="special">::</phrase><phrase role="identifier">ready_queue_type</phrase><co id="fiber.custom.c8" linkends="fiber.custom.c9" /> <phrase role="identifier">rqueue_t</phrase><phrase role="special">;</phrase> <phrase role="identifier">rqueue_t</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">{};</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">{};</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">flag_</phrase><phrase role="special">{</phrase> <phrase role="keyword">false</phrase> <phrase role="special">};</phrase> <phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="identifier">priority_scheduler</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">}</phrase> <phrase role="comment">// For a subclass of algorithm_with_properties&lt;&gt;, it's important to</phrase> <phrase role="comment">// override the correct awakened() overload.</phrase> <co id="fiber.custom.c10" linkends="fiber.custom.c11" /><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">,</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">props</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">ctx_priority</phrase> <phrase role="special">=</phrase> <phrase role="identifier">props</phrase><phrase role="special">.</phrase><phrase role="identifier">get_priority</phrase><phrase role="special">();</phrase> <co id="fiber.custom.c12" linkends="fiber.custom.c13" /> <phrase role="comment">// With this scheduler, fibers with higher priority values are</phrase> <phrase role="comment">// preferred over fibers with lower priority values. But fibers with</phrase> <phrase role="comment">// equal priority values are processed in round-robin fashion. So when</phrase> <phrase role="comment">// we're handed a new context*, put it at the end of the fibers</phrase> <phrase role="comment">// with that same priority. In other words: search for the first fiber</phrase> <phrase role="comment">// in the queue with LOWER priority, and insert before that one.</phrase> <phrase role="identifier">rqueue_t</phrase><phrase role="special">::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">i</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">find_if</phrase><phrase role="special">(</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="special">[</phrase><phrase role="identifier">ctx_priority</phrase><phrase role="special">,</phrase><phrase role="keyword">this</phrase><phrase role="special">](</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">c</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">properties</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase><phrase role="identifier">c</phrase> <phrase role="special">).</phrase><phrase role="identifier">get_priority</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;</phrase> <phrase role="identifier">ctx_priority</phrase><phrase role="special">;</phrase> <phrase role="special">}));</phrase> <phrase role="comment">// Now, whether or not we found a fiber with lower priority,</phrase> <phrase role="comment">// insert this new fiber here.</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <co id="fiber.custom.c14" linkends="fiber.custom.c15" /><phrase role="keyword">virtual</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">pick_next</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="comment">// if ready queue is empty, just tell caller</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">nullptr</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">(</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">front</phrase><phrase role="special">()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">pop_front</phrase><phrase role="special">();</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <co id="fiber.custom.c16" linkends="fiber.custom.c17" /><phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">has_ready_fibers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="special">!</phrase> <phrase role="identifier">rqueue_</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <co id="fiber.custom.c18" linkends="fiber.custom.c19" /><phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">property_change</phrase><phrase role="special">(</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase> <phrase role="special">*</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">,</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">props</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Although our priority_props class defines multiple properties, only</phrase> <phrase role="comment">// one of them (priority) actually calls notify() when changed. The</phrase> <phrase role="comment">// point of a property_change() override is to reshuffle the ready</phrase> <phrase role="comment">// queue according to the updated priority value.</phrase> <phrase role="comment">// 'ctx' might not be in our queue at all, if caller is changing the</phrase> <phrase role="comment">// priority of (say) the running fiber. If it's not there, no need to</phrase> <phrase role="comment">// move it: we'll handle it next time it hits awakened().</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">ready_is_linked</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase> <co id="fiber.custom.c20" linkends="fiber.custom.c21" /> <phrase role="keyword">return</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="comment">// Found ctx: unlink it</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">-&gt;</phrase><phrase role="identifier">ready_unlink</phrase><phrase role="special">();</phrase> <phrase role="comment">// Here we know that ctx was in our ready queue, but we've unlinked</phrase> <phrase role="comment">// it. We happen to have a method that will (re-)add a context* to the</phrase> <phrase role="comment">// right place in the ready queue.</phrase> <phrase role="identifier">awakened</phrase><phrase role="special">(</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">,</phrase> <phrase role="identifier">props</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">suspend_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">time_point</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase role="special">::</phrase><phrase role="identifier">steady_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">time_point</phrase><phrase role="special">::</phrase><phrase role="identifier">max</phrase><phrase role="special">)()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">time_point</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">);</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">.</phrase><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="special">[</phrase><phrase role="keyword">this</phrase><phrase role="special">](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">flag_</phrase><phrase role="special">;</phrase> <phrase role="special">});</phrase> <phrase role="identifier">flag_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">);</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">.</phrase><phrase role="identifier">wait_until</phrase><phrase role="special">(</phrase> <phrase role="identifier">lk</phrase><phrase role="special">,</phrase> <phrase role="identifier">time_point</phrase><phrase role="special">,</phrase> <phrase role="special">[</phrase><phrase role="keyword">this</phrase><phrase role="special">](){</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">flag_</phrase><phrase role="special">;</phrase> <phrase role="special">});</phrase> <phrase role="identifier">flag_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">unique_lock</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">(</phrase> <phrase role="identifier">mtx_</phrase><phrase role="special">);</phrase> <phrase role="identifier">flag_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="identifier">lk</phrase><phrase role="special">.</phrase><phrase role="identifier">unlock</phrase><phrase role="special">();</phrase> <phrase role="identifier">cnd_</phrase><phrase role="special">.</phrase><phrase role="identifier">notify_all</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase> <phrase role="special">};</phrase> </programlisting> </para> <calloutlist> <callout arearefs="fiber.custom.c8" id="fiber.custom.c9"> <para> See <link linkend="ready_queue_t">ready_queue_t</link>. </para> </callout> <callout arearefs="fiber.custom.c10" id="fiber.custom.c11"> <para> You must override the <link linkend="algorithm_with_properties_awakened"><code>algorithm_with_properties::awakened()</code></link> method. This is how your scheduler receives notification of a fiber that has become ready to run. </para> </callout> <callout arearefs="fiber.custom.c12" id="fiber.custom.c13"> <para> <code><phrase role="identifier">props</phrase></code> is the instance of priority_props associated with the passed fiber <code><phrase role="identifier">ctx</phrase></code>. </para> </callout> <callout arearefs="fiber.custom.c14" id="fiber.custom.c15"> <para> You must override the <link linkend="algorithm_with_properties_pick_next"><code>algorithm_with_properties::pick_next()</code></link> method. This is how your scheduler actually advises the fiber manager of the next fiber to run. </para> </callout> <callout arearefs="fiber.custom.c16" id="fiber.custom.c17"> <para> You must override <link linkend="algorithm_with_properties_has_ready_fibers"><code>algorithm_with_properties::has_ready_fibers()</code></link> to inform the fiber manager of the state of your ready queue. </para> </callout> <callout arearefs="fiber.custom.c18" id="fiber.custom.c19"> <para> Overriding <link linkend="algorithm_with_properties_property_change"><code>algorithm_with_properties::property_change()</code></link> is optional. This override handles the case in which the running fiber changes the priority of another ready fiber: a fiber already in our queue. In that case, move the updated fiber within the queue. </para> </callout> <callout arearefs="fiber.custom.c20" id="fiber.custom.c21"> <para> Your <code><phrase role="identifier">property_change</phrase><phrase role="special">()</phrase></code> override must be able to handle the case in which the passed <code><phrase role="identifier">ctx</phrase></code> is not in your ready queue. It might be running, or it might be blocked. </para> </callout> </calloutlist> <para> Our example <code><phrase role="identifier">priority_scheduler</phrase></code> doesn't override <link linkend="algorithm_with_properties_new_properties"><code>algorithm_with_properties::new_properties()</code></link>: we're content with allocating <code><phrase role="identifier">priority_props</phrase></code> instances on the heap. </para> <bridgehead renderas="sect3" id="fiber.custom.h3"> <phrase id="fiber.custom.replace_default_scheduler"/><link linkend="fiber.custom.replace_default_scheduler">Replace Default Scheduler</link> </bridgehead> <para> You must call <link linkend="use_scheduling_algorithm"><code>use_scheduling_algorithm()</code></link> at the start of each thread on which you want <emphasis role="bold">Boost.Fiber</emphasis> to use your custom scheduler rather than its own default <link linkend="class_round_robin"><code>round_robin</code></link>. Specifically, you must call <code><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">()</phrase></code> before performing any other <emphasis role="bold">Boost.Fiber</emphasis> operations on that thread. </para> <para> <programlisting><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">(</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">argc</phrase><phrase role="special">,</phrase> <phrase role="keyword">char</phrase> <phrase role="special">*</phrase><phrase role="identifier">argv</phrase><phrase role="special">[])</phrase> <phrase role="special">{</phrase> <phrase role="comment">// make sure we use our priority_scheduler rather than default round_robin</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">use_scheduling_algorithm</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">priority_scheduler</phrase> <phrase role="special">&gt;();</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> </programlisting> </para> <bridgehead renderas="sect3" id="fiber.custom.h4"> <phrase id="fiber.custom.use_properties"/><link linkend="fiber.custom.use_properties">Use Properties</link> </bridgehead> <para> The running fiber can access its own <link linkend="class_fiber_properties"><code>fiber_properties</code></link> subclass instance by calling <link linkend="this_fiber_properties"><code>this_fiber::properties()</code></link>. Although <code><phrase role="identifier">properties</phrase><phrase role="special">&lt;&gt;()</phrase></code> is a nullary function, you must pass, as a template parameter, the <code><phrase role="identifier">fiber_properties</phrase></code> subclass. </para> <para> <programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">properties</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&gt;().</phrase><phrase role="identifier">name</phrase> <phrase role="special">=</phrase> <phrase role="string">&quot;main&quot;</phrase><phrase role="special">;</phrase> </programlisting> </para> <para> Given a <link linkend="class_fiber"><code>fiber</code></link> instance still connected with a running fiber (that is, not <link linkend="fiber_detach"><code>fiber::detach()</code></link>ed), you may access that fiber's properties using <link linkend="fiber_properties"><code>fiber::properties()</code></link>. As with <code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">this_fiber</phrase><phrase role="special">::</phrase><phrase role="identifier">properties</phrase><phrase role="special">&lt;&gt;()</phrase></code>, you must pass your <code><phrase role="identifier">fiber_properties</phrase></code> subclass as the template parameter. </para> <para> <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">launch</phrase><phrase role="special">(</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">func</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">name</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">priority</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">fibers</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">func</phrase><phrase role="special">);</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">props</phrase><phrase role="special">(</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">.</phrase><phrase role="identifier">properties</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">priority_props</phrase> <phrase role="special">&gt;()</phrase> <phrase role="special">);</phrase> <phrase role="identifier">props</phrase><phrase role="special">.</phrase><phrase role="identifier">name</phrase> <phrase role="special">=</phrase> <phrase role="identifier">name</phrase><phrase role="special">;</phrase> <phrase role="identifier">props</phrase><phrase role="special">.</phrase><phrase role="identifier">set_priority</phrase><phrase role="special">(</phrase> <phrase role="identifier">priority</phrase><phrase role="special">);</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">fiber</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase> </programlisting> </para> <para> Launching a new fiber schedules that fiber as ready, but does <emphasis>not</emphasis> immediately enter its <emphasis>fiber-function</emphasis>. The current fiber retains control until it blocks (or yields, or terminates) for some other reason. As shown in the <code><phrase role="identifier">launch</phrase><phrase role="special">()</phrase></code> function above, it is reasonable to launch a fiber and immediately set relevant properties -- such as, for instance, its priority. Your custom scheduler can then make use of this information next time the fiber manager calls <link linkend="algorithm_with_properties_pick_next"><code>algorithm_with_properties::pick_next()</code></link>. </para> </section> <section id="fiber.rationale"> <title><link linkend="fiber.rationale">Rationale</link></title> <bridgehead renderas="sect3" id="fiber.rationale.h0"> <phrase id="fiber.rationale.preprocessor_defines"/><link linkend="fiber.rationale.preprocessor_defines">preprocessor defines</link> </bridgehead> <table frame="all" id="fiber.rationale.preopcessor_defines"> <title>preopcessor defines</title> <tgroup cols="2"> <thead> <row> <entry> </entry> <entry> </entry> </row> </thead> <tbody> <row> <entry> <para> BOOST_FIBERS_NO_ATOMICS </para> </entry> <entry> <para> no <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">atomic</phrase><phrase role="special">&lt;&gt;</phrase></code> used, inter-thread synchronization disabled </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPINLOCK_STD_MUTEX </para> </entry> <entry> <para> use <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">mutex</phrase></code> as spinlock instead of default <code><phrase role="identifier">XCHG</phrase></code>-sinlock with backoff </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SPIN_BACKOFF </para> </entry> <entry> <para> limit determines when to used <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> instead of mnemonic <code><phrase role="identifier">pause</phrase><phrase role="special">/</phrase><phrase role="identifier">yield</phrase></code> during busy wait (apllies on to <code><phrase role="identifier">XCHG</phrase></code>-spinlock) </para> </entry> </row> <row> <entry> <para> BOOST_FIBERS_SINGLE_CORE </para> </entry> <entry> <para> allways call <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">this_thread</phrase><phrase role="special">::</phrase><phrase role="identifier">yield</phrase><phrase role="special">()</phrase></code> without backoff during busy wait (apllies on to <code><phrase role="identifier">XCHG</phrase></code>-spinlock) </para> </entry> </row> </tbody> </tgroup> </table> <bridgehead renderas="sect3" id="fiber.rationale.h1"> <phrase id="fiber.rationale.distinction_between_coroutines_and_fibers"/><link linkend="fiber.rationale.distinction_between_coroutines_and_fibers">distinction between coroutines and fibers</link> </bridgehead> <para> The fiber library extends the coroutine library by adding a scheduler and synchronization mechanisms. </para> <itemizedlist> <listitem> <simpara> a coroutine yields </simpara> </listitem> <listitem> <simpara> a fiber blocks </simpara> </listitem> </itemizedlist> <para> When a coroutine yields, it passes control directly to its caller (or, in the case of symmetric coroutines, a designated other coroutine). When a fiber blocks, it implicitly passes control to the fiber scheduler. Coroutines have no scheduler because they need no scheduler.<footnote id="fiber.rationale.f0"> <para> <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4024.pdf">'N4024: Distinguishing coroutines and fibers'</ulink> </para> </footnote>. </para> <bridgehead renderas="sect3" id="fiber.rationale.h2"> <phrase id="fiber.rationale.what_about_transactional_memory"/><link linkend="fiber.rationale.what_about_transactional_memory">what about transactional memory</link> </bridgehead> <para> GCC supports transactional memory since version 4.7. Unfortunately tests show that transactional memory is slower (ca. 4x) than spinlocks using atomics. Once transactional memory is improved (supporting hybrid tm), spinlocks will be replaced by __transaction_atomic{} statements surrounding the critical sections. </para> <bridgehead renderas="sect3" id="fiber.rationale.h3"> <phrase id="fiber.rationale.synchronization_between_fibers_running_in_different_threads"/><link linkend="fiber.rationale.synchronization_between_fibers_running_in_different_threads">synchronization between fibers running in different threads</link> </bridgehead> <para> Synchronization classes from <ulink url="http://www.boost.org/doc/libs/release/libs/thread/index.html">Boost.Thread</ulink> block the entire thread. In contrast, the synchronization classes from <emphasis role="bold">Boost.Fiber</emphasis> block only specific fibers, so that the scheduler can still keep the thread busy running other fibers in the meantime. The synchronization classes from <emphasis role="bold">Boost.Fiber</emphasis> are designed to be thread-safe, i.e. it is possible to synchronize fibers running in different threads as well as fibers running in the same thread. (However, there is a build option to disable cross-thread fiber synchronization support; see <link linkend="cross_thread_sync">this description</link>.) </para> <anchor id="spurious_wakeup"/> <bridgehead renderas="sect3" id="fiber.rationale.h4"> <phrase id="fiber.rationale.spurious_wakeup"/><link linkend="fiber.rationale.spurious_wakeup">spurious wakeup</link> </bridgehead> <para> Spurious wakeup can happen when using <ulink url="http://en.cppreference.com/w/cpp/thread/condition_variable"><code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase></code></ulink>: the condition variable appears to be have been signaled while the awaited condition may still be false. Spurious wakeup can happen repeatedly and is caused on some multiprocessor systems where making <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase></code> wakeup completely predictable would slow down all <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_variable</phrase></code> operations.<footnote id="fiber.rationale.f1"> <para> David R. Butenhof <quote>Programming with POSIX Threads</quote> </para> </footnote> </para> <para> <link linkend="class_condition_variable"><code>condition_variable</code></link> is not subject to spurious wakeup. Nonetheless it is prudent to test the business-logic condition in a <code><phrase role="identifier">wait</phrase><phrase role="special">()</phrase></code> loop &mdash; or, equivalently, use one of the <code><phrase role="identifier">wait</phrase><phrase role="special">(</phrase> <phrase role="identifier">lock</phrase><phrase role="special">,</phrase> <phrase role="identifier">predicate</phrase> <phrase role="special">)</phrase></code> overloads. </para> <para> See also <link linkend="condition_variable_spurious_wakeups">No Spurious Wakeups</link>. </para> <bridgehead renderas="sect3" id="fiber.rationale.h5"> <phrase id="fiber.rationale.migrating_fibers_between_threads"/><link linkend="fiber.rationale.migrating_fibers_between_threads">migrating fibers between threads</link> </bridgehead> <para> Support for migrating fibers between threads has been integrated. The user-defined scheduler must call <link linkend="context_detach"><code>context::detach()</code></link> on a fiber-context on the source thread and <link linkend="context_attach"><code>context::attach()</code></link> on the destination thread, passing the fiber-context to migrate. (For more information about custom schedulers, see <link linkend="custom">Customization</link>.) Examples <code><phrase role="identifier">work_sharing</phrase></code> and <code><phrase role="identifier">work_stealing</phrase></code> in directory <code><phrase role="identifier">examples</phrase></code> might be used as a blueprint. </para> <para> See also <link linkend="migration">Migrating fibers between threads</link>. </para> <bridgehead renderas="sect3" id="fiber.rationale.h6"> <phrase id="fiber.rationale.support_for_boost_asio"/><link linkend="fiber.rationale.support_for_boost_asio">support for Boost.Asio</link> </bridgehead> <para> Support for <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink>&#8217;s <emphasis>async-result</emphasis> is not part of the official API. However, to integrate with a <ulink url="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html"><code><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">asio</phrase><phrase role="special">::</phrase><phrase role="identifier">io_service</phrase></code></ulink>, see <link linkend="integration">Sharing a Thread with Another Main Loop</link>. To interface smoothly with an arbitrary Asio async I/O operation, see <link linkend="callbacks_asio">Then There&#8217;s <ulink url="http://www.boost.org/doc/libs/release/libs/asio/index.html">Boost.Asio</ulink></link>. </para> <bridgehead renderas="sect3" id="fiber.rationale.h7"> <phrase id="fiber.rationale.tested_compilers"/><link linkend="fiber.rationale.tested_compilers">tested compilers</link> </bridgehead> <para> The library was tested with GCC-5.1.1, Clang-3.6.0 and MSVC-14.0 in c++11-mode. </para> <bridgehead renderas="sect3" id="fiber.rationale.h8"> <phrase id="fiber.rationale.supported_architectures"/><link linkend="fiber.rationale.supported_architectures">supported architectures</link> </bridgehead> <para> <emphasis role="bold">Boost.Fiber</emphasis> depends on <ulink url="http://www.boost.org/doc/libs/release/libs/context/index.html">Boost.Context</ulink> - the list of supported architectures can be found <ulink url="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/architectures.html">here</ulink>. </para> </section> <section id="fiber.acknowledgements"> <title><link linkend="fiber.acknowledgements">Acknowledgments</link></title> <para> I'd like to thank Agustín Bergé, Eugene Yakubovich, Giovanni Piero Deretta and especially Nat Goodspeed. </para> </section> </library>
0
repos/fiber/doc
repos/fiber/doc/html/index.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Chapter&#160;1.&#160;Fiber</title> <link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="next" href="fiber/overview.html" title="Overview"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> <td align="center"><a href="../../../../index.html">Home</a></td> <td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"><a accesskey="n" href="fiber/overview.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div> <div class="chapter"> <div class="titlepage"><div> <div><h2 class="title"> <a name="fiber"></a>Chapter&#160;1.&#160;Fiber</h2></div> <div><div class="author"><h3 class="author"> <span class="firstname">Oliver</span> <span class="surname">Kowalke</span> </h3></div></div> <div><p class="copyright">Copyright &#169; 2013 Oliver Kowalke</p></div> <div><div class="legalnotice"> <a name="fiber.legal"></a><p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></div> </div></div> <div class="toc"> <p><b>Table of Contents</b></p> <dl> <dt><span class="section"><a href="fiber/overview.html">Overview</a></span></dt> <dd><dl><dt><span class="section"><a href="fiber/overview/implementations__fcontext_t__ucontext_t_and_winfiber.html">Implementations: fcontext_t, ucontext_t and WinFiber</a></span></dt></dl></dd> <dt><span class="section"><a href="fiber/fiber_mgmt.html">Fiber management</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/fiber_mgmt/fiber.html">Class <code class="computeroutput"><span class="identifier">fiber</span></code></a></span></dt> <dt><span class="section"><a href="fiber/fiber_mgmt/id.html">Class fiber::id</a></span></dt> <dt><span class="section"><a href="fiber/fiber_mgmt/this_fiber.html">Namespace this_fiber</a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/scheduling.html">Scheduling</a></span></dt> <dt><span class="section"><a href="fiber/stack.html">Stack allocation</a></span></dt> <dd><dl><dt><span class="section"><a href="fiber/stack/valgrind.html">Support for valgrind</a></span></dt></dl></dd> <dt><span class="section"><a href="fiber/synchronization.html">Synchronization</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/synchronization/mutex_types.html">Mutex Types</a></span></dt> <dt><span class="section"><a href="fiber/synchronization/conditions.html">Condition Variables</a></span></dt> <dt><span class="section"><a href="fiber/synchronization/barriers.html">Barriers</a></span></dt> <dt><span class="section"><a href="fiber/synchronization/channels.html">Channels</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/synchronization/channels/buffered_channel.html">Buffered Channel</a></span></dt> <dt><span class="section"><a href="fiber/synchronization/channels/unbuffered_channel.html">Unbuffered Channel</a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/synchronization/futures.html">Futures</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/synchronization/futures/future.html">Future</a></span></dt> <dt><span class="section"><a href="fiber/synchronization/futures/promise.html">Template <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code></a></span></dt> <dt><span class="section"><a href="fiber/synchronization/futures/packaged_task.html">Template <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code></a></span></dt> </dl></dd> </dl></dd> <dt><span class="section"><a href="fiber/fls.html">Fiber local storage</a></span></dt> <dt><span class="section"><a href="fiber/migration.html">Migrating fibers between threads</a></span></dt> <dt><span class="section"><a href="fiber/callbacks.html">Integrating Fibers with Asynchronous Callbacks</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/callbacks/overview.html">Overview</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/return_errorcode.html">Return Errorcode</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/success_or_exception.html">Success or Exception</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/return_errorcode_or_data.html">Return Errorcode or Data</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/data_or_exception.html">Data or Exception</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/success_error_virtual_methods.html">Success/Error Virtual Methods</a></span></dt> <dt><span class="section"><a href="fiber/callbacks/then_there_s____boost_asio__.html">Then There&#8217;s <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/nonblocking.html">Integrating Fibers with Nonblocking I/O</a></span></dt> <dt><span class="section"><a href="fiber/when_any.html">when_any / when_all functionality</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/when_any/when_any.html">when_any</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__simple_completion.html">when_any, simple completion</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__return_value.html">when_any, return value</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__produce_first_outcome__whether_result_or_exception.html">when_any, produce first outcome, whether result or exception</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__produce_first_success.html">when_any, produce first success</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__heterogeneous_types.html">when_any, heterogeneous types</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_any/when_any__a_dubious_alternative.html">when_any, a dubious alternative</a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/when_any/when_all_functionality.html">when_all functionality</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/when_any/when_all_functionality/when_all__simple_completion.html">when_all, simple completion</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_all_functionality/when_all__return_values.html">when_all, return values</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_all_functionality/when_all_until_first_exception.html">when_all until first exception</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_all_functionality/wait_all__collecting_all_exceptions.html">wait_all, collecting all exceptions</a></span></dt> <dt><span class="section"><a href="fiber/when_any/when_all_functionality/when_all__heterogeneous_types.html">when_all, heterogeneous types</a></span></dt> </dl></dd> </dl></dd> <dt><span class="section"><a href="fiber/integration.html">Sharing a Thread with Another Main Loop</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/integration/overview.html">Overview</a></span></dt> <dt><span class="section"><a href="fiber/integration/event_driven_program.html">Event-Driven Program</a></span></dt> <dt><span class="section"><a href="fiber/integration/embedded_main_loop.html">Embedded Main Loop</a></span></dt> <dt><span class="section"><a href="fiber/integration/deeper_dive_into___boost_asio__.html">Deeper Dive into <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/speculation.html">Specualtive execution</a></span></dt> <dt><span class="section"><a href="fiber/numa.html">NUMA</a></span></dt> <dt><span class="section"><a href="fiber/gpu_computing.html">GPU computing</a></span></dt> <dd><dl> <dt><span class="section"><a href="fiber/gpu_computing/cuda.html">CUDA</a></span></dt> <dt><span class="section"><a href="fiber/gpu_computing/hip.html">ROCm/HIP</a></span></dt> </dl></dd> <dt><span class="section"><a href="fiber/worker.html">Running with worker threads</a></span></dt> <dt><span class="section"><a href="fiber/performance.html">Performance</a></span></dt> <dt><span class="section"><a href="fiber/tuning.html">Tuning</a></span></dt> <dt><span class="section"><a href="fiber/custom.html">Customization</a></span></dt> <dt><span class="section"><a href="fiber/rationale.html">Rationale</a></span></dt> <dt><span class="section"><a href="fiber/acknowledgements.html">Acknowledgments</a></span></dt> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"><p><small>Last revised: October 22, 2018 at 08:13:07 GMT</small></p></td> <td align="right"><div class="copyright-footer"></div></td> </tr></table> <hr> <div class="spirit-nav"><a accesskey="n" href="fiber/overview.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/migration.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Migrating fibers between threads</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="fls.html" title="Fiber local storage"> <link rel="next" href="callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fls.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="callbacks.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.migration"></a><a name="migration"></a><a class="link" href="migration.html" title="Migrating fibers between threads">Migrating fibers between threads</a> </h2></div></div></div> <h4> <a name="fiber.migration.h0"></a> <span><a name="fiber.migration.overview"></a></span><a class="link" href="migration.html#fiber.migration.overview">Overview</a> </h4> <p> Each fiber owns a stack and manages its execution state, including all registers and CPU flags, the instruction pointer and the stack pointer. That means, in general, a fiber is not bound to a specific thread.<sup>[<a name="fiber.migration.f0" href="#ftn.fiber.migration.f0" class="footnote">3</a>]</sup><sup>,</sup><sup>[<a name="fiber.migration.f1" href="#ftn.fiber.migration.f1" class="footnote">4</a>]</sup> </p> <p> Migrating a fiber from a logical CPU with heavy workload to another logical CPU with a lighter workload might speed up the overall execution. Note that in the case of NUMA-architectures, it is not always advisable to migrate data between threads. Suppose fiber <span class="emphasis"><em>f</em></span> is running on logical CPU <span class="emphasis"><em>cpu0</em></span> which belongs to NUMA node <span class="emphasis"><em>node0</em></span>. The data of <span class="emphasis"><em>f</em></span> are allocated on the physical memory located at <span class="emphasis"><em>node0</em></span>. Migrating the fiber from <span class="emphasis"><em>cpu0</em></span> to another logical CPU <span class="emphasis"><em>cpuX</em></span> which is part of a different NUMA node <span class="emphasis"><em>nodeX</em></span> might reduce the performance of the application due to increased latency of memory access. </p> <p> Only fibers that are contained in <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>&#8217;s ready queue can migrate between threads. You cannot migrate a running fiber, nor one that is <a class="link" href="overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a>. You cannot migrate a fiber if its <a class="link" href="scheduling.html#context_is_context"><code class="computeroutput">context::is_context()</code></a> method returns <code class="computeroutput"><span class="keyword">true</span></code> for <code class="computeroutput"><span class="identifier">pinned_context</span></code>. </p> <p> In <span class="bold"><strong>Boost.Fiber</strong></span> a fiber is migrated by invoking <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a> on the thread from which the fiber migrates and <a class="link" href="scheduling.html#context_attach"><code class="computeroutput">context::attach()</code></a> on the thread to which the fiber migrates. </p> <p> Thus, fiber migration is accomplished by sharing state between instances of a user-coded <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementation running on different threads. The fiber&#8217;s original thread calls <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a>, passing the fiber&#8217;s <a class="link" href="scheduling.html#class_context"><code class="computeroutput">context</code></a><code class="literal">*</code>. The <code class="computeroutput"><span class="identifier">awakened</span><span class="special">()</span></code> implementation calls <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a>. </p> <p> At some later point, when the same or a different thread calls <a class="link" href="scheduling.html#algorithm_pick_next"><code class="computeroutput">algorithm::pick_next()</code></a>, the <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> implementation selects a ready fiber and calls <a class="link" href="scheduling.html#context_attach"><code class="computeroutput">context::attach()</code></a> on it before returning it. </p> <p> As stated above, a <code class="computeroutput"><span class="identifier">context</span></code> for which <code class="computeroutput"><span class="identifier">is_context</span><span class="special">(</span><span class="identifier">pinned_context</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">true</span></code> must never be passed to either <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a> or <a class="link" href="scheduling.html#context_attach"><code class="computeroutput">context::attach()</code></a>. It may only be returned from <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> called by the <span class="emphasis"><em>same</em></span> thread that passed that context to <code class="computeroutput"><span class="identifier">awakened</span><span class="special">()</span></code>. </p> <h4> <a name="fiber.migration.h1"></a> <span><a name="fiber.migration.example_of_work_sharing"></a></span><a class="link" href="migration.html#fiber.migration.example_of_work_sharing">Example of work sharing</a> </h4> <p> In the example <a href="../../../examples/work_sharing.cpp" target="_top">work_sharing.cpp</a> multiple worker fibers are created on the main thread. Each fiber gets a character as parameter at construction. This character is printed out ten times. Between each iteration the fiber calls <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. That puts the fiber in the ready queue of the fiber-scheduler <span class="emphasis"><em>shared_ready_queue</em></span>, running in the current thread. The next fiber ready to be executed is dequeued from the shared ready queue and resumed by <span class="emphasis"><em>shared_ready_queue</em></span> running on <span class="emphasis"><em>any participating thread</em></span>. </p> <p> All instances of <span class="emphasis"><em>shared_ready_queue</em></span> share one global concurrent queue, used as ready queue. This mechanism shares all worker fibers between all instances of <span class="emphasis"><em>shared_ready_queue</em></span>, thus between all participating threads. </p> <h4> <a name="fiber.migration.h2"></a> <span><a name="fiber.migration.setup_of_threads_and_fibers"></a></span><a class="link" href="migration.html#fiber.migration.setup_of_threads_and_fibers">Setup of threads and fibers</a> </h4> <p> In <code class="computeroutput"><span class="identifier">main</span><span class="special">()</span></code> the fiber-scheduler is installed and the worker fibers and the threads are launched. </p> <p> </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">shared_work</span> <span class="special">&gt;();</span> <a class="co" name="fiber.migration.c0" href="migration.html#fiber.migration.c1"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">char</span> <span class="identifier">c</span> <span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="string">"abcdefghijklmnopqrstuvwxyz"</span><span class="special">))</span> <span class="special">{</span> <a class="co" name="fiber.migration.c2" href="migration.html#fiber.migration.c3"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">([</span><span class="identifier">c</span><span class="special">](){</span> <span class="identifier">whatevah</span><span class="special">(</span> <span class="identifier">c</span><span class="special">);</span> <span class="special">}).</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">++</span><span class="identifier">fiber_count</span><span class="special">;</span> <a class="co" name="fiber.migration.c4" href="migration.html#fiber.migration.c5"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_barrier</span> <span class="identifier">b</span><span class="special">(</span> <span class="number">4</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span> <span class="identifier">threads</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <a class="co" name="fiber.migration.c6" href="migration.html#fiber.migration.c7"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="identifier">thread</span><span class="special">,</span> <span class="special">&amp;</span> <span class="identifier">b</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="identifier">thread</span><span class="special">,</span> <span class="special">&amp;</span> <span class="identifier">b</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="identifier">thread</span><span class="special">,</span> <span class="special">&amp;</span> <span class="identifier">b</span><span class="special">)</span> <span class="special">};</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">wait</span><span class="special">();</span> <a class="co" name="fiber.migration.c8" href="migration.html#fiber.migration.c9"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> <span class="special">{</span> <span class="identifier">lock_type</span><a class="co" name="fiber.migration.c10" href="migration.html#fiber.migration.c11"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_count</span><span class="special">);</span> <span class="identifier">cnd_count</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="number">0</span> <span class="special">==</span> <span class="identifier">fiber_count</span><span class="special">;</span> <span class="special">}</span> <span class="special">);</span> <a class="co" name="fiber.migration.c12" href="migration.html#fiber.migration.c13"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> <span class="special">}</span> <a class="co" name="fiber.migration.c14" href="migration.html#fiber.migration.c15"><img src="../../../../../doc/src/images/callouts/8.png" alt="8" border="0"></a> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="number">0</span> <span class="special">==</span> <span class="identifier">fiber_count</span><span class="special">);</span> <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span> <span class="special">&amp;</span> <span class="identifier">t</span> <span class="special">:</span> <span class="identifier">threads</span><span class="special">)</span> <span class="special">{</span> <a class="co" name="fiber.migration.c16" href="migration.html#fiber.migration.c17"><img src="../../../../../doc/src/images/callouts/9.png" alt="9" border="0"></a> <span class="identifier">t</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <div class="calloutlist"><table border="0" summary="Callout list"> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c1"></a><a href="#fiber.migration.c0"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> <td valign="top" align="left"><p> Install the scheduling algorithm <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">shared_work</span></code> in the main thread too, so each new fiber gets launched into the shared pool. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c3"></a><a href="#fiber.migration.c2"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> <td valign="top" align="left"><p> Launch a number of worker fibers; each worker fiber picks up a character that is passed as parameter to fiber-function <code class="computeroutput"><span class="identifier">whatevah</span></code>. Each worker fiber gets detached. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c5"></a><a href="#fiber.migration.c4"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> <td valign="top" align="left"><p> Increment fiber counter for each new fiber. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c7"></a><a href="#fiber.migration.c6"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td> <td valign="top" align="left"><p> Launch a couple of threads that join the work sharing. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c9"></a><a href="#fiber.migration.c8"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> </p></td> <td valign="top" align="left"><p> sync with other threads: allow them to start processing </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c11"></a><a href="#fiber.migration.c10"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> </p></td> <td valign="top" align="left"><p> <code class="computeroutput"><span class="identifier">lock_type</span></code> is typedef'ed as <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a>&lt; <a href="http://en.cppreference.com/w/cpp/thread/mutex" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span></code></a> &gt; </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c13"></a><a href="#fiber.migration.c12"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> </p></td> <td valign="top" align="left"><p> Suspend main fiber and resume worker fibers in the meanwhile. Main fiber gets resumed (e.g returns from <code class="computeroutput"><span class="identifier">condition_variable_any</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>) if all worker fibers are complete. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c15"></a><a href="#fiber.migration.c14"><img src="../../../../../doc/src/images/callouts/8.png" alt="8" border="0"></a> </p></td> <td valign="top" align="left"><p> Releasing lock of mtx_count is required before joining the threads, otherwise the other threads would be blocked inside condition_variable::wait() and would never return (deadlock). </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c17"></a><a href="#fiber.migration.c16"><img src="../../../../../doc/src/images/callouts/9.png" alt="9" border="0"></a> </p></td> <td valign="top" align="left"><p> wait for threads to terminate </p></td> </tr> </table></div> <p> The start of the threads is synchronized with a barrier. The main fiber of each thread (including main thread) is suspended until all worker fibers are complete. When the main fiber returns from <a class="link" href="synchronization/conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>, the thread terminates: the main thread joins all other threads. </p> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">thread</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">thread_barrier</span> <span class="special">*</span> <span class="identifier">b</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">buffer</span><span class="special">;</span> <span class="identifier">buffer</span> <span class="special">&lt;&lt;</span> <span class="string">"thread started "</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">buffer</span><span class="special">.</span><span class="identifier">str</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">flush</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">shared_work</span> <span class="special">&gt;();</span> <a class="co" name="fiber.migration.c18" href="migration.html#fiber.migration.c19"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> <span class="identifier">b</span><span class="special">-&gt;</span><span class="identifier">wait</span><span class="special">();</span> <a class="co" name="fiber.migration.c20" href="migration.html#fiber.migration.c21"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> <span class="identifier">lock_type</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_count</span><span class="special">);</span> <span class="identifier">cnd_count</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="number">0</span> <span class="special">==</span> <span class="identifier">fiber_count</span><span class="special">;</span> <span class="special">}</span> <span class="special">);</span> <a class="co" name="fiber.migration.c22" href="migration.html#fiber.migration.c23"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="number">0</span> <span class="special">==</span> <span class="identifier">fiber_count</span><span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <div class="calloutlist"><table border="0" summary="Callout list"> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c19"></a><a href="#fiber.migration.c18"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> <td valign="top" align="left"><p> Install the scheduling algorithm <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">shared_work</span></code> in order to join the work sharing. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c21"></a><a href="#fiber.migration.c20"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> <td valign="top" align="left"><p> sync with other threads: allow them to start processing </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c23"></a><a href="#fiber.migration.c22"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> <td valign="top" align="left"><p> Suspend main fiber and resume worker fibers in the meanwhile. Main fiber gets resumed (e.g returns from <code class="computeroutput"><span class="identifier">condition_variable_any</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>) if all worker fibers are complete. </p></td> </tr> </table></div> <p> Each worker fiber executes function <code class="computeroutput"><span class="identifier">whatevah</span><span class="special">()</span></code> with character <code class="computeroutput"><span class="identifier">me</span></code> as parameter. The fiber yields in a loop and prints out a message if it was migrated to another thread. </p> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">whatevah</span><span class="special">(</span> <span class="keyword">char</span> <span class="identifier">me</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">my_thread</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">();</span> <a class="co" name="fiber.migration.c24" href="migration.html#fiber.migration.c25"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">buffer</span><span class="special">;</span> <span class="identifier">buffer</span> <span class="special">&lt;&lt;</span> <span class="string">"fiber "</span> <span class="special">&lt;&lt;</span> <span class="identifier">me</span> <span class="special">&lt;&lt;</span> <span class="string">" started on thread "</span> <span class="special">&lt;&lt;</span> <span class="identifier">my_thread</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">buffer</span><span class="special">.</span><span class="identifier">str</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">flush</span><span class="special">;</span> <span class="special">}</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <a class="co" name="fiber.migration.c26" href="migration.html#fiber.migration.c27"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">yield</span><span class="special">();</span> <a class="co" name="fiber.migration.c28" href="migration.html#fiber.migration.c29"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">new_thread</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">();</span> <a class="co" name="fiber.migration.c30" href="migration.html#fiber.migration.c31"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">new_thread</span> <span class="special">!=</span> <span class="identifier">my_thread</span><span class="special">)</span> <span class="special">{</span> <a class="co" name="fiber.migration.c32" href="migration.html#fiber.migration.c33"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> <span class="identifier">my_thread</span> <span class="special">=</span> <span class="identifier">new_thread</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">buffer</span><span class="special">;</span> <span class="identifier">buffer</span> <span class="special">&lt;&lt;</span> <span class="string">"fiber "</span> <span class="special">&lt;&lt;</span> <span class="identifier">me</span> <span class="special">&lt;&lt;</span> <span class="string">" switched to thread "</span> <span class="special">&lt;&lt;</span> <span class="identifier">my_thread</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">buffer</span><span class="special">.</span><span class="identifier">str</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">flush</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span> <span class="special">...</span> <span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="identifier">lock_type</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_count</span><span class="special">);</span> <span class="keyword">if</span> <span class="special">(</span> <span class="number">0</span> <span class="special">==</span> <span class="special">--</span><span class="identifier">fiber_count</span><span class="special">)</span> <span class="special">{</span> <a class="co" name="fiber.migration.c34" href="migration.html#fiber.migration.c35"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> <span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span> <span class="identifier">cnd_count</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span> <a class="co" name="fiber.migration.c36" href="migration.html#fiber.migration.c37"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> <span class="special">}</span> <span class="special">}</span> </pre> <p> </p> <div class="calloutlist"><table border="0" summary="Callout list"> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c25"></a><a href="#fiber.migration.c24"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> <td valign="top" align="left"><p> get ID of initial thread </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c27"></a><a href="#fiber.migration.c26"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> <td valign="top" align="left"><p> loop ten times </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c29"></a><a href="#fiber.migration.c28"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> <td valign="top" align="left"><p> yield to other fibers </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c31"></a><a href="#fiber.migration.c30"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td> <td valign="top" align="left"><p> get ID of current thread </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c33"></a><a href="#fiber.migration.c32"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> </p></td> <td valign="top" align="left"><p> test if fiber was migrated to another thread </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c35"></a><a href="#fiber.migration.c34"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> </p></td> <td valign="top" align="left"><p> Decrement fiber counter for each completed fiber. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.migration.c37"></a><a href="#fiber.migration.c36"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> </p></td> <td valign="top" align="left"><p> Notify all fibers waiting on <code class="computeroutput"><span class="identifier">cnd_count</span></code>. </p></td> </tr> </table></div> <h4> <a name="fiber.migration.h3"></a> <span><a name="fiber.migration.scheduling_fibers"></a></span><a class="link" href="migration.html#fiber.migration.scheduling_fibers">Scheduling fibers</a> </h4> <p> The fiber scheduler <code class="computeroutput"><span class="identifier">shared_ready_queue</span></code> is like <code class="computeroutput"><span class="identifier">round_robin</span></code>, except that it shares a common ready queue among all participating threads. A thread participates in this pool by executing <a class="link" href="fiber_mgmt/fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a> before any other <span class="bold"><strong>Boost.Fiber</strong></span> operation. </p> <p> The important point about the ready queue is that it&#8217;s a class static, common to all instances of shared_ready_queue. Fibers that are enqueued via <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a> (fibers that are ready to be resumed) are thus available to all threads. It is required to reserve a separate, scheduler-specific queue for the thread&#8217;s main fiber and dispatcher fibers: these may <span class="emphasis"><em>not</em></span> be shared between threads! When we&#8217;re passed either of these fibers, push it there instead of in the shared queue: it would be Bad News for thread B to retrieve and attempt to execute thread A&#8217;s main fiber. </p> <p> [awakened_ws] </p> <p> When <a class="link" href="scheduling.html#algorithm_pick_next"><code class="computeroutput">algorithm::pick_next()</code></a> gets called inside one thread, a fiber is dequeued from <span class="emphasis"><em>rqueue_</em></span> and will be resumed in that thread. </p> <p> [pick_next_ws] </p> <p> The source code above is found in <a href="../../../examples/work_sharing.cpp" target="_top">work_sharing.cpp</a>. </p> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.migration.f0" href="#fiber.migration.f0" class="para">3</a>] </sup> The <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber on each thread, that is, the fiber on which the thread is launched, cannot migrate to any other thread. Also <span class="bold"><strong>Boost.Fiber</strong></span> implicitly creates a dispatcher fiber for each thread &#8212; this cannot migrate either. </p></div> <div class="footnote"><p><sup>[<a name="ftn.fiber.migration.f1" href="#fiber.migration.f1" class="para">4</a>] </sup> Of course it would be problematic to migrate a fiber that relies on <a class="link" href="overview.html#thread_local_storage">thread-local storage</a>. </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fls.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="callbacks.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/numa.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>NUMA</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="speculation.html" title="Specualtive execution"> <link rel="next" href="gpu_computing.html" title="GPU computing"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="speculation.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gpu_computing.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.numa"></a><a name="numa"></a><a class="link" href="numa.html" title="NUMA">NUMA</a> </h2></div></div></div> <p> Modern micro-processors contain integrated memory controllers that are connected via channels to the memory. Accessing the memory can be organized in two kinds:<br> Uniform Memory Access (UMA) and Non-Uniform Memory Access (NUMA). </p> <p> In contrast to UMA, that provides a centralized pool of memory (and thus does not scale after a certain number of processors), a NUMA architecture divides the memory into local and remote memory relative to the micro-processor.<br> Local memory is directly attached to the processor's integrated memory controller. Memory connected to the memory controller of another micro-processor (multi-socket systems) is considered as remote memory. If a memory controller access remote memory it has to traverse the interconnect<sup>[<a name="fiber.numa.f0" href="#ftn.fiber.numa.f0" class="footnote">8</a>]</sup> and connect to the remote memory controller.<br> Thus accessing remote memory adds additional latency overhead to local memory access. Because of the different memory locations, a NUMA-system experiences <span class="emphasis"><em>non-uniform</em></span> memory access time.<br> As a consequence the best performance is achieved by keeping the memory access local. </p> <p> <span class="inlinemediaobject"><img src="../../../../../libs/fiber/doc/NUMA.png" align="middle" alt="NUMA"></span> </p> <h4> <a name="fiber.numa.h0"></a> <span><a name="fiber.numa.numa_support_in_boost_fiber"></a></span><a class="link" href="numa.html#fiber.numa.numa_support_in_boost_fiber">NUMA support in Boost.Fiber</a> </h4> <p> Because only a subset of the NUMA-functionality is exposed by several operating systems, Boost.Fiber provides only a minimalistic NUMA API. </p> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> In order to enable NUMA support, b2 property <code class="computeroutput"><span class="identifier">numa</span><span class="special">=</span><span class="identifier">on</span></code> must be specified and linked against additional library <code class="computeroutput"><span class="identifier">libboost_fiber_numa</span><span class="special">.</span><span class="identifier">so</span></code>. </p></td></tr> </table></div> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> MinGW using pthread implementation is not supported on Windows. </p></td></tr> </table></div> <div class="table"> <a name="fiber.numa.supported_functionality_operating_systems"></a><p class="title"><b>Table&#160;1.1.&#160;Supported functionality/operating systems</b></p> <div class="table-contents"><table class="table" summary="Supported functionality/operating systems"> <colgroup> <col> <col> <col> <col> <col> <col> <col> </colgroup> <thead><tr> <th> </th> <th> <p> AIX </p> </th> <th> <p> FreeBSD </p> </th> <th> <p> HP/UX </p> </th> <th> <p> Linux </p> </th> <th> <p> Solaris </p> </th> <th> <p> Windows </p> </th> </tr></thead> <tbody> <tr> <td> <p> pin thread </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> </tr> <tr> <td> <p> logical CPUs/NUMA nodes </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> + </p> </td> <td> <p> +<sup>[<a name="fiber.numa.f1" href="#ftn.fiber.numa.f1" class="footnote">a</a>]</sup> </p> </td> </tr> <tr> <td> <p> NUMA node distance </p> </td> <td> <p> - </p> </td> <td> <p> - </p> </td> <td> <p> - </p> </td> <td> <p> + </p> </td> <td> <p> - </p> </td> <td> <p> - </p> </td> </tr> <tr> <td> <p> tested on </p> </td> <td> <p> AIX 7.2 </p> </td> <td> <p> FreeBSD 11 </p> </td> <td> <p> - </p> </td> <td> <p> Arch Linux (4.10.13) </p> </td> <td> <p> OpenIndiana HIPSTER </p> </td> <td> <p> Windows 10 </p> </td> </tr> </tbody> <tbody class="footnotes"><tr><td colspan="7"><div class="footnote"><p><sup>[<a name="ftn.fiber.numa.f1" href="#fiber.numa.f1" class="para">a</a>] </sup> Windows organizes logical cpus in groups of 64; boost.fiber maps {group-id,cpud-id} to a scalar equivalent to cpu ID of Linux (64 * group ID + cpu ID). </p></div></td></tr></tbody> </table></div> </div> <br class="table-break"><p> In order to keep the memory access local as possible, the NUMA topology must be evaluated. </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">node</span> <span class="special">&gt;</span> <span class="identifier">topo</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">topology</span><span class="special">();</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">auto</span> <span class="identifier">n</span> <span class="special">:</span> <span class="identifier">topo</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"node: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">n</span><span class="special">.</span><span class="identifier">id</span> <span class="special">&lt;&lt;</span> <span class="string">" | "</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"cpus: "</span><span class="special">;</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">auto</span> <span class="identifier">cpu_id</span> <span class="special">:</span> <span class="identifier">n</span><span class="special">.</span><span class="identifier">logical_cpus</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">cpu_id</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"| distance: "</span><span class="special">;</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">auto</span> <span class="identifier">d</span> <span class="special">:</span> <span class="identifier">n</span><span class="special">.</span><span class="identifier">distance</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">d</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"done"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">output</span><span class="special">:</span> <span class="identifier">node</span><span class="special">:</span> <span class="number">0</span> <span class="special">|</span> <span class="identifier">cpus</span><span class="special">:</span> <span class="number">0</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span> <span class="number">16</span> <span class="number">17</span> <span class="number">18</span> <span class="number">19</span> <span class="number">20</span> <span class="number">21</span> <span class="number">22</span> <span class="number">23</span> <span class="special">|</span> <span class="identifier">distance</span><span class="special">:</span> <span class="number">10</span> <span class="number">21</span> <span class="identifier">node</span><span class="special">:</span> <span class="number">1</span> <span class="special">|</span> <span class="identifier">cpus</span><span class="special">:</span> <span class="number">8</span> <span class="number">9</span> <span class="number">10</span> <span class="number">11</span> <span class="number">12</span> <span class="number">13</span> <span class="number">14</span> <span class="number">15</span> <span class="number">24</span> <span class="number">25</span> <span class="number">26</span> <span class="number">27</span> <span class="number">28</span> <span class="number">29</span> <span class="number">30</span> <span class="number">31</span> <span class="special">|</span> <span class="identifier">distance</span><span class="special">:</span> <span class="number">21</span> <span class="number">10</span> <span class="identifier">done</span> </pre> <p> The example shows that the systems consits out of 2 NUMA-nodes, to each NUMA-node belong 16 logical cpus. The distance measures the costs to access the memory of another NUMA-node. A NUMA-node has always a distance <code class="computeroutput"><span class="number">10</span></code> to itself (lowest possible value).<br> The position in the array corresponds with the NUMA-node ID. </p> <p> Some work-loads benefit from pinning threads to a logical cpus. For instance scheduling algorithm <a class="link" href="numa.html#class_numa_work_stealing"><code class="computeroutput">numa::work_stealing</code></a> pins the thread that runs the fiber scheduler to a logical cpu. This prevents the operating system scheduler to move the thread to another logical cpu that might run other fiber scheduler(s) or migrating the thread to a logical cpu part of another NUMA-node. </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">node_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">node</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">topo</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// thread registers itself at work-stealing scheduler</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">work_stealing</span> <span class="special">&gt;(</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">node_id</span><span class="special">,</span> <span class="identifier">topo</span><span class="special">);</span> <span class="special">...</span> <span class="special">}</span> <span class="comment">// evaluate the NUMA topology</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">node</span> <span class="special">&gt;</span> <span class="identifier">topo</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">topology</span><span class="special">();</span> <span class="comment">// start-thread runs on NUMA-node `0`</span> <span class="keyword">auto</span> <span class="identifier">node</span> <span class="special">=</span> <span class="identifier">topo</span><span class="special">[</span><span class="number">0</span><span class="special">];</span> <span class="comment">// start-thread is pinnded to first cpu ID in the list of logical cpus of NUMA-node `0`</span> <span class="keyword">auto</span> <span class="identifier">start_cpu_id</span> <span class="special">=</span> <span class="special">*</span> <span class="identifier">node</span><span class="special">.</span><span class="identifier">logical_cpus</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> <span class="comment">// start worker-threads first</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span> <span class="special">&gt;</span> <span class="identifier">threads</span><span class="special">;</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">auto</span> <span class="special">&amp;</span> <span class="identifier">node</span> <span class="special">:</span> <span class="identifier">topo</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span> <span class="special">:</span> <span class="identifier">node</span><span class="special">.</span><span class="identifier">logical_cpus</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// exclude start-thread</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">start_cpu_id</span> <span class="special">!=</span> <span class="identifier">cpu_id</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// spawn thread</span> <span class="identifier">threads</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">(</span> <span class="identifier">thread</span><span class="special">,</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">node</span><span class="special">.</span><span class="identifier">id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span> <span class="identifier">topo</span><span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="special">}</span> <span class="comment">// start-thread registers itself on work-stealing scheduler</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">work_stealing</span> <span class="special">&gt;(</span> <span class="identifier">start_cpu_id</span><span class="special">,</span> <span class="identifier">node</span><span class="special">.</span><span class="identifier">id</span><span class="special">,</span> <span class="identifier">topo</span><span class="special">);</span> <span class="special">...</span> </pre> <p> The example evaluates the NUMA topology with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">topology</span><span class="special">()</span></code> and spawns for each logical cpu a thread. Each spawned thread installs the NUMA-aware work-stealing scheduler. The scheduler pins the thread to the logical cpu that was specified at construction.<br> If the local queue of one thread runs out of ready fibers, the thread tries to steal a ready fiber from another thread running at logical cpu that belong to the same NUMA-node (local memory access). If no fiber could be stolen, the thread tries to steal fibers from logical cpus part of other NUMA-nodes (remote memory access). </p> <h4> <a name="fiber.numa.h1"></a> <span><a name="fiber.numa.synopsis"></a></span><a class="link" href="numa.html#fiber.numa.synopsis">Synopsis</a> </h4> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">pin_thread</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">topology</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">node</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">id</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">logical_cpus</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">distance</span><span class="special">;</span> <span class="special">};</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">node</span> <span class="special">&gt;</span> <span class="identifier">topology</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">pin_thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span><span class="special">);</span> <span class="keyword">void</span> <span class="identifier">pin_thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">native_handle_type</span><span class="special">);</span> <span class="special">}}}</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">work_stealing</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">work_stealing</span><span class="special">;</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="class_numa_node_bridgehead"></a> <span><a name="class_numa_node"></a></span> <a class="link" href="numa.html#class_numa_node">Class <code class="computeroutput">numa::node</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">topology</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">node</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">id</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">logical_cpus</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">distance</span><span class="special">;</span> <span class="special">};</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="numa_node_id_bridgehead"></a> <span><a name="numa_node_id"></a></span> <a class="link" href="numa.html#numa_node_id">Data member <code class="computeroutput">id</code></a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">id</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> ID of the NUMA-node </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_node_logical_cpus_bridgehead"></a> <span><a name="numa_node_logical_cpus"></a></span> <a class="link" href="numa.html#numa_node_logical_cpus">Data member <code class="computeroutput">logical_cpus</code></a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">logical_cpus</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> set of logical cpu IDs belonging to the NUMA-node </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_node_distance_bridgehead"></a> <span><a name="numa_node_distance"></a></span> <a class="link" href="numa.html#numa_node_distance">Data member <code class="computeroutput">distance</code></a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="special">&gt;</span> <span class="identifier">distance</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The distance between NUMA-nodes describe the cots of accessing the remote memory. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> A NUMA-node has a distance of <code class="computeroutput"><span class="number">10</span></code> to itself, remote NUMA-nodes have a distance &gt; <code class="computeroutput"><span class="number">10</span></code>. The index in the array corresponds to the ID <code class="computeroutput"><span class="identifier">id</span></code> of the NUMA-node. At the moment only Linux returns the correct distances, for all other operating systems remote NUMA-nodes get a default value of <code class="computeroutput"><span class="number">20</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_node_operator_less_bridgehead"></a> <span><a name="numa_node_operator_less"></a></span> <a class="link" href="numa.html#numa_node_operator_less">Member function <code class="computeroutput">operator&lt;</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">node</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">lhs</span> <span class="special">!=</span> <span class="identifier">rhs</span></code> is true and the implementation-defined total order of <code class="computeroutput"><span class="identifier">node</span><span class="special">::</span><span class="identifier">id</span></code> values places <code class="computeroutput"><span class="identifier">lhs</span></code> before <code class="computeroutput"><span class="identifier">rhs</span></code>, false otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_topology_bridgehead"></a> <span><a name="numa_topology"></a></span> <a class="link" href="numa.html#numa_topology">Non-member function <code class="computeroutput">numa::topology()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">topology</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">node</span> <span class="special">&gt;</span> <span class="identifier">topology</span><span class="special">();</span> <span class="special">}}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Evaluates the NUMA topology. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> a vector of NUMA-nodes describing the NUMA architecture of the system (each element represents a NUMA-node). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">system_error</span></code> </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_pin_thread_bridgehead"></a> <span><a name="numa_pin_thread"></a></span> <a class="link" href="numa.html#numa_pin_thread">Non-member function <code class="computeroutput">numa::pin_thread()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">pin_thread</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">pin_thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span><span class="special">);</span> <span class="keyword">void</span> <span class="identifier">pin_thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">native_handle_type</span> <span class="identifier">h</span><span class="special">);</span> <span class="special">}}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> First version pins <code class="computeroutput"><span class="keyword">this</span> <span class="identifier">thread</span></code> to the logical cpu with ID <code class="computeroutput"><span class="identifier">cpu_id</span></code>, e.g. the operating system scheduler will not migrate the thread to another logical cpu. The second variant pins the thread with the native ID <code class="computeroutput"><span class="identifier">h</span></code> to logical cpu with ID <code class="computeroutput"><span class="identifier">cpu_id</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">system_error</span></code> </p></dd> </dl> </div> <p> </p> <h5> <a name="class_numa_work_stealing_bridgehead"></a> <span><a name="class_numa_work_stealing"></a></span> <a class="link" href="numa.html#class_numa_work_stealing">Class <code class="computeroutput">numa::work_stealing</code></a> </h5> <p> </p> <p> This class implements <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>; the thread running this scheduler is pinned to the given logical cpu. If the local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers that run on logical cpus that belong to the same NUMA-node (local memory access).<br> If no ready fibers can be stolen from the local NUMA-node, the algorithm selects schedulers running on other NUMA-nodes (remote memory access).<br> The victim scheduler (from which a ready fiber is stolen) is selected at random. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">numa</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">work_stealing</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numa</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">work_stealing</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">algorithm</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">node_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">node</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">topo</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">suspend</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">work_stealing</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">work_stealing</span> <span class="special">&amp;&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">work_stealing</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">work_stealing</span> <span class="special">&amp;&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}}}</span> </pre> <h4> <a name="fiber.numa.h2"></a> <span><a name="fiber.numa.constructor"></a></span><a class="link" href="numa.html#fiber.numa.constructor">Constructor</a> </h4> <pre class="programlisting"><span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">cpu_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">node_id</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">numa</span><span class="special">::</span><span class="identifier">node</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">topo</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">suspend</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs work-stealing scheduling algorithm. The thread is pinned to logical cpu with ID <code class="computeroutput"><span class="identifier">cpu_id</span></code>. If local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers using <code class="computeroutput"><span class="identifier">topology</span></code> (represents the NUMA-topology of the system). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">system_error</span></code> </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> If <code class="computeroutput"><span class="identifier">suspend</span></code> is set to <code class="computeroutput"><span class="keyword">true</span></code>, then the scheduler suspends if no ready fiber could be stolen. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote (other thread or fiber scheduler). </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_work_stealing_awakened_bridgehead"></a> <span><a name="numa_work_stealing_awakened"></a></span> <a class="link" href="numa.html#numa_work_stealing_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Enqueues fiber <code class="computeroutput"><span class="identifier">f</span></code> onto the shared ready queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_work_stealing_pick_next_bridgehead"></a> <span><a name="numa_work_stealing_pick_next"></a></span> <a class="link" href="numa.html#numa_work_stealing_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber at the head of the ready queue, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if the queue is empty. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Placing ready fibers onto the tail of the sahred queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_work_stealing_has_ready_fibers_bridgehead"></a> <span><a name="numa_work_stealing_has_ready_fibers"></a></span> <a class="link" href="numa.html#numa_work_stealing_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_work_stealing_suspend_until_bridgehead"></a> <span><a name="numa_work_stealing_suspend_until"></a></span> <a class="link" href="numa.html#numa_work_stealing_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs <code class="computeroutput"><span class="identifier">work_stealing</span></code> that no ready fiber will be available until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. This implementation blocks in <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">wait_until</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="numa_work_stealing_notify_bridgehead"></a> <span><a name="numa_work_stealing_notify"></a></span> <a class="link" href="numa.html#numa_work_stealing_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Wake up a pending call to <a class="link" href="scheduling.html#work_stealing_suspend_until"><code class="computeroutput">work_stealing::suspend_until()</code></a>, some fibers might be ready. This implementation wakes <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> via <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">notify_all</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.numa.f0" href="#fiber.numa.f0" class="para">8</a>] </sup> On x86 the interconnection is implemented by Intel's Quick Path Interconnect (QPI) and AMD's HyperTransport. </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="speculation.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gpu_computing.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/stack.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Stack allocation</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="scheduling.html" title="Scheduling"> <link rel="next" href="stack/valgrind.html" title="Support for valgrind"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="scheduling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack/valgrind.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.stack"></a><a name="stack"></a><a class="link" href="stack.html" title="Stack allocation">Stack allocation</a> </h2></div></div></div> <div class="toc"><dl><dt><span class="section"><a href="stack/valgrind.html">Support for valgrind</a></span></dt></dl></div> <p> A <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> uses internally an __econtext__ which manages a set of registers and a stack. The memory used by the stack is allocated/deallocated via a <span class="emphasis"><em>stack_allocator</em></span> which is required to model a <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>. </p> <p> A <span class="emphasis"><em>stack_allocator</em></span> can be passed to <a class="link" href="fiber_mgmt/fiber.html#fiber_fiber"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">()</span></code></a> or to <a class="link" href="synchronization/futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a>. </p> <a name="stack_allocator_concept"></a><h4> <a name="fiber.stack.h0"></a> <span><a name="fiber.stack.stack_allocator_concept"></a></span><a class="link" href="stack.html#fiber.stack.stack_allocator_concept">stack-allocator concept</a> </h4> <p> A <span class="emphasis"><em>stack_allocator</em></span> must satisfy the <span class="emphasis"><em>stack-allocator concept</em></span> requirements shown in the following table, in which <code class="computeroutput"><span class="identifier">a</span></code> is an object of a <span class="emphasis"><em>stack_allocator</em></span> type, <code class="computeroutput"><span class="identifier">sctx</span></code> is a <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a>, and <code class="computeroutput"><span class="identifier">size</span></code> is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>: </p> <div class="informaltable"><table class="table"> <colgroup> <col> <col> <col> </colgroup> <thead><tr> <th> <p> expression </p> </th> <th> <p> return type </p> </th> <th> <p> notes </p> </th> </tr></thead> <tbody> <tr> <td> <p> <code class="computeroutput"><span class="identifier">a</span><span class="special">(</span><span class="identifier">size</span><span class="special">)</span></code> </p> </td> <td> </td> <td> <p> creates a stack allocator </p> </td> </tr> <tr> <td> <p> <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">()</span></code> </p> </td> <td> <p> <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a> </p> </td> <td> <p> creates a stack </p> </td> </tr> <tr> <td> <p> <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">sctx</span><span class="special">)</span></code> </p> </td> <td> <p> <code class="computeroutput"><span class="keyword">void</span></code> </p> </td> <td> <p> deallocates the stack created by <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">()</span></code> </p> </td> </tr> </tbody> </table></div> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> The implementation of <code class="computeroutput"><span class="identifier">allocate</span><span class="special">()</span></code> might include logic to protect against exceeding the context's available stack size rather than leaving it as undefined behaviour. </p></td></tr> </table></div> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> Calling <code class="computeroutput"><span class="identifier">deallocate</span><span class="special">()</span></code> with a <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a> not obtained from <code class="computeroutput"><span class="identifier">allocate</span><span class="special">()</span></code> results in undefined behaviour. </p></td></tr> </table></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> The memory for the stack is not required to be aligned; alignment takes place inside __econtext__. </p></td></tr> </table></div> <p> See also <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack.html" target="_top">Boost.Context stack allocation</a>. In particular, <code class="computeroutput"><span class="identifier">traits_type</span></code> methods are as described for <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_traits.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">stack_traits</span></code></a>. </p> <p> </p> <h5> <a name="class_protected_fixedsize_stack_bridgehead"></a> <span><a name="class_protected_fixedsize_stack"></a></span> <a class="link" href="stack.html#class_protected_fixedsize_stack">Class <code class="computeroutput">protected_fixedsize_stack</code></a> </h5> <p> </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> which models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>. It appends a guard page at the end of each stack to protect against exceeding the stack. If the guard page is accessed (read or write operation) a segmentation fault/access violation is generated by the operating system. </p> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> Using <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> is expensive. Launching a new fiber with a stack of this type incurs the overhead of setting the memory protection; once allocated, this stack is just as efficient to use as <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a>. </p></td></tr> </table></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> The appended <code class="computeroutput"><span class="identifier">guard</span> <span class="identifier">page</span></code> is <span class="bold"><strong>not</strong></span> mapped to physical memory, only virtual addresses are used. </p></td></tr> </table></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">protected_fixedsize</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">protected_fixedsize</span> <span class="special">{</span> <span class="identifier">protected_fixesize</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span> <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span> <span class="special">}</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="protected_fixedsize_allocate_bridgehead"></a> <span><a name="protected_fixedsize_allocate"></a></span> <a class="link" href="stack.html#protected_fixedsize_allocate">Member function <code class="computeroutput">allocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">size</span> <span class="special">&lt;=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code> bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </p></dd> </dl> </div> <p> </p> <h5> <a name="protected_fixesize_deallocate_bridgehead"></a> <span><a name="protected_fixesize_deallocate"></a></span> <a class="link" href="stack.html#protected_fixesize_deallocate">Member function <code class="computeroutput">deallocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span> <span class="special">&lt;=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Deallocates the stack space. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_pooled_fixedsize_stack_bridgehead"></a> <span><a name="class_pooled_fixedsize_stack"></a></span> <a class="link" href="stack.html#class_pooled_fixedsize_stack">Class <code class="computeroutput">pooled_fixedsize_stack</code></a> </h5> <p> </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_pooled_fixedsize_stack"><code class="computeroutput">pooled_fixedsize_stack</code></a> which models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>. In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> it does not append a guard page at the end of each stack. The memory is managed internally by <a href="http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">pool</span><span class="special">&lt;&gt;</span></code></a>. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">pooled_fixedsize_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">pooled_fixedsize_stack</span> <span class="special">{</span> <span class="identifier">pooled_fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">next_size</span> <span class="special">=</span> <span class="number">32</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_size</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span> <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span> <span class="special">}</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="pooled_fixedsize_bridgehead"></a> <span><a name="pooled_fixedsize"></a></span> <a class="link" href="stack.html#pooled_fixedsize">Constructor</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">pooled_fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">next_size</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_size</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">stack_size</span><span class="special">)</span></code> and <code class="computeroutput"><span class="number">0</span> <span class="special">&lt;</span> <span class="identifier">next_size</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Allocates memory of at least <code class="computeroutput"><span class="identifier">stack_size</span></code> bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. Argument <code class="computeroutput"><span class="identifier">next_size</span></code> determines the number of stacks to request from the system the first time that <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> needs to allocate system memory. The third argument <code class="computeroutput"><span class="identifier">max_size</span></code> controls how much memory might be allocated for stacks &#8212; a value of zero means no upper limit. </p></dd> </dl> </div> <p> </p> <h5> <a name="pooled_fixedsize_allocate_bridgehead"></a> <span><a name="pooled_fixedsize_allocate"></a></span> <a class="link" href="stack.html#pooled_fixedsize_allocate">Member function <code class="computeroutput">allocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">stack_size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Allocates memory of at least <code class="computeroutput"><span class="identifier">stack_size</span></code> bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </p></dd> </dl> </div> <p> </p> <h5> <a name="pooled_fixesize_deallocate_bridgehead"></a> <span><a name="pooled_fixesize_deallocate"></a></span> <a class="link" href="stack.html#pooled_fixesize_deallocate">Member function <code class="computeroutput">deallocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Deallocates the stack space. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> This stack allocator is not thread safe. </p></td></tr> </table></div> <p> </p> <h5> <a name="class_fixedsize_stack_bridgehead"></a> <span><a name="class_fixedsize_stack"></a></span> <a class="link" href="stack.html#class_fixedsize_stack">Class <code class="computeroutput">fixedsize_stack</code></a> </h5> <p> </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a> which models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>. In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> it does not append a guard page at the end of each stack. The memory is simply managed by <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">malloc</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">free</span><span class="special">()</span></code>. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">context</span><span class="special">/</span><span class="identifier">fixedsize_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">fixedsize_stack</span> <span class="special">{</span> <span class="identifier">fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span> <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span> <span class="special">}</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="fixedsize_allocate_bridgehead"></a> <span><a name="fixedsize_allocate"></a></span> <a class="link" href="stack.html#fixedsize_allocate">Member function <code class="computeroutput">allocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code> bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </p></dd> </dl> </div> <p> </p> <h5> <a name="fixesize_deallocate_bridgehead"></a> <span><a name="fixesize_deallocate"></a></span> <a class="link" href="stack.html#fixesize_deallocate">Member function <code class="computeroutput">deallocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Deallocates the stack space. </p></dd> </dl> </div> <p> <a name="segmented"></a></p> <h5> <a name="class_segmented_stack_bridgehead"></a> <span><a name="class_segmented_stack"></a></span> <a class="link" href="stack.html#class_segmented_stack">Class <code class="computeroutput">segmented_stack</code></a> </h5> <p> </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> supports usage of a <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a>, i.e. the stack grows on demand. The fiber is created with a minimal stack size which will be increased as required. Class <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>. In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> and <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a> it creates a stack which grows on demand. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Segmented stacks are currently only supported by <span class="bold"><strong>gcc</strong></span> from version <span class="bold"><strong>4.7</strong></span> and <span class="bold"><strong>clang</strong></span> from version <span class="bold"><strong>3.4</strong></span> onwards. In order to use a <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> <span class="bold"><strong>Boost.Fiber</strong></span> must be built with property <code class="computeroutput"><span class="identifier">segmented</span><span class="special">-</span><span class="identifier">stacks</span></code>, e.g. <span class="bold"><strong>toolset=gcc segmented-stacks=on</strong></span> and applying BOOST_USE_SEGMENTED_STACKS at b2/bjam command line. </p></td></tr> </table></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Segmented stacks can only be used with callcc() using property <code class="computeroutput"><span class="identifier">context</span><span class="special">-</span><span class="identifier">impl</span><span class="special">=</span><span class="identifier">ucontext</span></code>. </p></td></tr> </table></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">segmented_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">segmented_stack</span> <span class="special">{</span> <span class="identifier">segmented_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span> <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span> <span class="special">}</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="segmented_allocate_bridgehead"></a> <span><a name="segmented_allocate"></a></span> <a class="link" href="stack.html#segmented_allocate">Member function <code class="computeroutput">allocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code> bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack. </p></dd> </dl> </div> <p> </p> <h5> <a name="segmented_deallocate_bridgehead"></a> <span><a name="segmented_deallocate"></a></span> <a class="link" href="stack.html#segmented_deallocate">Member function <code class="computeroutput">deallocate</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Deallocates the stack space. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> If the library is compiled for segmented stacks, <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> is the only available stack allocator. </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="scheduling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack/valgrind.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/integration.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Sharing a Thread with Another Main Loop</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="when_any/when_all_functionality/when_all__heterogeneous_types.html" title="when_all, heterogeneous types"> <link rel="next" href="integration/overview.html" title="Overview"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any/when_all_functionality/when_all__heterogeneous_types.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="integration/overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.integration"></a><a name="integration"></a><a class="link" href="integration.html" title="Sharing a Thread with Another Main Loop">Sharing a Thread with Another Main Loop</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="integration/overview.html">Overview</a></span></dt> <dt><span class="section"><a href="integration/event_driven_program.html">Event-Driven Program</a></span></dt> <dt><span class="section"><a href="integration/embedded_main_loop.html">Embedded Main Loop</a></span></dt> <dt><span class="section"><a href="integration/deeper_dive_into___boost_asio__.html">Deeper Dive into <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a></span></dt> </dl></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any/when_all_functionality/when_all__heterogeneous_types.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="integration/overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/custom.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Customization</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="tuning.html" title="Tuning"> <link rel="next" href="rationale.html" title="Rationale"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="tuning.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rationale.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.custom"></a><a name="custom"></a><a class="link" href="custom.html" title="Customization">Customization</a> </h2></div></div></div> <h4> <a name="fiber.custom.h0"></a> <span><a name="fiber.custom.overview"></a></span><a class="link" href="custom.html#fiber.custom.overview">Overview</a> </h4> <p> As noted in the <a class="link" href="scheduling.html#scheduling">Scheduling</a> section, by default <span class="bold"><strong>Boost.Fiber</strong></span> uses its own <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> scheduler for each thread. To control the way <span class="bold"><strong>Boost.Fiber</strong></span> schedules ready fibers on a particular thread, in general you must follow several steps. This section discusses those steps, whereas <a class="link" href="scheduling.html#scheduling">Scheduling</a> serves as a reference for the classes involved. </p> <p> The library's fiber manager keeps track of suspended (blocked) fibers. Only when a fiber becomes ready to run is it passed to the scheduler. Of course, if there are fewer than two ready fibers, the scheduler's job is trivial. Only when there are two or more ready fibers does the particular scheduler implementation start to influence the overall sequence of fiber execution. </p> <p> In this section we illustrate a simple custom scheduler that honors an integer fiber priority. We will implement it such that a fiber with higher priority is preferred over a fiber with lower priority. Any fibers with equal priority values are serviced on a round-robin basis. </p> <p> The full source code for the examples below is found in <a href="../../../examples/priority.cpp" target="_top">priority.cpp</a>. </p> <h4> <a name="fiber.custom.h1"></a> <span><a name="fiber.custom.custom_property_class"></a></span><a class="link" href="custom.html#fiber.custom.custom_property_class">Custom Property Class</a> </h4> <p> The first essential point is that we must associate an integer priority with each fiber.<sup>[<a name="fiber.custom.f0" href="#ftn.fiber.custom.f0" class="footnote">11</a>]</sup> </p> <p> One might suggest deriving a custom <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> subclass to store such properties. There are a couple of reasons for the present mechanism. </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> <span class="bold"><strong>Boost.Fiber</strong></span> provides a number of different ways to launch a fiber. (Consider <a class="link" href="synchronization/futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a>.) Higher-level libraries might introduce additional such wrapper functions. A custom scheduler must associate its custom properties with <span class="emphasis"><em>every</em></span> fiber in the thread, not only the ones explicitly launched by instantiating a custom <code class="computeroutput"><span class="identifier">fiber</span></code> subclass. </li> <li class="listitem"> Consider a large existing program that launches fibers in many different places in the code. We discover a need to introduce a custom scheduler for a particular thread. If supporting that scheduler's custom properties required a particular <code class="computeroutput"><span class="identifier">fiber</span></code> subclass, we would have to hunt down and modify every place that launches a fiber on that thread. </li> <li class="listitem"> The <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> class is actually just a handle to internal <a class="link" href="scheduling.html#class_context"><code class="computeroutput">context</code></a> data. A subclass of <code class="computeroutput"><span class="identifier">fiber</span></code> would not add data to <code class="computeroutput"><span class="identifier">context</span></code>. </li> </ol></div> <p> The present mechanism allows you to <span class="quote">&#8220;<span class="quote">drop in</span>&#8221;</span> a custom scheduler with its attendant custom properties <span class="emphasis"><em>without</em></span> altering the rest of your application. </p> <p> Instead of deriving a custom scheduler fiber properties subclass from <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a>, you must instead derive it from <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a>. </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">priority_props</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber_properties</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">priority_props</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx</span><span class="special">):</span> <span class="identifier">fiber_properties</span><span class="special">(</span> <span class="identifier">ctx</span><span class="special">),</span> <a class="co" name="fiber.custom.c0" href="custom.html#fiber.custom.c1"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> <span class="identifier">priority_</span><span class="special">(</span> <span class="number">0</span><span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="keyword">int</span> <span class="identifier">get_priority</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">priority_</span><span class="special">;</span> <a class="co" name="fiber.custom.c2" href="custom.html#fiber.custom.c3"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> <span class="special">}</span> <span class="comment">// Call this method to alter priority, because we must notify</span> <span class="comment">// priority_scheduler of any change.</span> <span class="keyword">void</span> <span class="identifier">set_priority</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">p</span><span class="special">)</span> <span class="special">{</span> <a class="co" name="fiber.custom.c4" href="custom.html#fiber.custom.c5"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> <span class="comment">// Of course, it's only worth reshuffling the queue and all if we're</span> <span class="comment">// actually changing the priority.</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">p</span> <span class="special">!=</span> <span class="identifier">priority_</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">priority_</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">;</span> <span class="identifier">notify</span><span class="special">();</span> <span class="special">}</span> <span class="special">}</span> <span class="comment">// The fiber name of course is solely for purposes of this example</span> <span class="comment">// program; it has nothing to do with implementing scheduler priority.</span> <span class="comment">// This is a public data member -- not requiring set/get access methods --</span> <span class="comment">// because we need not inform the scheduler of any change.</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">name</span><span class="special">;</span> <a class="co" name="fiber.custom.c6" href="custom.html#fiber.custom.c7"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> <span class="keyword">private</span><span class="special">:</span> <span class="keyword">int</span> <span class="identifier">priority_</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <div class="calloutlist"><table border="0" summary="Callout list"> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c1"></a><a href="#fiber.custom.c0"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> <td valign="top" align="left"><p> Your subclass constructor must accept a <code class="literal"><a class="link" href="scheduling.html#class_context"><code class="computeroutput">context</code></a>*</code> and pass it to the <code class="computeroutput"><span class="identifier">fiber_properties</span></code> constructor. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c3"></a><a href="#fiber.custom.c2"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> <td valign="top" align="left"><p> Provide read access methods at your own discretion. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c5"></a><a href="#fiber.custom.c4"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> <td valign="top" align="left"><p> It's important to call <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> on any change in a property that can affect the scheduler's behavior. Therefore, such modifications should only be performed through an access method. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c7"></a><a href="#fiber.custom.c6"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td> <td valign="top" align="left"><p> A property that does not affect the scheduler does not need access methods. </p></td> </tr> </table></div> <h4> <a name="fiber.custom.h2"></a> <span><a name="fiber.custom.custom_scheduler_class"></a></span><a class="link" href="custom.html#fiber.custom.custom_scheduler_class">Custom Scheduler Class</a> </h4> <p> Now we can derive a custom scheduler from <a class="link" href="scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a>, specifying our custom property class <code class="computeroutput"><span class="identifier">priority_props</span></code> as the template parameter. </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">priority_scheduler</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">algorithm_with_properties</span><span class="special">&lt;</span> <span class="identifier">priority_props</span> <span class="special">&gt;</span> <span class="special">{</span> <span class="keyword">private</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">scheduler</span><span class="special">::</span><span class="identifier">ready_queue_type</span><a class="co" name="fiber.custom.c8" href="custom.html#fiber.custom.c9"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> <span class="identifier">rqueue_t</span><span class="special">;</span> <span class="identifier">rqueue_t</span> <span class="identifier">rqueue_</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx_</span><span class="special">{};</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span> <span class="identifier">cnd_</span><span class="special">{};</span> <span class="keyword">bool</span> <span class="identifier">flag_</span><span class="special">{</span> <span class="keyword">false</span> <span class="special">};</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">priority_scheduler</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">rqueue_</span><span class="special">()</span> <span class="special">{</span> <span class="special">}</span> <span class="comment">// For a subclass of algorithm_with_properties&lt;&gt;, it's important to</span> <span class="comment">// override the correct awakened() overload.</span> <a class="co" name="fiber.custom.c10" href="custom.html#fiber.custom.c11"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx</span><span class="special">,</span> <span class="identifier">priority_props</span> <span class="special">&amp;</span> <span class="identifier">props</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">ctx_priority</span> <span class="special">=</span> <span class="identifier">props</span><span class="special">.</span><span class="identifier">get_priority</span><span class="special">();</span> <a class="co" name="fiber.custom.c12" href="custom.html#fiber.custom.c13"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> <span class="comment">// With this scheduler, fibers with higher priority values are</span> <span class="comment">// preferred over fibers with lower priority values. But fibers with</span> <span class="comment">// equal priority values are processed in round-robin fashion. So when</span> <span class="comment">// we're handed a new context*, put it at the end of the fibers</span> <span class="comment">// with that same priority. In other words: search for the first fiber</span> <span class="comment">// in the queue with LOWER priority, and insert before that one.</span> <span class="identifier">rqueue_t</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">i</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="special">[</span><span class="identifier">ctx_priority</span><span class="special">,</span><span class="keyword">this</span><span class="special">](</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">properties</span><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">c</span> <span class="special">).</span><span class="identifier">get_priority</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">ctx_priority</span><span class="special">;</span> <span class="special">}));</span> <span class="comment">// Now, whether or not we found a fiber with lower priority,</span> <span class="comment">// insert this new fiber here.</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">i</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">ctx</span><span class="special">);</span> <span class="special">}</span> <a class="co" name="fiber.custom.c14" href="custom.html#fiber.custom.c15"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a><span class="keyword">virtual</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="comment">// if ready queue is empty, just tell caller</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">front</span><span class="special">()</span> <span class="special">);</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">pop_front</span><span class="special">();</span> <span class="keyword">return</span> <span class="identifier">ctx</span><span class="special">;</span> <span class="special">}</span> <a class="co" name="fiber.custom.c16" href="custom.html#fiber.custom.c17"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">return</span> <span class="special">!</span> <span class="identifier">rqueue_</span><span class="special">.</span><span class="identifier">empty</span><span class="special">();</span> <span class="special">}</span> <a class="co" name="fiber.custom.c18" href="custom.html#fiber.custom.c19"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">property_change</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx</span><span class="special">,</span> <span class="identifier">priority_props</span> <span class="special">&amp;</span> <span class="identifier">props</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="comment">// Although our priority_props class defines multiple properties, only</span> <span class="comment">// one of them (priority) actually calls notify() when changed. The</span> <span class="comment">// point of a property_change() override is to reshuffle the ready</span> <span class="comment">// queue according to the updated priority value.</span> <span class="comment">// 'ctx' might not be in our queue at all, if caller is changing the</span> <span class="comment">// priority of (say) the running fiber. If it's not there, no need to</span> <span class="comment">// move it: we'll handle it next time it hits awakened().</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">ctx</span><span class="special">-&gt;</span><span class="identifier">ready_is_linked</span><span class="special">())</span> <span class="special">{</span> <a class="co" name="fiber.custom.c20" href="custom.html#fiber.custom.c21"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> <span class="keyword">return</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// Found ctx: unlink it</span> <span class="identifier">ctx</span><span class="special">-&gt;</span><span class="identifier">ready_unlink</span><span class="special">();</span> <span class="comment">// Here we know that ctx was in our ready queue, but we've unlinked</span> <span class="comment">// it. We happen to have a method that will (re-)add a context* to the</span> <span class="comment">// right place in the ready queue.</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">ctx</span><span class="special">,</span> <span class="identifier">props</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">time_point</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">::</span><span class="identifier">max</span><span class="special">)()</span> <span class="special">==</span> <span class="identifier">time_point</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_</span><span class="special">);</span> <span class="identifier">cnd_</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="special">[</span><span class="keyword">this</span><span class="special">](){</span> <span class="keyword">return</span> <span class="identifier">flag_</span><span class="special">;</span> <span class="special">});</span> <span class="identifier">flag_</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_</span><span class="special">);</span> <span class="identifier">cnd_</span><span class="special">.</span><span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">time_point</span><span class="special">,</span> <span class="special">[</span><span class="keyword">this</span><span class="special">](){</span> <span class="keyword">return</span> <span class="identifier">flag_</span><span class="special">;</span> <span class="special">});</span> <span class="identifier">flag_</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_</span><span class="special">);</span> <span class="identifier">flag_</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span> <span class="identifier">cnd_</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span> <span class="special">}</span> <span class="special">};</span> </pre> <p> </p> <div class="calloutlist"><table border="0" summary="Callout list"> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c9"></a><a href="#fiber.custom.c8"><img src="../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> <td valign="top" align="left"><p> See <a class="link" href="scheduling.html#ready_queue_t">ready_queue_t</a>. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c11"></a><a href="#fiber.custom.c10"><img src="../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> <td valign="top" align="left"><p> You must override the <a class="link" href="scheduling.html#algorithm_with_properties_awakened"><code class="computeroutput">algorithm_with_properties::awakened()</code></a> method. This is how your scheduler receives notification of a fiber that has become ready to run. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c13"></a><a href="#fiber.custom.c12"><img src="../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> <td valign="top" align="left"><p> <code class="computeroutput"><span class="identifier">props</span></code> is the instance of priority_props associated with the passed fiber <code class="computeroutput"><span class="identifier">ctx</span></code>. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c15"></a><a href="#fiber.custom.c14"><img src="../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td> <td valign="top" align="left"><p> You must override the <a class="link" href="scheduling.html#algorithm_with_properties_pick_next"><code class="computeroutput">algorithm_with_properties::pick_next()</code></a> method. This is how your scheduler actually advises the fiber manager of the next fiber to run. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c17"></a><a href="#fiber.custom.c16"><img src="../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> </p></td> <td valign="top" align="left"><p> You must override <a class="link" href="scheduling.html#algorithm_with_properties_has_ready_fibers"><code class="computeroutput">algorithm_with_properties::has_ready_fibers()</code></a> to inform the fiber manager of the state of your ready queue. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c19"></a><a href="#fiber.custom.c18"><img src="../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> </p></td> <td valign="top" align="left"><p> Overriding <a class="link" href="scheduling.html#algorithm_with_properties_property_change"><code class="computeroutput">algorithm_with_properties::property_change()</code></a> is optional. This override handles the case in which the running fiber changes the priority of another ready fiber: a fiber already in our queue. In that case, move the updated fiber within the queue. </p></td> </tr> <tr> <td width="5%" valign="top" align="left"><p><a name="fiber.custom.c21"></a><a href="#fiber.custom.c20"><img src="../../../../../doc/src/images/callouts/7.png" alt="7" border="0"></a> </p></td> <td valign="top" align="left"><p> Your <code class="computeroutput"><span class="identifier">property_change</span><span class="special">()</span></code> override must be able to handle the case in which the passed <code class="computeroutput"><span class="identifier">ctx</span></code> is not in your ready queue. It might be running, or it might be blocked. </p></td> </tr> </table></div> <p> Our example <code class="computeroutput"><span class="identifier">priority_scheduler</span></code> doesn't override <a class="link" href="scheduling.html#algorithm_with_properties_new_properties"><code class="computeroutput">algorithm_with_properties::new_properties()</code></a>: we're content with allocating <code class="computeroutput"><span class="identifier">priority_props</span></code> instances on the heap. </p> <h4> <a name="fiber.custom.h3"></a> <span><a name="fiber.custom.replace_default_scheduler"></a></span><a class="link" href="custom.html#fiber.custom.replace_default_scheduler">Replace Default Scheduler</a> </h4> <p> You must call <a class="link" href="fiber_mgmt/fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a> at the start of each thread on which you want <span class="bold"><strong>Boost.Fiber</strong></span> to use your custom scheduler rather than its own default <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a>. Specifically, you must call <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code> before performing any other <span class="bold"><strong>Boost.Fiber</strong></span> operations on that thread. </p> <p> </p> <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">argv</span><span class="special">[])</span> <span class="special">{</span> <span class="comment">// make sure we use our priority_scheduler rather than default round_robin</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">priority_scheduler</span> <span class="special">&gt;();</span> <span class="special">...</span> <span class="special">}</span> </pre> <p> </p> <h4> <a name="fiber.custom.h4"></a> <span><a name="fiber.custom.use_properties"></a></span><a class="link" href="custom.html#fiber.custom.use_properties">Use Properties</a> </h4> <p> The running fiber can access its own <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a> subclass instance by calling <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_properties"><code class="computeroutput">this_fiber::properties()</code></a>. Although <code class="computeroutput"><span class="identifier">properties</span><span class="special">&lt;&gt;()</span></code> is a nullary function, you must pass, as a template parameter, the <code class="computeroutput"><span class="identifier">fiber_properties</span></code> subclass. </p> <p> </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">properties</span><span class="special">&lt;</span> <span class="identifier">priority_props</span> <span class="special">&gt;().</span><span class="identifier">name</span> <span class="special">=</span> <span class="string">"main"</span><span class="special">;</span> </pre> <p> </p> <p> Given a <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> instance still connected with a running fiber (that is, not <a class="link" href="fiber_mgmt/fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a>ed), you may access that fiber's properties using <a class="link" href="fiber_mgmt/fiber.html#fiber_properties"><code class="computeroutput">fiber::properties()</code></a>. As with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">properties</span><span class="special">&lt;&gt;()</span></code>, you must pass your <code class="computeroutput"><span class="identifier">fiber_properties</span></code> subclass as the template parameter. </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">launch</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">func</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">priority</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">func</span><span class="special">);</span> <span class="identifier">priority_props</span> <span class="special">&amp;</span> <span class="identifier">props</span><span class="special">(</span> <span class="identifier">fiber</span><span class="special">.</span><span class="identifier">properties</span><span class="special">&lt;</span> <span class="identifier">priority_props</span> <span class="special">&gt;()</span> <span class="special">);</span> <span class="identifier">props</span><span class="special">.</span><span class="identifier">name</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">;</span> <span class="identifier">props</span><span class="special">.</span><span class="identifier">set_priority</span><span class="special">(</span> <span class="identifier">priority</span><span class="special">);</span> <span class="keyword">return</span> <span class="identifier">fiber</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> Launching a new fiber schedules that fiber as ready, but does <span class="emphasis"><em>not</em></span> immediately enter its <span class="emphasis"><em>fiber-function</em></span>. The current fiber retains control until it blocks (or yields, or terminates) for some other reason. As shown in the <code class="computeroutput"><span class="identifier">launch</span><span class="special">()</span></code> function above, it is reasonable to launch a fiber and immediately set relevant properties -- such as, for instance, its priority. Your custom scheduler can then make use of this information next time the fiber manager calls <a class="link" href="scheduling.html#algorithm_with_properties_pick_next"><code class="computeroutput">algorithm_with_properties::pick_next()</code></a>. </p> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.custom.f0" href="#fiber.custom.f0" class="para">11</a>] </sup> A previous version of the Fiber library implicitly tracked an int priority for each fiber, even though the default scheduler ignored it. This has been dropped, since the library now supports arbitrary scheduler-specific fiber properties. </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="tuning.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rationale.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/worker.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Running with worker threads</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="gpu_computing/hip.html" title="ROCm/HIP"> <link rel="next" href="performance.html" title="Performance"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.worker"></a><a name="worker"></a><a class="link" href="worker.html" title="Running with worker threads">Running with worker threads</a> </h2></div></div></div> <h4> <a name="fiber.worker.h0"></a> <span><a name="fiber.worker.keep_workers_running"></a></span><a class="link" href="worker.html#fiber.worker.keep_workers_running">Keep workers running</a> </h4> <p> If a worker thread is used but no fiber is created or parts of the framework (like <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>) are touched, then no fiber scheduler is instantiated. </p> <pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="special">[]{</span> <span class="comment">// fiber scheduler not instantiated</span> <span class="special">});</span> <span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> If <span class="emphasis"><em>use_scheduling_algorithm&lt;&gt;()</em></span> is invoked, the fiber scheduler is created. If the worker thread simply returns, destroys the scheduler and terminates. </p> <pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="special">[]{</span> <span class="comment">// fiber scheduler created</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">my_fiber_scheduler</span><span class="special">&gt;();</span> <span class="special">});</span> <span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> In order to keep the worker thread running, the fiber associated with the thread stack (which is called <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber) is blocked. For instance the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber might wait on a <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a>. For a gracefully shutdown <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> is signalled and the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber returns. The scheduler gets destructed if all fibers of the worker thread have been terminated. </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span> <span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span> <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span> <span class="comment">// suspend till signalled</span> <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span> <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span> <span class="special">});</span> <span class="comment">// signal termination</span> <span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span> <span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <h4> <a name="fiber.worker.h1"></a> <span><a name="fiber.worker.processing_tasks"></a></span><a class="link" href="worker.html#fiber.worker.processing_tasks">Processing tasks</a> </h4> <p> Tasks can be transferred via channels. The worker thread runs a pool of fibers that dequeue and executed tasks from the channel. The termination is signalled via closing the channel. </p> <pre class="programlisting"><span class="keyword">using</span> <span class="identifier">task</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()&gt;;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span><span class="identifier">task</span><span class="special">&gt;</span> <span class="identifier">ch</span><span class="special">{</span><span class="number">1024</span><span class="special">};</span> <span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span> <span class="comment">// create pool of fibers</span> <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span><span class="special">;</span> <span class="identifier">i</span><span class="special">&lt;</span><span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">{</span> <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span> <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span> <span class="comment">// dequeue and process tasks</span> <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span> <span class="identifier">tsk</span><span class="special">();</span> <span class="special">}</span> <span class="special">}}.</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span> <span class="comment">// dequeue and process tasks</span> <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span> <span class="identifier">tsk</span><span class="special">();</span> <span class="special">}</span> <span class="special">});</span> <span class="comment">// feed channel with tasks</span> <span class="identifier">ch</span><span class="special">.</span><span class="identifier">push</span><span class="special">([]{</span> <span class="special">...</span> <span class="special">});</span> <span class="special">...</span> <span class="comment">// signal termination</span> <span class="identifier">ch</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span> <span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> An alternative is to use a work-stealing scheduler. This kind of scheduling algorithm a worker thread steals fibers from the ready-queue of other worker threads if its own ready-queue is empty. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Wait till all worker threads have registered the work-stealing scheduling algorithm. </p></td></tr> </table></div> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span> <span class="comment">// start wotrker-thread first</span> <span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span> <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span> <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span> <span class="comment">// suspend main-fiber from the worker thread</span> <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span> <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span> <span class="special">});</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span> <span class="comment">// create fibers with tasks</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">{[]{</span> <span class="special">...</span> <span class="special">}};</span> <span class="special">...</span> <span class="comment">// signal termination</span> <span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span> <span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> Because the TIB (thread information block on Windows) is not fully described in the MSDN, it might be possible that not all required TIB-parts are swapped. Using WinFiber implementation might be an alternative (see documentation about <a href="http://www.boost.org/doc/libs/1_65_1/libs/context/doc/html/context/cc/implementations__fcontext_t__ucontext_t_and_winfiber.html" target="_top"><span class="emphasis"><em>implementations fcontext_t, ucontext_t and WinFiber of boost.context</em></span></a>). </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/fls.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Fiber local storage</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="synchronization/futures/packaged_task.html" title="Template packaged_task&lt;&gt;"> <link rel="next" href="migration.html" title="Migrating fibers between threads"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="synchronization/futures/packaged_task.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="migration.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.fls"></a><a class="link" href="fls.html" title="Fiber local storage">Fiber local storage</a> </h2></div></div></div> <h4> <a name="fiber.fls.h0"></a> <span><a name="fiber.fls.synopsis"></a></span><a class="link" href="fls.html#fiber.fls.synopsis">Synopsis</a> </h4> <p> Fiber local storage allows a separate instance of a given data item for each fiber. </p> <h4> <a name="fiber.fls.h1"></a> <span><a name="fiber.fls.cleanup_at_fiber_exit"></a></span><a class="link" href="fls.html#fiber.fls.cleanup_at_fiber_exit">Cleanup at fiber exit</a> </h4> <p> When a fiber exits, the objects associated with each <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> instance are destroyed. By default, the object pointed to by a pointer <code class="computeroutput"><span class="identifier">p</span></code> is destroyed by invoking <code class="computeroutput"><span class="keyword">delete</span> <span class="identifier">p</span></code>, but this can be overridden for a specific instance of <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> by providing a cleanup routine <code class="computeroutput"><span class="identifier">func</span></code> to the constructor. In this case, the object is destroyed by invoking <code class="computeroutput"><span class="identifier">func</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span></code>. The cleanup functions are called in an unspecified order. </p> <p> </p> <h5> <a name="class_fiber_specific_ptr_bridgehead"></a> <span><a name="class_fiber_specific_ptr"></a></span> <a class="link" href="fls.html#class_fiber_specific_ptr">Class <code class="computeroutput">fiber_specific_ptr</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">fss</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">fiber_specific_ptr</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">element_type</span><span class="special">;</span> <span class="identifier">fiber_specific_ptr</span><span class="special">();</span> <span class="keyword">explicit</span> <span class="identifier">fiber_specific_ptr</span><span class="special">(</span> <span class="keyword">void</span><span class="special">(*</span><span class="identifier">fn</span><span class="special">)(</span><span class="identifier">T</span><span class="special">*)</span> <span class="special">);</span> <span class="special">~</span><span class="identifier">fiber_specific_ptr</span><span class="special">();</span> <span class="identifier">fiber_specific_ptr</span><span class="special">(</span> <span class="identifier">fiber_specific_ptr</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">fiber_specific_ptr</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">fiber_specific_ptr</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">T</span> <span class="special">*</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">T</span> <span class="special">*</span> <span class="keyword">operator</span><span class="special">-&gt;()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">T</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">T</span> <span class="special">*</span> <span class="identifier">release</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">reset</span><span class="special">(</span> <span class="identifier">T</span> <span class="special">*);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h4> <a name="fiber.fls.h2"></a> <span><a name="fiber.fls.constructor"></a></span><a class="link" href="fls.html#fiber.fls.constructor">Constructor</a> </h4> <pre class="programlisting"><span class="identifier">fiber_specific_ptr</span><span class="special">();</span> <span class="keyword">explicit</span> <span class="identifier">fiber_specific_ptr</span><span class="special">(</span> <span class="keyword">void</span><span class="special">(*</span><span class="identifier">fn</span><span class="special">)(</span><span class="identifier">T</span><span class="special">*)</span> <span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Requires:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">delete</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> is well-formed; <code class="computeroutput"><span class="identifier">fn</span><span class="special">(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">())</span></code> does not throw </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Construct a <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> object for storing a pointer to an object of type <code class="computeroutput"><span class="identifier">T</span></code> specific to each fiber. When <code class="computeroutput"><span class="identifier">reset</span><span class="special">()</span></code> is called, or the fiber exits, <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> calls <code class="computeroutput"><span class="identifier">fn</span><span class="special">(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">())</span></code>. If the no-arguments constructor is used, the default <code class="computeroutput"><span class="keyword">delete</span></code>-based cleanup function will be used to destroy the fiber-local objects. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs. </p></dd> </dl> </div> <h4> <a name="fiber.fls.h3"></a> <span><a name="fiber.fls.destructor"></a></span><a class="link" href="fls.html#fiber.fls.destructor">Destructor</a> </h4> <pre class="programlisting"><span class="special">~</span><span class="identifier">fiber_specific_ptr</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Requires:</span></dt> <dd><p> All the fiber specific instances associated to this <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> (except maybe the one associated to this fiber) must be nullptr. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Calls <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">reset</span><span class="special">()</span></code> to clean up the associated value for the current fiber, and destroys <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Remarks:</span></dt> <dd><p> The requirement is an implementation restriction. If the destructor promised to delete instances for all fibers, the implementation would be forced to maintain a list of all the fibers having an associated specific ptr, which is against the goal of fiber specific data. In general, a <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> should outlive the fibers that use it. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Care needs to be taken to ensure that any fibers still running after an instance of <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> has been destroyed do not call any member functions on that instance. </p></td></tr> </table></div> <p> </p> <h5> <a name="fiber_specific_ptr_get_bridgehead"></a> <span><a name="fiber_specific_ptr_get"></a></span> <a class="link" href="fls.html#fiber_specific_ptr_get">Member function <code class="computeroutput">get</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">T</span> <span class="special">*</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> The pointer associated with the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> The initial value associated with an instance of <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a> is <code class="computeroutput"><span class="keyword">nullptr</span></code> for each fiber. </p></td></tr> </table></div> <p> </p> <h5> <a name="fiber_specific_ptr_operator_arrow_bridgehead"></a> <span><a name="fiber_specific_ptr_operator_arrow"></a></span> <a class="link" href="fls.html#fiber_specific_ptr_operator_arrow">Member function <code class="computeroutput">operator-&gt;</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">T</span> <span class="special">*</span> <span class="keyword">operator</span><span class="special">-&gt;()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Requires:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> is not <code class="computeroutput"><span class="keyword">nullptr</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_specific_ptr_operator_star_bridgehead"></a> <span><a name="fiber_specific_ptr_operator_star"></a></span> <a class="link" href="fls.html#fiber_specific_ptr_operator_star">Member function <code class="computeroutput">operator*</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">T</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Requires:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> is not <code class="computeroutput"><span class="keyword">nullptr</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="special">*(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">())</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_specific_ptr_release_bridgehead"></a> <span><a name="fiber_specific_ptr_release"></a></span> <a class="link" href="fls.html#fiber_specific_ptr_release">Member function <code class="computeroutput">release</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">T</span> <span class="special">*</span> <span class="identifier">release</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Return <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> and store <code class="computeroutput"><span class="keyword">nullptr</span></code> as the pointer associated with the current fiber without invoking the cleanup function. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()==</span><span class="keyword">nullptr</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_specific_ptr_reset_bridgehead"></a> <span><a name="fiber_specific_ptr_reset"></a></span> <a class="link" href="fls.html#fiber_specific_ptr_reset">Member function <code class="computeroutput">reset</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">reset</span><span class="special">(</span> <span class="identifier">T</span> <span class="special">*</span> <span class="identifier">new_value</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()!=</span><span class="identifier">new_value</span></code> and <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> is not <code class="computeroutput"><span class="keyword">nullptr</span></code>, invoke <code class="computeroutput"><span class="keyword">delete</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">fn</span><span class="special">(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">())</span></code> as appropriate. Store <code class="computeroutput"><span class="identifier">new_value</span></code> as the pointer associated with the current fiber. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()==</span><span class="identifier">new_value</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exception raised during cleanup of previous value. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="synchronization/futures/packaged_task.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="migration.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/callbacks.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Integrating Fibers with Asynchronous Callbacks</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="migration.html" title="Migrating fibers between threads"> <link rel="next" href="callbacks/overview.html" title="Overview"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="migration.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="callbacks/overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.callbacks"></a><a name="callbacks"></a><a class="link" href="callbacks.html" title="Integrating Fibers with Asynchronous Callbacks">Integrating Fibers with Asynchronous Callbacks</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="callbacks/overview.html">Overview</a></span></dt> <dt><span class="section"><a href="callbacks/return_errorcode.html">Return Errorcode</a></span></dt> <dt><span class="section"><a href="callbacks/success_or_exception.html">Success or Exception</a></span></dt> <dt><span class="section"><a href="callbacks/return_errorcode_or_data.html">Return Errorcode or Data</a></span></dt> <dt><span class="section"><a href="callbacks/data_or_exception.html">Data or Exception</a></span></dt> <dt><span class="section"><a href="callbacks/success_error_virtual_methods.html">Success/Error Virtual Methods</a></span></dt> <dt><span class="section"><a href="callbacks/then_there_s____boost_asio__.html">Then There&#8217;s <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a></span></dt> </dl></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="migration.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="callbacks/overview.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/rationale.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Rationale</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="custom.html" title="Customization"> <link rel="next" href="acknowledgements.html" title="Acknowledgments"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="custom.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a> </h2></div></div></div> <h4> <a name="fiber.rationale.h0"></a> <span><a name="fiber.rationale.preprocessor_defines"></a></span><a class="link" href="rationale.html#fiber.rationale.preprocessor_defines">preprocessor defines</a> </h4> <div class="table"> <a name="fiber.rationale.preopcessor_defines"></a><p class="title"><b>Table&#160;1.6.&#160;preopcessor defines</b></p> <div class="table-contents"><table class="table" summary="preopcessor defines"> <colgroup> <col> <col> </colgroup> <thead><tr> <th> </th> <th> </th> </tr></thead> <tbody> <tr> <td> <p> BOOST_FIBERS_NO_ATOMICS </p> </td> <td> <p> no <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;&gt;</span></code> used, inter-thread synchronization disabled </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_STD_MUTEX </p> </td> <td> <p> use <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span></code> as spinlock instead of default <code class="computeroutput"><span class="identifier">XCHG</span></code>-sinlock with backoff </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPIN_BACKOFF </p> </td> <td> <p> limit determines when to used <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code> instead of mnemonic <code class="computeroutput"><span class="identifier">pause</span><span class="special">/</span><span class="identifier">yield</span></code> during busy wait (apllies on to <code class="computeroutput"><span class="identifier">XCHG</span></code>-spinlock) </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SINGLE_CORE </p> </td> <td> <p> allways call <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code> without backoff during busy wait (apllies on to <code class="computeroutput"><span class="identifier">XCHG</span></code>-spinlock) </p> </td> </tr> </tbody> </table></div> </div> <br class="table-break"><h4> <a name="fiber.rationale.h1"></a> <span><a name="fiber.rationale.distinction_between_coroutines_and_fibers"></a></span><a class="link" href="rationale.html#fiber.rationale.distinction_between_coroutines_and_fibers">distinction between coroutines and fibers</a> </h4> <p> The fiber library extends the coroutine library by adding a scheduler and synchronization mechanisms. </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"> a coroutine yields </li> <li class="listitem"> a fiber blocks </li> </ul></div> <p> When a coroutine yields, it passes control directly to its caller (or, in the case of symmetric coroutines, a designated other coroutine). When a fiber blocks, it implicitly passes control to the fiber scheduler. Coroutines have no scheduler because they need no scheduler.<sup>[<a name="fiber.rationale.f0" href="#ftn.fiber.rationale.f0" class="footnote">12</a>]</sup>. </p> <h4> <a name="fiber.rationale.h2"></a> <span><a name="fiber.rationale.what_about_transactional_memory"></a></span><a class="link" href="rationale.html#fiber.rationale.what_about_transactional_memory">what about transactional memory</a> </h4> <p> GCC supports transactional memory since version 4.7. Unfortunately tests show that transactional memory is slower (ca. 4x) than spinlocks using atomics. Once transactional memory is improved (supporting hybrid tm), spinlocks will be replaced by __transaction_atomic{} statements surrounding the critical sections. </p> <h4> <a name="fiber.rationale.h3"></a> <span><a name="fiber.rationale.synchronization_between_fibers_running_in_different_threads"></a></span><a class="link" href="rationale.html#fiber.rationale.synchronization_between_fibers_running_in_different_threads">synchronization between fibers running in different threads</a> </h4> <p> Synchronization classes from <a href="http://www.boost.org/doc/libs/release/libs/thread/index.html" target="_top">Boost.Thread</a> block the entire thread. In contrast, the synchronization classes from <span class="bold"><strong>Boost.Fiber</strong></span> block only specific fibers, so that the scheduler can still keep the thread busy running other fibers in the meantime. The synchronization classes from <span class="bold"><strong>Boost.Fiber</strong></span> are designed to be thread-safe, i.e. it is possible to synchronize fibers running in different threads as well as fibers running in the same thread. (However, there is a build option to disable cross-thread fiber synchronization support; see <a class="link" href="overview.html#cross_thread_sync">this description</a>.) </p> <a name="spurious_wakeup"></a><h4> <a name="fiber.rationale.h4"></a> <span><a name="fiber.rationale.spurious_wakeup"></a></span><a class="link" href="rationale.html#fiber.rationale.spurious_wakeup">spurious wakeup</a> </h4> <p> Spurious wakeup can happen when using <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code></a>: the condition variable appears to be have been signaled while the awaited condition may still be false. Spurious wakeup can happen repeatedly and is caused on some multiprocessor systems where making <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code> wakeup completely predictable would slow down all <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code> operations.<sup>[<a name="fiber.rationale.f1" href="#ftn.fiber.rationale.f1" class="footnote">13</a>]</sup> </p> <p> <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> is not subject to spurious wakeup. Nonetheless it is prudent to test the business-logic condition in a <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> loop &#8212; or, equivalently, use one of the <code class="computeroutput"><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lock</span><span class="special">,</span> <span class="identifier">predicate</span> <span class="special">)</span></code> overloads. </p> <p> See also <a class="link" href="synchronization/conditions.html#condition_variable_spurious_wakeups">No Spurious Wakeups</a>. </p> <h4> <a name="fiber.rationale.h5"></a> <span><a name="fiber.rationale.migrating_fibers_between_threads"></a></span><a class="link" href="rationale.html#fiber.rationale.migrating_fibers_between_threads">migrating fibers between threads</a> </h4> <p> Support for migrating fibers between threads has been integrated. The user-defined scheduler must call <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a> on a fiber-context on the source thread and <a class="link" href="scheduling.html#context_attach"><code class="computeroutput">context::attach()</code></a> on the destination thread, passing the fiber-context to migrate. (For more information about custom schedulers, see <a class="link" href="custom.html#custom">Customization</a>.) Examples <code class="computeroutput"><span class="identifier">work_sharing</span></code> and <code class="computeroutput"><span class="identifier">work_stealing</span></code> in directory <code class="computeroutput"><span class="identifier">examples</span></code> might be used as a blueprint. </p> <p> See also <a class="link" href="migration.html#migration">Migrating fibers between threads</a>. </p> <h4> <a name="fiber.rationale.h6"></a> <span><a name="fiber.rationale.support_for_boost_asio"></a></span><a class="link" href="rationale.html#fiber.rationale.support_for_boost_asio">support for Boost.Asio</a> </h4> <p> Support for <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a>&#8217;s <span class="emphasis"><em>async-result</em></span> is not part of the official API. However, to integrate with a <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span></code></a>, see <a class="link" href="integration.html#integration">Sharing a Thread with Another Main Loop</a>. To interface smoothly with an arbitrary Asio async I/O operation, see <a class="link" href="callbacks/then_there_s____boost_asio__.html#callbacks_asio">Then There&#8217;s <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a>. </p> <h4> <a name="fiber.rationale.h7"></a> <span><a name="fiber.rationale.tested_compilers"></a></span><a class="link" href="rationale.html#fiber.rationale.tested_compilers">tested compilers</a> </h4> <p> The library was tested with GCC-5.1.1, Clang-3.6.0 and MSVC-14.0 in c++11-mode. </p> <h4> <a name="fiber.rationale.h8"></a> <span><a name="fiber.rationale.supported_architectures"></a></span><a class="link" href="rationale.html#fiber.rationale.supported_architectures">supported architectures</a> </h4> <p> <span class="bold"><strong>Boost.Fiber</strong></span> depends on <a href="http://www.boost.org/doc/libs/release/libs/context/index.html" target="_top">Boost.Context</a> - the list of supported architectures can be found <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/architectures.html" target="_top">here</a>. </p> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.rationale.f0" href="#fiber.rationale.f0" class="para">12</a>] </sup> <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4024.pdf" target="_top">'N4024: Distinguishing coroutines and fibers'</a> </p></div> <div class="footnote"><p><sup>[<a name="ftn.fiber.rationale.f1" href="#fiber.rationale.f1" class="para">13</a>] </sup> David R. Butenhof <span class="quote">&#8220;<span class="quote">Programming with POSIX Threads</span>&#8221;</span> </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="custom.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/speculation.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Specualtive execution</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="integration/deeper_dive_into___boost_asio__.html" title="Deeper Dive into Boost.Asio"> <link rel="next" href="numa.html" title="NUMA"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="integration/deeper_dive_into___boost_asio__.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="numa.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.speculation"></a><a name="speculation"></a><a class="link" href="speculation.html" title="Specualtive execution">Specualtive execution</a> </h2></div></div></div> <h4> <a name="fiber.speculation.h0"></a> <span><a name="fiber.speculation.hardware_transactional_memory"></a></span><a class="link" href="speculation.html#fiber.speculation.hardware_transactional_memory">Hardware transactional memory</a> </h4> <p> With help of hardware transactional memory multiple logical processors execute a critical region speculatively, e.g. without explicit synchronization.<br> If the transactional execution completes successfully, then all memory operations performed within the transactional region are commited without any inter-thread serialization.<br> When the optimistic execution fails, the processor aborts the transaction and discards all performed modifications.<br> In non-transactional code a single lock serializes the access to a critical region. With a transactional memory, multiple logical processor start a transaction and update the memory (the data) inside the ciritical region. Unless some logical processors try to update the same data, the transactions would always succeed. </p> <h4> <a name="fiber.speculation.h1"></a> <span><a name="fiber.speculation.intel_transactional_synchronisation_extensions__tsx_"></a></span><a class="link" href="speculation.html#fiber.speculation.intel_transactional_synchronisation_extensions__tsx_">Intel Transactional Synchronisation Extensions (TSX)</a> </h4> <p> TSX is Intel's implementation of hardware transactional memory in modern Intel processors<sup>[<a name="fiber.speculation.f0" href="#ftn.fiber.speculation.f0" class="footnote">7</a>]</sup>.<br> In TSX the hardware keeps track of which cachelines have been read from and which have been written to in a transaction. The cache-line size (64-byte) and the n-way set associative cache determine the maximum size of memory in a transaction. For instance if a transaction modifies 9 cache-lines at a processor with a 8-way set associative cache, the transaction will always be aborted. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> TXS is enabled if property <code class="computeroutput"><span class="identifier">htm</span><span class="special">=</span><span class="identifier">tsx</span></code> is specified at b2 command-line and <code class="computeroutput"><span class="identifier">BOOST_USE_TSX</span></code> is applied to the compiler. </p></td></tr> </table></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> A TSX-transaction will be aborted if the floating point state is modified inside a critical region. As a consequence floating point operations, e.g. store/load of floating point related registers during a fiber (context) switch are disabled. </p></td></tr> </table></div> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> TSX can not be used together with MSVC at this time! </p></td></tr> </table></div> <p> Boost.Fiber uses TSX-enabled spinlocks to protect critical regions (see section <a class="link" href="tuning.html#tuning">Tuning</a>). </p> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.speculation.f0" href="#fiber.speculation.f0" class="para">7</a>] </sup> intel.com: <a href="https://software.intel.com/en-us/node/695149" target="_top">Intel Transactional Synchronization Extensions</a> </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="integration/deeper_dive_into___boost_asio__.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="numa.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/nonblocking.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Integrating Fibers with Nonblocking I/O</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="callbacks/then_there_s____boost_asio__.html" title="Then There&#8217;s Boost.Asio"> <link rel="next" href="when_any.html" title="when_any / when_all functionality"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="callbacks/then_there_s____boost_asio__.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.nonblocking"></a><a name="nonblocking"></a><a class="link" href="nonblocking.html" title="Integrating Fibers with Nonblocking I/O">Integrating Fibers with Nonblocking I/O</a> </h2></div></div></div> <h4> <a name="fiber.nonblocking.h0"></a> <span><a name="fiber.nonblocking.overview"></a></span><a class="link" href="nonblocking.html#fiber.nonblocking.overview">Overview</a> </h4> <p> <span class="emphasis"><em>Nonblocking</em></span> I/O is distinct from <span class="emphasis"><em>asynchronous</em></span> I/O. A true async I/O operation promises to initiate the operation and notify the caller on completion, usually via some sort of callback (as described in <a class="link" href="callbacks.html#callbacks">Integrating Fibers with Asynchronous Callbacks</a>). </p> <p> In contrast, a nonblocking I/O operation refuses to start at all if it would be necessary to block, returning an error code such as <a href="http://man7.org/linux/man-pages/man3/errno.3.html" target="_top"><code class="computeroutput"><span class="identifier">EWOULDBLOCK</span></code></a>. The operation is performed only when it can complete immediately. In effect, the caller must repeatedly retry the operation until it stops returning <code class="computeroutput"><span class="identifier">EWOULDBLOCK</span></code>. </p> <p> In a classic event-driven program, it can be something of a headache to use nonblocking I/O. At the point where the nonblocking I/O is attempted, a return value of <code class="computeroutput"><span class="identifier">EWOULDBLOCK</span></code> requires the caller to pass control back to the main event loop, arranging to retry again on the next iteration. </p> <p> Worse, a nonblocking I/O operation might <span class="emphasis"><em>partially</em></span> succeed. That means that the relevant business logic must continue receiving control on every main loop iteration until all required data have been processed: a doubly-nested loop, implemented as a callback-driven state machine. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> can simplify this problem immensely. Once you have integrated with the application's main loop as described in <a class="link" href="integration.html#integration">Sharing a Thread with Another Main Loop</a>, waiting for the next main-loop iteration is as simple as calling <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. </p> <h4> <a name="fiber.nonblocking.h1"></a> <span><a name="fiber.nonblocking.example_nonblocking_api"></a></span><a class="link" href="nonblocking.html#fiber.nonblocking.example_nonblocking_api">Example Nonblocking API</a> </h4> <p> For purposes of illustration, consider this API: </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">NonblockingAPI</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">NonblockingAPI</span><span class="special">();</span> <span class="comment">// nonblocking operation: may return EWOULDBLOCK</span> <span class="keyword">int</span> <span class="identifier">read</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">desired</span><span class="special">);</span> <span class="special">...</span> <span class="special">};</span> </pre> <p> </p> <h4> <a name="fiber.nonblocking.h2"></a> <span><a name="fiber.nonblocking.polling_for_completion"></a></span><a class="link" href="nonblocking.html#fiber.nonblocking.polling_for_completion">Polling for Completion</a> </h4> <p> We can build a low-level wrapper around <code class="computeroutput"><span class="identifier">NonblockingAPI</span><span class="special">::</span><span class="identifier">read</span><span class="special">()</span></code> that shields its caller from ever having to deal with <code class="computeroutput"><span class="identifier">EWOULDBLOCK</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// guaranteed not to return EWOULDBLOCK</span> <span class="keyword">int</span> <span class="identifier">read_chunk</span><span class="special">(</span> <span class="identifier">NonblockingAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">desired</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">error</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">EWOULDBLOCK</span> <span class="special">==</span> <span class="special">(</span> <span class="identifier">error</span> <span class="special">=</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">read</span><span class="special">(</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">desired</span><span class="special">)</span> <span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="comment">// not ready yet -- try again on the next iteration of the</span> <span class="comment">// application's main loop</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">yield</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">return</span> <span class="identifier">error</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <h4> <a name="fiber.nonblocking.h3"></a> <span><a name="fiber.nonblocking.filling_all_desired_data"></a></span><a class="link" href="nonblocking.html#fiber.nonblocking.filling_all_desired_data">Filling All Desired Data</a> </h4> <p> Given <code class="computeroutput"><span class="identifier">read_chunk</span><span class="special">()</span></code>, we can straightforwardly iterate until we have all desired data: </p> <p> </p> <pre class="programlisting"><span class="comment">// keep reading until desired length, EOF or error</span> <span class="comment">// may return both partial data and nonzero error</span> <span class="keyword">int</span> <span class="identifier">read_desired</span><span class="special">(</span> <span class="identifier">NonblockingAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">desired</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// we're going to accumulate results into 'data'</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">chunk</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">error</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">length</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">desired</span> <span class="special">&amp;&amp;</span> <span class="special">(</span> <span class="identifier">error</span> <span class="special">=</span> <span class="identifier">read_chunk</span><span class="special">(</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">chunk</span><span class="special">,</span> <span class="identifier">desired</span> <span class="special">-</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">length</span><span class="special">()</span> <span class="special">)</span> <span class="special">)</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">append</span><span class="special">(</span> <span class="identifier">chunk</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">return</span> <span class="identifier">error</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> (Of <span class="emphasis"><em>course</em></span> there are more efficient ways to accumulate string data. That's not the point of this example.) </p> <h4> <a name="fiber.nonblocking.h4"></a> <span><a name="fiber.nonblocking.wrapping_it_up"></a></span><a class="link" href="nonblocking.html#fiber.nonblocking.wrapping_it_up">Wrapping it Up</a> </h4> <p> Finally, we can define a relevant exception: </p> <p> </p> <pre class="programlisting"><span class="comment">// exception class augmented with both partially-read data and errorcode</span> <span class="keyword">class</span> <span class="identifier">IncompleteRead</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">IncompleteRead</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">what</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">partial</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span> <span class="identifier">what</span><span class="special">),</span> <span class="identifier">partial_</span><span class="special">(</span> <span class="identifier">partial</span><span class="special">),</span> <span class="identifier">ec_</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">get_partial</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">partial_</span><span class="special">;</span> <span class="special">}</span> <span class="keyword">int</span> <span class="identifier">get_errorcode</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">ec_</span><span class="special">;</span> <span class="special">}</span> <span class="keyword">private</span><span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">partial_</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">ec_</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> and write a simple <code class="computeroutput"><span class="identifier">read</span><span class="special">()</span></code> function that either returns all desired data or throws <code class="computeroutput"><span class="identifier">IncompleteRead</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// read all desired data or throw IncompleteRead</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">read</span><span class="special">(</span> <span class="identifier">NonblockingAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">desired</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">data</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">ec</span><span class="special">(</span> <span class="identifier">read_desired</span><span class="special">(</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">desired</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// for present purposes, EOF isn't a failure</span> <span class="keyword">if</span> <span class="special">(</span> <span class="number">0</span> <span class="special">==</span> <span class="identifier">ec</span> <span class="special">||</span> <span class="identifier">EOF</span> <span class="special">==</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">data</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// oh oh, partial read</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">msg</span><span class="special">;</span> <span class="identifier">msg</span> <span class="special">&lt;&lt;</span> <span class="string">"NonblockingAPI::read() error "</span> <span class="special">&lt;&lt;</span> <span class="identifier">ec</span> <span class="special">&lt;&lt;</span> <span class="string">" after "</span> <span class="special">&lt;&lt;</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">length</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">desired</span> <span class="special">&lt;&lt;</span> <span class="string">" characters"</span><span class="special">;</span> <span class="keyword">throw</span> <span class="identifier">IncompleteRead</span><span class="special">(</span> <span class="identifier">msg</span><span class="special">.</span><span class="identifier">str</span><span class="special">(),</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <p> Once we can transparently wait for the next main-loop iteration using <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>, ordinary encapsulation Just Works. </p> <p> The source code above is found in <a href="../../../examples/adapt_nonblocking.cpp" target="_top">adapt_nonblocking.cpp</a>. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="callbacks/then_there_s____boost_asio__.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/when_any.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any / when_all functionality</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="nonblocking.html" title="Integrating Fibers with Nonblocking I/O"> <link rel="next" href="when_any/when_any.html" title="when_any"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="nonblocking.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any/when_any.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.when_any"></a><a name="when_any"></a><a class="link" href="when_any.html" title="when_any / when_all functionality">when_any / when_all functionality</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="when_any/when_any.html">when_any</a></span></dt> <dd><dl> <dt><span class="section"><a href="when_any/when_any/when_any__simple_completion.html">when_any, simple completion</a></span></dt> <dt><span class="section"><a href="when_any/when_any/when_any__return_value.html">when_any, return value</a></span></dt> <dt><span class="section"><a href="when_any/when_any/when_any__produce_first_outcome__whether_result_or_exception.html">when_any, produce first outcome, whether result or exception</a></span></dt> <dt><span class="section"><a href="when_any/when_any/when_any__produce_first_success.html">when_any, produce first success</a></span></dt> <dt><span class="section"><a href="when_any/when_any/when_any__heterogeneous_types.html">when_any, heterogeneous types</a></span></dt> <dt><span class="section"><a href="when_any/when_any/when_any__a_dubious_alternative.html">when_any, a dubious alternative</a></span></dt> </dl></dd> <dt><span class="section"><a href="when_any/when_all_functionality.html">when_all functionality</a></span></dt> <dd><dl> <dt><span class="section"><a href="when_any/when_all_functionality/when_all__simple_completion.html">when_all, simple completion</a></span></dt> <dt><span class="section"><a href="when_any/when_all_functionality/when_all__return_values.html">when_all, return values</a></span></dt> <dt><span class="section"><a href="when_any/when_all_functionality/when_all_until_first_exception.html">when_all until first exception</a></span></dt> <dt><span class="section"><a href="when_any/when_all_functionality/wait_all__collecting_all_exceptions.html">wait_all, collecting all exceptions</a></span></dt> <dt><span class="section"><a href="when_any/when_all_functionality/when_all__heterogeneous_types.html">when_all, heterogeneous types</a></span></dt> </dl></dd> </dl></div> <h4> <a name="fiber.when_any.h0"></a> <span><a name="fiber.when_any.overview"></a></span><a class="link" href="when_any.html#fiber.when_any.overview">Overview</a> </h4> <p> A bit of wisdom from the early days of computing still holds true today: prefer to model program state using the instruction pointer rather than with Boolean flags. In other words, if the program must <span class="quote">&#8220;<span class="quote">do something</span>&#8221;</span> and then do something almost the same, but with minor changes... perhaps parts of that something should be broken out as smaller separate functions, rather than introducing flags to alter the internal behavior of a monolithic function. </p> <p> To that we would add: prefer to describe control flow using C++ native constructs such as function calls, <code class="computeroutput"><span class="keyword">if</span></code>, <code class="computeroutput"><span class="keyword">while</span></code>, <code class="computeroutput"><span class="keyword">for</span></code>, <code class="computeroutput"><span class="keyword">do</span></code> et al. rather than as chains of callbacks. </p> <p> One of the great strengths of <span class="bold"><strong>Boost.Fiber</strong></span> is the flexibility it confers on the coder to restructure an application from chains of callbacks to straightforward C++ statement sequence, even when code in that fiber is in fact interleaved with code running in other fibers. </p> <p> There has been much recent discussion about the benefits of when_any and when_all functionality. When dealing with asynchronous and possibly unreliable services, these are valuable idioms. But of course when_any and when_all are closely tied to the use of chains of callbacks. </p> <p> This section presents recipes for achieving the same ends, in the context of a fiber that wants to <span class="quote">&#8220;<span class="quote">do something</span>&#8221;</span> when one or more other independent activities have completed. Accordingly, these are <code class="computeroutput"><span class="identifier">wait_something</span><span class="special">()</span></code> functions rather than <code class="computeroutput"><span class="identifier">when_something</span><span class="special">()</span></code> functions. The expectation is that the calling fiber asks to launch those independent activities, then waits for them, then sequentially proceeds with whatever processing depends on those results. </p> <p> The function names shown (e.g. <a class="link" href="when_any/when_any/when_any__simple_completion.html#wait_first_simple"><code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code></a>) are for illustrative purposes only, because all these functions have been bundled into a single source file. Presumably, if (say) <a class="link" href="when_any/when_any/when_any__produce_first_success.html#wait_first_success"><code class="computeroutput"><span class="identifier">wait_first_success</span><span class="special">()</span></code></a> best suits your application needs, you could introduce that variant with the name <code class="computeroutput"><span class="identifier">wait_any</span><span class="special">()</span></code>. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> The functions presented in this section accept variadic argument lists of task functions. Corresponding <code class="computeroutput"><span class="identifier">wait_something</span><span class="special">()</span></code> functions accepting a container of task functions are left as an exercise for the interested reader. Those should actually be simpler. Most of the complexity would arise from overloading the same name for both purposes. </p></td></tr> </table></div> <p> All the source code for this section is found in <a href="../../../examples/wait_stuff.cpp" target="_top">wait_stuff.cpp</a>. </p> <h4> <a name="fiber.when_any.h1"></a> <span><a name="fiber.when_any.example_task_function"></a></span><a class="link" href="when_any.html#fiber.when_any.example_task_function">Example Task Function</a> </h4> <p> <a name="wait_sleeper"></a>We found it convenient to model an asynchronous task using this function: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">T</span> <span class="identifier">sleeper_impl</span><span class="special">(</span> <span class="identifier">T</span> <span class="identifier">item</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">ms</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">thrw</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">descb</span><span class="special">,</span> <span class="identifier">funcb</span><span class="special">;</span> <span class="identifier">descb</span> <span class="special">&lt;&lt;</span> <span class="identifier">item</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">desc</span><span class="special">(</span> <span class="identifier">descb</span><span class="special">.</span><span class="identifier">str</span><span class="special">()</span> <span class="special">);</span> <span class="identifier">funcb</span> <span class="special">&lt;&lt;</span> <span class="string">" sleeper("</span> <span class="special">&lt;&lt;</span> <span class="identifier">item</span> <span class="special">&lt;&lt;</span> <span class="string">")"</span><span class="special">;</span> <span class="identifier">Verbose</span> <span class="identifier">v</span><span class="special">(</span> <span class="identifier">funcb</span><span class="special">.</span><span class="identifier">str</span><span class="special">()</span> <span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">sleep_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">milliseconds</span><span class="special">(</span> <span class="identifier">ms</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">thrw</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span> <span class="identifier">desc</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">return</span> <span class="identifier">item</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> with type-specific <code class="computeroutput"><span class="identifier">sleeper</span><span class="special">()</span></code> <span class="quote">&#8220;<span class="quote">front ends</span>&#8221;</span> for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>, <code class="computeroutput"><span class="keyword">double</span></code> and <code class="computeroutput"><span class="keyword">int</span></code>. </p> <p> <code class="computeroutput"><span class="identifier">Verbose</span></code> simply prints a message to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span></code> on construction and destruction. </p> <p> Basically: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> <code class="computeroutput"><span class="identifier">sleeper</span><span class="special">()</span></code> prints a start message; </li> <li class="listitem"> sleeps for the specified number of milliseconds; </li> <li class="listitem"> if <code class="computeroutput"><span class="identifier">thrw</span></code> is passed as <code class="computeroutput"><span class="keyword">true</span></code>, throws a string description of the passed <code class="computeroutput"><span class="identifier">item</span></code>; </li> <li class="listitem"> else returns the passed <code class="computeroutput"><span class="identifier">item</span></code>. </li> <li class="listitem"> On the way out, <code class="computeroutput"><span class="identifier">sleeper</span><span class="special">()</span></code> produces a stop message. </li> </ol></div> <p> This function will feature in the example calls to the various functions presented below. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="nonblocking.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any/when_any.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/synchronization.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Synchronization</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="stack/valgrind.html" title="Support for valgrind"> <link rel="next" href="synchronization/mutex_types.html" title="Mutex Types"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="stack/valgrind.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synchronization/mutex_types.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.synchronization"></a><a name="synchronization"></a><a class="link" href="synchronization.html" title="Synchronization">Synchronization</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="synchronization/mutex_types.html">Mutex Types</a></span></dt> <dt><span class="section"><a href="synchronization/conditions.html">Condition Variables</a></span></dt> <dt><span class="section"><a href="synchronization/barriers.html">Barriers</a></span></dt> <dt><span class="section"><a href="synchronization/channels.html">Channels</a></span></dt> <dd><dl> <dt><span class="section"><a href="synchronization/channels/buffered_channel.html">Buffered Channel</a></span></dt> <dt><span class="section"><a href="synchronization/channels/unbuffered_channel.html">Unbuffered Channel</a></span></dt> </dl></dd> <dt><span class="section"><a href="synchronization/futures.html">Futures</a></span></dt> <dd><dl> <dt><span class="section"><a href="synchronization/futures/future.html">Future</a></span></dt> <dt><span class="section"><a href="synchronization/futures/promise.html">Template <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code></a></span></dt> <dt><span class="section"><a href="synchronization/futures/packaged_task.html">Template <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code></a></span></dt> </dl></dd> </dl></div> <p> In general, <span class="bold"><strong>Boost.Fiber</strong></span> synchronization objects can neither be moved nor copied. A synchronization object acts as a mutually-agreed rendezvous point between different fibers. If such an object were copied somewhere else, the new copy would have no consumers. If such an object were <span class="emphasis"><em>moved</em></span> somewhere else, leaving the original instance in an unspecified state, existing consumers would behave strangely. </p> <p> The fiber synchronization objects provided by this library will, by default, safely synchronize fibers running on different threads. However, this level of synchronization can be removed (for performance) by building the library with <span class="bold"><strong><code class="computeroutput"><span class="identifier">BOOST_FIBERS_NO_ATOMICS</span></code></strong></span> defined. When the library is built with that macro, you must ensure that all the fibers referencing a particular synchronization object are running in the same thread. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="stack/valgrind.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synchronization/mutex_types.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/overview.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Overview</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="next" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html" title="Implementations: fcontext_t, ucontext_t and WinFiber"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.overview"></a><a class="link" href="overview.html" title="Overview">Overview</a> </h2></div></div></div> <div class="toc"><dl><dt><span class="section"><a href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html">Implementations: fcontext_t, ucontext_t and WinFiber</a></span></dt></dl></div> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides a framework for micro-/userland-threads (fibers) scheduled cooperatively. The API contains classes and functions to manage and synchronize fibers similiarly to <a href="http://en.cppreference.com/w/cpp/thread" target="_top">standard thread support library</a>. </p> <p> Each fiber has its own stack. </p> <p> A fiber can save the current execution state, including all registers and CPU flags, the instruction pointer, and the stack pointer and later restore this state. The idea is to have multiple execution paths running on a single thread using cooperative scheduling (versus threads, which are preemptively scheduled). The running fiber decides explicitly when it should yield to allow another fiber to run (context switching). <span class="bold"><strong>Boost.Fiber</strong></span> internally uses <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html" target="_top"><span class="emphasis"><em>call/cc</em></span></a> from <a href="http://www.boost.org/doc/libs/release/libs/context/index.html" target="_top">Boost.Context</a>; the classes in this library manage, schedule and, when needed, synchronize those execution contexts. A context switch between threads usually costs thousands of CPU cycles on x86, compared to a fiber switch with less than a hundred cycles. A fiber runs on a single thread at any point in time. </p> <p> In order to use the classes and functions described here, you can either include the specific headers specified by the descriptions of each class or function, or include the master library header: </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">all</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> </pre> <p> which includes all the other headers in turn. </p> <p> The namespaces used are: </p> <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span> </pre> <h4> <a name="fiber.overview.h0"></a> <span><a name="fiber.overview.fibers_and_threads"></a></span><a class="link" href="overview.html#fiber.overview.fibers_and_threads">Fibers and Threads</a> </h4> <p> Control is cooperatively passed between fibers launched on a given thread. At a given moment, on a given thread, at most one fiber is running. </p> <p> Spawning additional fibers on a given thread does not distribute your program across more hardware cores, though it can make more effective use of the core on which it's running. </p> <p> On the other hand, a fiber may safely access any resource exclusively owned by its parent thread without explicitly needing to defend that resource against concurrent access by other fibers on the same thread. You are already guaranteed that no other fiber on that thread is concurrently touching that resource. This can be particularly important when introducing concurrency in legacy code. You can safely spawn fibers running old code, using asynchronous I/O to interleave execution. </p> <p> In effect, fibers provide a natural way to organize concurrent code based on asynchronous I/O. Instead of chaining together completion handlers, code running on a fiber can make what looks like a normal blocking function call. That call can cheaply suspend the calling fiber, allowing other fibers on the same thread to run. When the operation has completed, the suspended fiber resumes, without having to explicitly save or restore its state. Its local stack variables persist across the call. </p> <p> A fiber can be migrated from one thread to another, though the library does not do this by default. It is possible for you to supply a custom scheduler that migrates fibers between threads. You may specify custom fiber properties to help your scheduler decide which fibers are permitted to migrate. Please see <a class="link" href="migration.html#migration">Migrating fibers between threads</a> and <a class="link" href="custom.html#custom">Customization</a> for more details. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> allows to <span class="bold"><strong><code class="computeroutput"><span class="identifier">multiplex</span> <span class="identifier">fibers</span> <span class="identifier">across</span> <span class="identifier">multiple</span> <span class="identifier">cores</span></code></strong></span> (see <a class="link" href="numa.html#class_numa_work_stealing"><code class="computeroutput">numa::work_stealing</code></a>). </p> <p> A fiber launched on a particular thread continues running on that thread unless migrated. It might be unblocked (see <a class="link" href="overview.html#blocking">Blocking</a> below) by some other thread, but that only transitions the fiber from <span class="quote">&#8220;<span class="quote">blocked</span>&#8221;</span> to <span class="quote">&#8220;<span class="quote">ready</span>&#8221;</span> on its current thread &#8212; it does not cause the fiber to resume on the thread that unblocked it. </p> <a name="thread_local_storage"></a><h4> <a name="fiber.overview.h1"></a> <span><a name="fiber.overview.thread_local_storage"></a></span><a class="link" href="overview.html#fiber.overview.thread_local_storage">thread-local storage</a> </h4> <p> Unless migrated, a fiber may access thread-local storage; however that storage will be shared among all fibers running on the same thread. For fiber-local storage, please see <a class="link" href="fls.html#class_fiber_specific_ptr"><code class="computeroutput">fiber_specific_ptr</code></a>. </p> <a name="cross_thread_sync"></a><h4> <a name="fiber.overview.h2"></a> <span><a name="fiber.overview.boost_fibers_no_atomics"></a></span><a class="link" href="overview.html#fiber.overview.boost_fibers_no_atomics">BOOST_FIBERS_NO_ATOMICS</a> </h4> <p> The fiber synchronization objects provided by this library will, by default, safely synchronize fibers running on different threads. However, this level of synchronization can be removed (for performance) by building the library with <span class="bold"><strong><code class="computeroutput"><span class="identifier">BOOST_FIBERS_NO_ATOMICS</span></code></strong></span> defined. When the library is built with that macro, you must ensure that all the fibers referencing a particular synchronization object are running in the same thread. Please see <a class="link" href="synchronization.html#synchronization">Synchronization</a>. </p> <a name="blocking"></a><h4> <a name="fiber.overview.h3"></a> <span><a name="fiber.overview.blocking"></a></span><a class="link" href="overview.html#fiber.overview.blocking">Blocking</a> </h4> <p> Normally, when this documentation states that a particular fiber <span class="emphasis"><em>blocks</em></span> (or equivalently, <span class="emphasis"><em>suspends),</em></span> it means that it yields control, allowing other fibers on the same thread to run. The synchronization mechanisms provided by <span class="bold"><strong>Boost.Fiber</strong></span> have this behavior. </p> <p> A fiber may, of course, use normal thread synchronization mechanisms; however a fiber that invokes any of these mechanisms will block its entire thread, preventing any other fiber from running on that thread in the meantime. For instance, when a fiber wants to wait for a value from another fiber in the same thread, using <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span></code> would be unfortunate: <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code> would block the whole thread, preventing the other fiber from delivering its value. Use <a class="link" href="synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> instead. </p> <p> Similarly, a fiber that invokes a normal blocking I/O operation will block its entire thread. Fiber authors are encouraged to consistently use asynchronous I/O. <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a> and other asynchronous I/O operations can straightforwardly be adapted for <span class="bold"><strong>Boost.Fiber</strong></span>: see <a class="link" href="callbacks.html#callbacks">Integrating Fibers with Asynchronous Callbacks</a>. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> depends upon <a href="http://www.boost.org/doc/libs/release/libs/context/index.html" target="_top">Boost.Context</a>. Boost version 1.61.0 or greater is required. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> This library requires C++11! </p></td></tr> </table></div> <div class="important"><table border="0" summary="Important"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> <th align="left">Important</th> </tr> <tr><td align="left" valign="top"><p> Windows using fcontext_t: turn off global program optimization (/GL) and change /EHsc (compiler assumes that functions declared as extern "C" never throw a C++ exception) to /EHs (tells compiler assumes that functions declared as extern "C" may throw an exception). </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/tuning.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Tuning</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="performance.html" title="Performance"> <link rel="next" href="custom.html" title="Customization"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="performance.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="custom.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.tuning"></a><a name="tuning"></a><a class="link" href="tuning.html" title="Tuning">Tuning</a> </h2></div></div></div> <h4> <a name="fiber.tuning.h0"></a> <span><a name="fiber.tuning.disable_synchronization"></a></span><a class="link" href="tuning.html#fiber.tuning.disable_synchronization">Disable synchronization</a> </h4> <p> With <a class="link" href="overview.html#cross_thread_sync"><code class="computeroutput"><span class="identifier">BOOST_FIBERS_NO_ATOMICS</span></code></a> defined at the compiler&#8217;s command line, synchronization between fibers (in different threads) is disabled. This is acceptable if the application is single threaded and/or fibers are not synchronized between threads. </p> <h4> <a name="fiber.tuning.h1"></a> <span><a name="fiber.tuning.memory_allocation"></a></span><a class="link" href="tuning.html#fiber.tuning.memory_allocation">Memory allocation</a> </h4> <p> Memory allocation algorithm is significant for performance in a multithreaded environment, especially for <span class="bold"><strong>Boost.Fiber</strong></span> where fiber stacks are allocated on the heap. The default user-level memory allocator (UMA) of glibc is ptmalloc2 but it can be replaced by another UMA that fit better for the concret work-load For instance Google&#8217;s <a href="http://goog-perftools.sourceforge.net/doc/tcmalloc.html" target="_top">TCmalloc</a> enables a better performance at the <span class="emphasis"><em>skynet</em></span> microbenchmark than glibc&#8217;s default memory allocator. </p> <h4> <a name="fiber.tuning.h2"></a> <span><a name="fiber.tuning.scheduling_strategies"></a></span><a class="link" href="tuning.html#fiber.tuning.scheduling_strategies">Scheduling strategies</a> </h4> <p> The fibers in a thread are coordinated by a fiber manager. Fibers trade control cooperatively, rather than preemptively. Depending on the work-load several strategies of scheduling the fibers are possible <sup>[<a name="fiber.tuning.f0" href="#ftn.fiber.tuning.f0" class="footnote">10</a>]</sup> that can be implmented on behalf of <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>. </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"> work-stealing: ready fibers are hold in a local queue, when the fiber-scheduler's local queue runs out of ready fibers, it randomly selects another fiber-scheduler and tries to steal a ready fiber from the victim (implemented in <a class="link" href="scheduling.html#class_work_stealing"><code class="computeroutput">work_stealing</code></a> and <a class="link" href="numa.html#class_numa_work_stealing"><code class="computeroutput">numa::work_stealing</code></a>) </li> <li class="listitem"> work-requesting: ready fibers are hold in a local queue, when the fiber-scheduler's local queue runs out of ready fibers, it randomly selects another fiber-scheduler and requests for a ready fibers, the victim fiber-scheduler sends a ready-fiber back </li> <li class="listitem"> work-sharing: ready fibers are hold in a global queue, fiber-scheduler concurrently push and pop ready fibers to/from the global queue (implemented in <a class="link" href="scheduling.html#class_shared_work"><code class="computeroutput">shared_work</code></a>) </li> <li class="listitem"> work-distribution: fibers that became ready are proactivly distributed to idle fiber-schedulers or fiber-schedulers with low load </li> <li class="listitem"> work-balancing: a dedicated (helper) fiber-scheduler periodically collects informations about all fiber-scheduler running in other threads and re-distributes ready fibers among them </li> </ul></div> <h4> <a name="fiber.tuning.h3"></a> <span><a name="fiber.tuning.ttas_locks"></a></span><a class="link" href="tuning.html#fiber.tuning.ttas_locks">TTAS locks</a> </h4> <p> Boost.Fiber uses internally spinlocks to protect critical regions if fibers running on different threads interact. Spinlocks are implemented as TTAS (test-test-and-set) locks, i.e. the spinlock tests the lock before calling an atomic exchange. This strategy helps to reduce the cache line invalidations triggered by acquiring/releasing the lock. </p> <h4> <a name="fiber.tuning.h4"></a> <span><a name="fiber.tuning.spin_wait_loop"></a></span><a class="link" href="tuning.html#fiber.tuning.spin_wait_loop">Spin-wait loop</a> </h4> <p> A lock is considered under contention, if a thread repeatedly fails to acquire the lock because some other thread was faster. Waiting for a short time lets other threads finish before trying to enter the critical section again. While busy waiting on the lock, relaxing the CPU (via pause/yield mnemonic) gives the CPU a hint that the code is in a spin-wait loop. </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"> prevents expensive pipeline flushes (speculatively executed load and compare instructions are not pushed to pipeline) </li> <li class="listitem"> another hardware thread (simultaneous multithreading) can get time slice </li> <li class="listitem"> it does delay a few CPU cycles, but this is necessary to prevent starvation </li> </ul></div> <p> It is obvious that this strategy is useless on single core systems because the lock can only released if the thread gives up its time slice in order to let other threads run. The macro BOOST_FIBERS_SPIN_SINGLE_CORE replaces the CPU hints (pause/yield mnemonic) by informing the operating system (via <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread_yield</span><span class="special">()</span></code>) that the thread gives up its time slice and the operating system switches to another thread. </p> <h4> <a name="fiber.tuning.h5"></a> <span><a name="fiber.tuning.exponential_back_off"></a></span><a class="link" href="tuning.html#fiber.tuning.exponential_back_off">Exponential back-off</a> </h4> <p> The macro BOOST_FIBERS_RETRY_THRESHOLD determines how many times the CPU iterates in the spin-wait loop before yielding the thread or blocking in futex-wait. The spinlock tracks how many times the thread failed to acquire the lock. The higher the contention, the longer the thread should back-off. A <span class="quote">&#8220;<span class="quote">Binary Exponential Backoff</span>&#8221;</span> algorithm together with a randomized contention window is utilized for this purpose. BOOST_FIBERS_CONTENTION_WINDOW_THRESHOLD determines the upper limit of the contention window (expressed as the exponent for basis of two). </p> <h4> <a name="fiber.tuning.h6"></a> <span><a name="fiber.tuning.speculative_execution__hardware_transactional_memory_"></a></span><a class="link" href="tuning.html#fiber.tuning.speculative_execution__hardware_transactional_memory_">Speculative execution (hardware transactional memory)</a> </h4> <p> Boost.Fiber uses spinlocks to protect critical regions that can be used together with transactional memory (see section <a class="link" href="speculation.html#speculation">Speculative execution</a>). </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> TXS is enabled if property <code class="computeroutput"><span class="identifier">htm</span><span class="special">=</span><span class="identifier">tsx</span></code> is specified at b2 command-line and <code class="computeroutput"><span class="identifier">BOOST_USE_TSX</span></code> is applied to the compiler. </p></td></tr> </table></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> A TSX-transaction will be aborted if the floating point state is modified inside a critical region. As a consequence floating point operations, e.g. tore/load of floating point related registers during a fiber (context) switch are disabled. </p></td></tr> </table></div> <h4> <a name="fiber.tuning.h7"></a> <span><a name="fiber.tuning.numa_systems"></a></span><a class="link" href="tuning.html#fiber.tuning.numa_systems">NUMA systems</a> </h4> <p> Modern multi-socket systems are usually designed as <a class="link" href="numa.html#numa">NUMA systems</a>. A suitable fiber scheduler like <a class="link" href="numa.html#class_numa_work_stealing"><code class="computeroutput">numa::work_stealing</code></a> reduces remote memory access (latence). </p> <h4> <a name="fiber.tuning.h8"></a> <span><a name="fiber.tuning.parameters"></a></span><a class="link" href="tuning.html#fiber.tuning.parameters">Parameters</a> </h4> <div class="table"> <a name="fiber.tuning.parameters_that_migh_be_defiend_at_compiler_s_command_line"></a><p class="title"><b>Table&#160;1.5.&#160;Parameters that migh be defiend at compiler's command line</b></p> <div class="table-contents"><table class="table" summary="Parameters that migh be defiend at compiler's command line"> <colgroup> <col> <col> <col> </colgroup> <thead><tr> <th> <p> Parameter </p> </th> <th> <p> Default value </p> </th> <th> <p> Effect on Boost.Fiber </p> </th> </tr></thead> <tbody> <tr> <td> <p> BOOST_FIBERS_NO_ATOMICS </p> </td> <td> <p> - </p> </td> <td> <p> no multithreading support, all atomics removed, no synchronization between fibers running in different threads </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_STD_MUTEX </p> </td> <td> <p> - </p> </td> <td> <p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mutex</span></code> used inside spinlock </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS </p> </td> <td> <p> + </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, adaptive retries while busy waiting </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_FUTEX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, suspend on futex after certain number of retries </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries, suspend on futex certain amount of retries </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS + BOOST_USE_TSX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap and speculative execution (Intel TSX required) </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE + BOOST_USE_TSX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, adaptive retries while busy waiting and speculative execution (Intel TSX required) </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_FUTEX + BOOST_USE_TSX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, suspend on futex after certain number of retries and speculative execution (Intel TSX required) </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX + BOOST_USE_TSX </p> </td> <td> <p> - </p> </td> <td> <p> spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries, suspend on futex certain amount of retries and speculative execution (Intel TSX required) </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPIN_SINGLE_CORE </p> </td> <td> <p> - </p> </td> <td> <p> on single core machines with multiple threads, yield thread (<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code>) after collisions </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_RETRY_THRESHOLD </p> </td> <td> <p> 64 </p> </td> <td> <p> max number of retries while busy spinning, the use fallback </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_CONTENTION_WINDOW_THRESHOLD </p> </td> <td> <p> 16 </p> </td> <td> <p> max size of collisions window, expressed as exponent for the basis of two </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPIN_BEFORE_SLEEP0 </p> </td> <td> <p> 32 </p> </td> <td> <p> max number of retries that relax the processor before the thread sleeps for 0s </p> </td> </tr> <tr> <td> <p> BOOST_FIBERS_SPIN_BEFORE_YIELD </p> </td> <td> <p> 64 </p> </td> <td> <p> max number of retries where the thread sleeps for 0s before yield thread (<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code>) </p> </td> </tr> </tbody> </table></div> </div> <br class="table-break"><div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.tuning.f0" href="#fiber.tuning.f0" class="para">10</a>] </sup> 1024cores.net: <a href="http://www.1024cores.net/home/scalable-architecture/task-scheduling-strategies" target="_top">Task Scheduling Strategies</a> </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="performance.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="custom.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/performance.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Performance</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="worker.html" title="Running with worker threads"> <link rel="next" href="tuning.html" title="Tuning"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="worker.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tuning.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.performance"></a><a class="link" href="performance.html" title="Performance">Performance</a> </h2></div></div></div> <p> Performance measurements were taken using <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">highresolution_clock</span></code>, with overhead corrections. The code was compiled with gcc-6.3.1, using build options: variant = release, optimization = speed. Tests were executed on dual Intel XEON E5 2620v4 2.2GHz, 16C/32T, 64GB RAM, running Linux (x86_64). </p> <p> Measurements headed 1C/1T were run in a single-threaded process. </p> <p> The <a href="https://github.com/atemerev/skynet" target="_top">microbenchmark <span class="emphasis"><em>syknet</em></span></a> from Alexander Temerev was ported and used for performance measurements. At the root the test spawns 10 threads-of-execution (ToE), e.g. actor/goroutine/fiber etc.. Each spawned ToE spawns additional 10 ToEs ... until <span class="bold"><strong>1,000,000</strong></span> ToEs are created. ToEs return back their ordinal numbers (0 ... 999,999), which are summed on the previous level and sent back upstream, until reaching the root. The test was run 10-20 times, producing a range of values for each measurement. </p> <div class="table"> <a name="fiber.performance.time_per_actor_erlang_process_goroutine__other_languages___average_over_1_000_000_"></a><p class="title"><b>Table&#160;1.2.&#160;time per actor/erlang process/goroutine (other languages) (average over 1,000,000)</b></p> <div class="table-contents"><table class="table" summary="time per actor/erlang process/goroutine (other languages) (average over 1,000,000)"> <colgroup> <col> <col> <col> </colgroup> <thead><tr> <th> <p> Haskell | stack-1.4.0/ghc-8.0.1 </p> </th> <th> <p> Go | go1.8.1 </p> </th> <th> <p> Erlang | erts-8.3 </p> </th> </tr></thead> <tbody><tr> <td> <p> 0.05 &#181;s - 0.06 &#181;s </p> </td> <td> <p> 0.42 &#181;s - 0.49 &#181;s </p> </td> <td> <p> 0.63 &#181;s - 0.73 &#181;s </p> </td> </tr></tbody> </table></div> </div> <br class="table-break"><p> Pthreads are created with a stack size of 8kB while <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span></code>'s use the system default (1MB - 2MB). The microbenchmark could <span class="bold"><strong>not</strong></span> be <span class="bold"><strong>run</strong></span> with 1,000,000 threads because of <span class="bold"><strong>resource exhaustion</strong></span> (pthread and std::thread). Instead the test runs only at <span class="bold"><strong>10,000</strong></span> threads. </p> <div class="table"> <a name="fiber.performance.time_per_thread__average_over_10_000___unable_to_spawn_1_000_000_threads_"></a><p class="title"><b>Table&#160;1.3.&#160;time per thread (average over 10,000 - unable to spawn 1,000,000 threads)</b></p> <div class="table-contents"><table class="table" summary="time per thread (average over 10,000 - unable to spawn 1,000,000 threads)"> <colgroup> <col> <col> <col> </colgroup> <thead><tr> <th> <p> pthread </p> </th> <th> <p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span></code> </p> </th> <th> <p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">async</span></code> </p> </th> </tr></thead> <tbody><tr> <td> <p> 54 &#181;s - 73 &#181;s </p> </td> <td> <p> 52 &#181;s - 73 &#181;s </p> </td> <td> <p> 106 &#181;s - 122 &#181;s </p> </td> </tr></tbody> </table></div> </div> <br class="table-break"><p> The test utilizes 16 cores with Symmetric MultiThreading enabled (32 logical CPUs). The fiber stacks are allocated by <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a>. </p> <p> As the benchmark shows, the memory allocation algorithm is significant for performance in a multithreaded environment. The tests use glibc&#8217;s memory allocation algorithm (based on ptmalloc2) as well as Google&#8217;s <a href="http://goog-perftools.sourceforge.net/doc/tcmalloc.html" target="_top">TCmalloc</a> (via linkflags="-ltcmalloc").<sup>[<a name="fiber.performance.f0" href="#ftn.fiber.performance.f0" class="footnote">9</a>]</sup> </p> <p> In the <a class="link" href="scheduling.html#class_work_stealing"><code class="computeroutput">work_stealing</code></a> scheduling algorithm, each thread has its own local queue. Fibers that are ready to run are pushed to and popped from the local queue. If the queue runs out of ready fibers, fibers are stolen from the local queues of other participating threads. </p> <div class="table"> <a name="fiber.performance.time_per_fiber__average_over_1_000_000_"></a><p class="title"><b>Table&#160;1.4.&#160;time per fiber (average over 1.000.000)</b></p> <div class="table-contents"><table class="table" summary="time per fiber (average over 1.000.000)"> <colgroup> <col> <col> </colgroup> <thead><tr> <th> <p> fiber (16C/32T, work stealing, tcmalloc) </p> </th> <th> <p> fiber (1C/1T, round robin, tcmalloc) </p> </th> </tr></thead> <tbody><tr> <td> <p> 0.05 &#181;s - 0.09 &#181;s </p> </td> <td> <p> 1.69 &#181;s - 1.79 &#181;s </p> </td> </tr></tbody> </table></div> </div> <br class="table-break"><div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.performance.f0" href="#fiber.performance.f0" class="para">9</a>] </sup> Tais B. Ferreira, Rivalino Matias, Autran Macedo, Lucio B. Araujo <span class="quote">&#8220;<span class="quote">An Experimental Study on Memory Allocators in Multicore and Multithreaded Applications</span>&#8221;</span>, PDCAT &#8217;11 Proceedings of the 2011 12th International Conference on Parallel and Distributed Computing, Applications and Technologies, pages 92-98 </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="worker.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tuning.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/gpu_computing.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>GPU computing</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="numa.html" title="NUMA"> <link rel="next" href="gpu_computing/cuda.html" title="CUDA"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="numa.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gpu_computing/cuda.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.gpu_computing"></a><a class="link" href="gpu_computing.html" title="GPU computing">GPU computing</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="gpu_computing/cuda.html">CUDA</a></span></dt> <dt><span class="section"><a href="gpu_computing/hip.html">ROCm/HIP</a></span></dt> </dl></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="numa.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gpu_computing/cuda.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/acknowledgements.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Acknowledgments</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="rationale.html" title="Rationale"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="rationale.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.acknowledgements"></a><a class="link" href="acknowledgements.html" title="Acknowledgments">Acknowledgments</a> </h2></div></div></div> <p> I'd like to thank Agust&#237;n Berg&#233;, Eugene Yakubovich, Giovanni Piero Deretta and especially Nat Goodspeed. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="rationale.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/scheduling.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Scheduling</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="fiber_mgmt/this_fiber.html" title="Namespace this_fiber"> <link rel="next" href="stack.html" title="Stack allocation"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fiber_mgmt/this_fiber.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.scheduling"></a><a name="scheduling"></a><a class="link" href="scheduling.html" title="Scheduling">Scheduling</a> </h2></div></div></div> <p> The fibers in a thread are coordinated by a fiber manager. Fibers trade control cooperatively, rather than preemptively: the currently-running fiber retains control until it invokes some operation that passes control to the manager. Each time a fiber suspends (or yields), the fiber manager consults a scheduler to determine which fiber will run next. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides the fiber manager, but the scheduler is a customization point. (See <a class="link" href="custom.html#custom">Customization</a>.) </p> <p> Each thread has its own scheduler. Different threads in a process may use different schedulers. By default, <span class="bold"><strong>Boost.Fiber</strong></span> implicitly instantiates <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> as the scheduler for each thread. </p> <p> You are explicitly permitted to code your own <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> subclass. For the most part, your <code class="computeroutput"><span class="identifier">algorithm</span></code> subclass need not defend against cross-thread calls: the fiber manager intercepts and defers such calls. Most <code class="computeroutput"><span class="identifier">algorithm</span></code> methods are only ever directly called from the thread whose fibers it is managing &#8212; with exceptions as documented below. </p> <p> Your <code class="computeroutput"><span class="identifier">algorithm</span></code> subclass is engaged on a particular thread by calling <a class="link" href="fiber_mgmt/fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a>: </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">thread_fn</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">my_fiber_scheduler</span> <span class="special">&gt;();</span> <span class="special">...</span> <span class="special">}</span> </pre> <p> A scheduler class must implement interface <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>. <span class="bold"><strong>Boost.Fiber</strong></span> provides schedulers: <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a>, <a class="link" href="scheduling.html#class_work_stealing"><code class="computeroutput">work_stealing</code></a>, <a class="link" href="numa.html#class_numa_work_stealing"><code class="computeroutput">numa::work_stealing</code></a> and <a class="link" href="scheduling.html#class_shared_work"><code class="computeroutput">shared_work</code></a>. </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">thread</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">thread_count</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// thread registers itself at work-stealing scheduler</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span> <span class="special">&gt;(</span> <span class="identifier">thread_count</span><span class="special">);</span> <span class="special">...</span> <span class="special">}</span> <span class="comment">// count of logical cpus</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">thread_count</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">hardware_concurrency</span><span class="special">();</span> <span class="comment">// start worker-threads first</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span> <span class="special">&gt;</span> <span class="identifier">threads</span><span class="special">;</span> <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span> <span class="comment">/* count start-thread */</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">thread_count</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// spawn thread</span> <span class="identifier">threads</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">(</span> <span class="identifier">thread</span><span class="special">,</span> <span class="identifier">thread_count</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// start-thread registers itself at work-stealing scheduler</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span> <span class="special">&gt;(</span> <span class="identifier">thread_count</span><span class="special">);</span> <span class="special">...</span> </pre> <p> The example spawns as many threads as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">::</span><span class="identifier">hardware_concurrency</span><span class="special">()</span></code> returns. Each thread runs a <a class="link" href="scheduling.html#class_work_stealing"><code class="computeroutput">work_stealing</code></a> scheduler. Each instance of this scheduler needs to know how many threads run the work-stealing scheduler in the program. If the local queue of one thread runs out of ready fibers, the thread tries to steal a ready fiber from another thread running this scheduler. </p> <p> </p> <h5> <a name="class_algorithm_bridgehead"></a> <span><a name="class_algorithm"></a></span> <a class="link" href="scheduling.html#class_algorithm">Class <code class="computeroutput">algorithm</code></a> </h5> <p> </p> <p> <code class="computeroutput"><span class="identifier">algorithm</span></code> is the abstract base class defining the interface that a fiber scheduler must implement. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">algorithm</span> <span class="special">{</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">algorithm</span><span class="special">();</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="algorithm_awakened_bridgehead"></a> <span><a name="algorithm_awakened"></a></span> <a class="link" href="scheduling.html#algorithm_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs the scheduler that fiber <code class="computeroutput"><span class="identifier">f</span></code> is ready to run. Fiber <code class="computeroutput"><span class="identifier">f</span></code> might be newly launched, or it might have been blocked but has just been awakened, or it might have called <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This method advises the scheduler to add fiber <code class="computeroutput"><span class="identifier">f</span></code> to its collection of fibers ready to run. A typical scheduler implementation places <code class="computeroutput"><span class="identifier">f</span></code> into a queue. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_pick_next_bridgehead"></a> <span><a name="algorithm_pick_next"></a></span> <a class="link" href="scheduling.html#algorithm_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber which is to be resumed next, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if there is no ready fiber. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This is where the scheduler actually specifies the fiber which is to run next. A typical scheduler implementation chooses the head of the ready queue. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_has_ready_fibers_bridgehead"></a> <span><a name="algorithm_has_ready_fibers"></a></span> <a class="link" href="scheduling.html#algorithm_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_suspend_until_bridgehead"></a> <span><a name="algorithm_suspend_until"></a></span> <a class="link" href="scheduling.html#algorithm_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs the scheduler that no fiber will be ready until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This method allows a custom scheduler to yield control to the containing environment in whatever way makes sense. The fiber manager is stating that <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> need not return until <code class="computeroutput"><span class="identifier">abs_time</span></code> &#8212; or <a class="link" href="scheduling.html#algorithm_notify"><code class="computeroutput">algorithm::notify()</code></a> is called &#8212; whichever comes first. The interaction with <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> means that, for instance, calling <a href="http://en.cppreference.com/w/cpp/thread/sleep_until" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">this_thread</span><span class="special">::</span><span class="identifier">sleep_until</span><span class="special">(</span><span class="identifier">abs_time</span><span class="special">)</span></code></a> would be too simplistic. <a class="link" href="scheduling.html#round_robin_suspend_until"><code class="computeroutput">round_robin::suspend_until()</code></a> uses a <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span></code></a> to coordinate with <a class="link" href="scheduling.html#round_robin_notify"><code class="computeroutput">round_robin::notify()</code></a>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Given that <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> might be called from another thread, your <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> implementation &#8212; like the rest of your <code class="computeroutput"><span class="identifier">algorithm</span></code> implementation &#8212; must guard any data it shares with your <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> implementation. </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_notify_bridgehead"></a> <span><a name="algorithm_notify"></a></span> <a class="link" href="scheduling.html#algorithm_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Requests the scheduler to return from a pending call to <a class="link" href="scheduling.html#algorithm_suspend_until"><code class="computeroutput">algorithm::suspend_until()</code></a>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Alone among the <code class="computeroutput"><span class="identifier">algorithm</span></code> methods, <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> may be called from another thread. Your <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> implementation must guard any data it shares with the rest of your <code class="computeroutput"><span class="identifier">algorithm</span></code> implementation. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_round_robin_bridgehead"></a> <span><a name="class_round_robin"></a></span> <a class="link" href="scheduling.html#class_round_robin">Class <code class="computeroutput">round_robin</code></a> </h5> <p> </p> <p> This class implements <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>, scheduling fibers in round-robin fashion. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">round_robin</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">round_robin</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">algorithm</span> <span class="special">{</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="round_robin_awakened_bridgehead"></a> <span><a name="round_robin_awakened"></a></span> <a class="link" href="scheduling.html#round_robin_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Enqueues fiber <code class="computeroutput"><span class="identifier">f</span></code> onto a ready queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="round_robin_pick_next_bridgehead"></a> <span><a name="round_robin_pick_next"></a></span> <a class="link" href="scheduling.html#round_robin_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber at the head of the ready queue, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if the queue is empty. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Placing ready fibers onto the tail of a queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </p></dd> </dl> </div> <p> </p> <h5> <a name="round_robin_has_ready_fibers_bridgehead"></a> <span><a name="round_robin_has_ready_fibers"></a></span> <a class="link" href="scheduling.html#round_robin_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="round_robin_suspend_until_bridgehead"></a> <span><a name="round_robin_suspend_until"></a></span> <a class="link" href="scheduling.html#round_robin_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs <code class="computeroutput"><span class="identifier">round_robin</span></code> that no ready fiber will be available until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. This implementation blocks in <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">wait_until</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="round_robin_notify_bridgehead"></a> <span><a name="round_robin_notify"></a></span> <a class="link" href="scheduling.html#round_robin_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Wake up a pending call to <a class="link" href="scheduling.html#round_robin_suspend_until"><code class="computeroutput">round_robin::suspend_until()</code></a>, some fibers might be ready. This implementation wakes <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> via <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">notify_all</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_work_stealing_bridgehead"></a> <span><a name="class_work_stealing"></a></span> <a class="link" href="scheduling.html#class_work_stealing">Class <code class="computeroutput">work_stealing</code></a> </h5> <p> </p> <p> This class implements <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>; if the local ready-queue runs out of ready fibers, ready fibers are stolen from other schedulers.<br> The victim scheduler (from which a ready fiber is stolen) is selected at random. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported. </p></td></tr> </table></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">work_stealing</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">work_stealing</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">algorithm</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">thread_count</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">suspend</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">work_stealing</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">work_stealing</span> <span class="special">&amp;&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">work_stealing</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">work_stealing</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">work_stealing</span> <span class="special">&amp;&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}}</span> </pre> <h4> <a name="fiber.scheduling.h0"></a> <span><a name="fiber.scheduling.constructor"></a></span><a class="link" href="scheduling.html#fiber.scheduling.constructor">Constructor</a> </h4> <pre class="programlisting"><span class="identifier">work_stealing</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint32_t</span> <span class="identifier">thread_count</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">suspend</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs work-stealing scheduling algorithm. <code class="computeroutput"><span class="identifier">thread_count</span></code> represents the number of threads running this algorithm. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">system_error</span></code> </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> If <code class="computeroutput"><span class="identifier">suspend</span></code> is set to <code class="computeroutput"><span class="keyword">true</span></code>, then the scheduler suspends if no ready fiber could be stolen. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote (other thread or fiber scheduler). </p></dd> </dl> </div> <p> </p> <h5> <a name="work_stealing_awakened_bridgehead"></a> <span><a name="work_stealing_awakened"></a></span> <a class="link" href="scheduling.html#work_stealing_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Enqueues fiber <code class="computeroutput"><span class="identifier">f</span></code> onto the shared ready queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="work_stealing_pick_next_bridgehead"></a> <span><a name="work_stealing_pick_next"></a></span> <a class="link" href="scheduling.html#work_stealing_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber at the head of the ready queue, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if the queue is empty. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Placing ready fibers onto the tail of the sahred queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </p></dd> </dl> </div> <p> </p> <h5> <a name="work_stealing_has_ready_fibers_bridgehead"></a> <span><a name="work_stealing_has_ready_fibers"></a></span> <a class="link" href="scheduling.html#work_stealing_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="work_stealing_suspend_until_bridgehead"></a> <span><a name="work_stealing_suspend_until"></a></span> <a class="link" href="scheduling.html#work_stealing_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs <code class="computeroutput"><span class="identifier">work_stealing</span></code> that no ready fiber will be available until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. This implementation blocks in <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">wait_until</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="work_stealing_notify_bridgehead"></a> <span><a name="work_stealing_notify"></a></span> <a class="link" href="scheduling.html#work_stealing_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Wake up a pending call to <a class="link" href="scheduling.html#work_stealing_suspend_until"><code class="computeroutput">work_stealing::suspend_until()</code></a>, some fibers might be ready. This implementation wakes <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> via <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">notify_all</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_shared_work_bridgehead"></a> <span><a name="class_shared_work"></a></span> <a class="link" href="scheduling.html#class_shared_work">Class <code class="computeroutput">shared_work</code></a> </h5> <p> </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Because of the non-locality of data, <span class="emphasis"><em>shared_work</em></span> is less performant than <a class="link" href="scheduling.html#class_work_stealing"><code class="computeroutput">work_stealing</code></a>. </p></td></tr> </table></div> <p> This class implements <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>, scheduling fibers in round-robin fashion. Ready fibers are shared between all instances (running on different threads) of shared_work, thus the work is distributed equally over all threads. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported. </p></td></tr> </table></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">algo</span><span class="special">/</span><span class="identifier">shared_work</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">shared_work</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">algorithm</span> <span class="special">{</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="shared_work_awakened_bridgehead"></a> <span><a name="shared_work_awakened"></a></span> <a class="link" href="scheduling.html#shared_work_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Enqueues fiber <code class="computeroutput"><span class="identifier">f</span></code> onto the shared ready queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_work_pick_next_bridgehead"></a> <span><a name="shared_work_pick_next"></a></span> <a class="link" href="scheduling.html#shared_work_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber at the head of the ready queue, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if the queue is empty. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Placing ready fibers onto the tail of the shared queue, and returning them from the head of that queue, shares the thread between ready fibers in round-robin fashion. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_work_has_ready_fibers_bridgehead"></a> <span><a name="shared_work_has_ready_fibers"></a></span> <a class="link" href="scheduling.html#shared_work_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_work_suspend_until_bridgehead"></a> <span><a name="shared_work_suspend_until"></a></span> <a class="link" href="scheduling.html#shared_work_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs <code class="computeroutput"><span class="identifier">shared_work</span></code> that no ready fiber will be available until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. This implementation blocks in <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">wait_until</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_work_notify_bridgehead"></a> <span><a name="shared_work_notify"></a></span> <a class="link" href="scheduling.html#shared_work_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Wake up a pending call to <a class="link" href="scheduling.html#shared_work_suspend_until"><code class="computeroutput">shared_work::suspend_until()</code></a>, some fibers might be ready. This implementation wakes <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> via <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">condition_variable</span><span class="special">::</span><span class="identifier">notify_all</span><span class="special">()</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h4> <a name="fiber.scheduling.h1"></a> <span><a name="fiber.scheduling.custom_scheduler_fiber_properties"></a></span><a class="link" href="scheduling.html#fiber.scheduling.custom_scheduler_fiber_properties">Custom Scheduler Fiber Properties</a> </h4> <p> A scheduler class directly derived from <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> can use any information available from <a class="link" href="scheduling.html#class_context"><code class="computeroutput">context</code></a> to implement the <code class="computeroutput"><span class="identifier">algorithm</span></code> interface. But a custom scheduler might need to track additional properties for a fiber. For instance, a priority-based scheduler would need to track a fiber&#8217;s priority. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides a mechanism by which your custom scheduler can associate custom properties with each fiber. </p> <p> </p> <h5> <a name="class_fiber_properties_bridgehead"></a> <span><a name="class_fiber_properties"></a></span> <a class="link" href="scheduling.html#class_fiber_properties">Class <code class="computeroutput">fiber_properties</code></a> </h5> <p> </p> <p> A custom fiber properties class must be derived from <code class="computeroutput"><span class="identifier">fiber_properties</span></code>. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">properties</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">fiber_properties</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">fiber_properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">fiber_properties</span><span class="special">();</span> <span class="keyword">protected</span><span class="special">:</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h4> <a name="fiber.scheduling.h2"></a> <span><a name="fiber.scheduling.constructor0"></a></span><a class="link" href="scheduling.html#fiber.scheduling.constructor0">Constructor</a> </h4> <pre class="programlisting"><span class="identifier">fiber_properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs base-class component of custom subclass. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Your subclass constructor must accept a <code class="computeroutput"><span class="identifier">context</span><span class="special">*</span></code> and pass it to the base-class <code class="computeroutput"><span class="identifier">fiber_properties</span></code> constructor. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_properties_notify_bridgehead"></a> <span><a name="fiber_properties_notify"></a></span> <a class="link" href="scheduling.html#fiber_properties_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Pass control to the custom <a class="link" href="scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> subclass&#8217;s <a class="link" href="scheduling.html#algorithm_with_properties_property_change"><code class="computeroutput">algorithm_with_properties::property_change()</code></a> method. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> A custom scheduler&#8217;s <a class="link" href="scheduling.html#algorithm_with_properties_pick_next"><code class="computeroutput">algorithm_with_properties::pick_next()</code></a> method might dynamically select from the ready fibers, or <a class="link" href="scheduling.html#algorithm_with_properties_awakened"><code class="computeroutput">algorithm_with_properties::awakened()</code></a> might instead insert each ready fiber into some form of ready queue for <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code>. In the latter case, if application code modifies a fiber property (e.g. priority) that should affect that fiber&#8217;s relationship to other ready fibers, the custom scheduler must be given the opportunity to reorder its ready queue. The custom property subclass should implement an access method to modify such a property; that access method should call <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> once the new property value has been stored. This passes control to the custom scheduler&#8217;s <code class="computeroutput"><span class="identifier">property_change</span><span class="special">()</span></code> method, allowing the custom scheduler to reorder its ready queue appropriately. Use at your discretion. Of course, if you define a property which does not affect the behavior of the <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> method, you need not call <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> when that property is modified. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_algorithm_with_properties_bridgehead"></a> <span><a name="class_algorithm_with_properties"></a></span> <a class="link" href="scheduling.html#class_algorithm_with_properties">Template <code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> </h5> <p> </p> <p> A custom scheduler that depends on a custom properties class <code class="computeroutput"><span class="identifier">PROPS</span></code> should be derived from <code class="computeroutput"><span class="identifier">algorithm_with_properties</span><span class="special">&lt;</span><span class="identifier">PROPS</span><span class="special">&gt;</span></code>. <code class="computeroutput"><span class="identifier">PROPS</span></code> should be derived from <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a>. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">algorithm_with_properties</span> <span class="special">{</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*,</span> <span class="identifier">PROPS</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">property_change</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*,</span> <span class="identifier">PROPS</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">virtual</span> <span class="identifier">fiber_properties</span> <span class="special">*</span> <span class="identifier">new_properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*);</span> <span class="special">};</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="algorithm_with_properties_awakened_bridgehead"></a> <span><a name="algorithm_with_properties_awakened"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_awakened">Member function <code class="computeroutput">awakened</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">awakened</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs the scheduler that fiber <code class="computeroutput"><span class="identifier">f</span></code> is ready to run, like <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a>. Passes the fiber&#8217;s associated <code class="computeroutput"><span class="identifier">PROPS</span></code> instance. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> An <code class="computeroutput"><span class="identifier">algorithm_with_properties</span><span class="special">&lt;&gt;</span></code> subclass must override this method instead of <code class="computeroutput"><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">awakened</span><span class="special">()</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_pick_next_bridgehead"></a> <span><a name="algorithm_with_properties_pick_next"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_pick_next">Member function <code class="computeroutput">pick_next</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">pick_next</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the fiber which is to be resumed next, or <code class="computeroutput"><span class="keyword">nullptr</span></code> if there is no ready fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> same as <a class="link" href="scheduling.html#algorithm_pick_next"><code class="computeroutput">algorithm::pick_next()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_has_ready_fibers_bridgehead"></a> <span><a name="algorithm_with_properties_has_ready_fibers"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_has_ready_fibers">Member function <code class="computeroutput">has_ready_fibers</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> same as <a class="link" href="scheduling.html#algorithm_has_ready_fibers"><code class="computeroutput">algorithm::has_ready_fibers()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_suspend_until_bridgehead"></a> <span><a name="algorithm_with_properties_suspend_until"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_suspend_until">Member function <code class="computeroutput">suspend_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Informs the scheduler that no fiber will be ready until time-point <code class="computeroutput"><span class="identifier">abs_time</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> same as <a class="link" href="scheduling.html#algorithm_suspend_until"><code class="computeroutput">algorithm::suspend_until()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_notify_bridgehead"></a> <span><a name="algorithm_with_properties_notify"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_notify">Member function <code class="computeroutput">notify</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Requests the scheduler to return from a pending call to <a class="link" href="scheduling.html#algorithm_with_properties_suspend_until"><code class="computeroutput">algorithm_with_properties::suspend_until()</code></a>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> same as <a class="link" href="scheduling.html#algorithm_notify"><code class="computeroutput">algorithm::notify()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_properties_bridgehead"></a> <span><a name="algorithm_with_properties_properties"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_properties">Member function <code class="computeroutput">properties</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">PROPS</span><span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> the <code class="computeroutput"><span class="identifier">PROPS</span></code> instance associated with fiber <code class="computeroutput"><span class="identifier">f</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The fiber&#8217;s associated <code class="computeroutput"><span class="identifier">PROPS</span></code> instance is already passed to <a class="link" href="scheduling.html#algorithm_with_properties_awakened"><code class="computeroutput">algorithm_with_properties::awakened()</code></a> and <a class="link" href="scheduling.html#algorithm_with_properties_property_change"><code class="computeroutput">algorithm_with_properties::property_change()</code></a>. However, every <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> subclass is expected to track a collection of ready <a class="link" href="scheduling.html#class_context"><code class="computeroutput">context</code></a> instances. This method allows your custom scheduler to retrieve the <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a> subclass instance for any <code class="computeroutput"><span class="identifier">context</span></code> in its collection. </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_property_change_bridgehead"></a> <span><a name="algorithm_with_properties_property_change"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_property_change">Member function <code class="computeroutput">property_change</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">property_change</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Notify the custom scheduler of a possibly-relevant change to a property belonging to fiber <code class="computeroutput"><span class="identifier">f</span></code>. <code class="computeroutput"><span class="identifier">properties</span></code> contains the new values of all relevant properties. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This method is only called when a custom <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a> subclass explicitly calls <a class="link" href="scheduling.html#fiber_properties_notify"><code class="computeroutput">fiber_properties::notify()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="algorithm_with_properties_new_properties_bridgehead"></a> <span><a name="algorithm_with_properties_new_properties"></a></span> <a class="link" href="scheduling.html#algorithm_with_properties_new_properties">Member function <code class="computeroutput">new_properties</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">virtual</span> <span class="identifier">fiber_properties</span> <span class="special">*</span> <span class="identifier">new_properties</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> A new instance of <a class="link" href="scheduling.html#class_fiber_properties"><code class="computeroutput">fiber_properties</code></a> subclass <code class="computeroutput"><span class="identifier">PROPS</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> By default, <code class="computeroutput"><span class="identifier">algorithm_with_properties</span><span class="special">&lt;&gt;::</span><span class="identifier">new_properties</span><span class="special">()</span></code> simply returns <code class="computeroutput"><span class="keyword">new</span> <span class="identifier">PROPS</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span></code>, placing the <code class="computeroutput"><span class="identifier">PROPS</span></code> instance on the heap. Override this method to allocate <code class="computeroutput"><span class="identifier">PROPS</span></code> some other way. The returned <code class="computeroutput"><span class="identifier">fiber_properties</span></code> pointer must point to the <code class="computeroutput"><span class="identifier">PROPS</span></code> instance to be associated with fiber <code class="computeroutput"><span class="identifier">f</span></code>. </p></dd> </dl> </div> <p> <a name="context"></a></p> <h5> <a name="class_context_bridgehead"></a> <span><a name="class_context"></a></span> <a class="link" href="scheduling.html#class_context">Class <code class="computeroutput">context</code></a> </h5> <p> </p> <p> While you are free to treat <code class="computeroutput"><span class="identifier">context</span><span class="special">*</span></code> as an opaque token, certain <code class="computeroutput"><span class="identifier">context</span></code> members may be useful to a custom scheduler implementation. </p> <p> <a name="ready_queue_t"></a>Of particular note is the fact that <code class="computeroutput"><span class="identifier">context</span></code> contains a hook to participate in a <a href="http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive</span><span class="special">::</span><span class="identifier">list</span></code></a> <code class="literal">typedef</code>&#8217;ed as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">scheduler</span><span class="special">::</span><span class="identifier">ready_queue_t</span></code>. This hook is reserved for use by <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementations. (For instance, <a class="link" href="scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> contains a <code class="computeroutput"><span class="identifier">ready_queue_t</span></code> instance to manage its ready fibers.) See <a class="link" href="scheduling.html#context_ready_is_linked"><code class="computeroutput">context::ready_is_linked()</code></a>, <a class="link" href="scheduling.html#context_ready_link"><code class="computeroutput">context::ready_link()</code></a>, <a class="link" href="scheduling.html#context_ready_unlink"><code class="computeroutput">context::ready_unlink()</code></a>. </p> <p> Your <code class="computeroutput"><span class="identifier">algorithm</span></code> implementation may use any container you desire to manage passed <code class="computeroutput"><span class="identifier">context</span></code> instances. <code class="computeroutput"><span class="identifier">ready_queue_t</span></code> avoids some of the overhead of typical STL containers. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">context</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">type</span> <span class="special">{</span> <span class="identifier">none</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="identifier">main_context</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="comment">// fiber associated with thread's stack</span> <span class="identifier">dispatcher_context</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="comment">// special fiber for maintenance operations</span> <span class="identifier">worker_context</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="comment">// fiber not special to the library</span> <span class="identifier">pinned_context</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span> <span class="comment">// fiber must not be migrated to another thread</span> <span class="special">};</span> <span class="keyword">class</span> <span class="identifier">context</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">class</span> <span class="identifier">id</span><span class="special">;</span> <span class="keyword">static</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">active</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">context</span><span class="special">(</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">context</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">detach</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">attach</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">is_context</span><span class="special">(</span> <span class="identifier">type</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">is_terminated</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">ready_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">remote_ready_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">wait_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">ready_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">remote_ready_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">ready_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">remote_ready_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">wait_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">suspend</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">schedule</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">};</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="context_active_bridgehead"></a> <span><a name="context_active"></a></span> <a class="link" href="scheduling.html#context_active">Static member function <code class="computeroutput">active</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">active</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> Pointer to instance of current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="context_get_id_bridgehead"></a> <span><a name="context_get_id"></a></span> <a class="link" href="scheduling.html#context_get_id">Member function <code class="computeroutput">get_id</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">context</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> If <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> refers to a fiber of execution, an instance of <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> that represents that fiber. Otherwise returns a default-constructed <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="fiber_mgmt/fiber.html#fiber_get_id"><code class="computeroutput">fiber::get_id()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="context_attach_bridgehead"></a> <span><a name="context_attach"></a></span> <a class="link" href="scheduling.html#context_attach">Member function <code class="computeroutput">attach</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">attach</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_scheduler</span><span class="special">()</span> <span class="special">==</span> <span class="keyword">nullptr</span></code> </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Attach fiber <code class="computeroutput"><span class="identifier">f</span></code> to scheduler running <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_scheduler</span><span class="special">()</span> <span class="special">!=</span> <span class="keyword">nullptr</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> A typical call: <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">active</span><span class="special">()-&gt;</span><span class="identifier">attach</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span></code> </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">f</span></code> must not be the running fiber&#8217;s context. It must not be <a class="link" href="overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> or terminated. It must not be a <code class="computeroutput"><span class="identifier">pinned_context</span></code>. It must be currently detached. It must not currently be linked into an <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementation&#8217;s ready queue. Most of these conditions are implied by <code class="computeroutput"><span class="identifier">f</span></code> being owned by an <code class="computeroutput"><span class="identifier">algorithm</span></code> implementation: that is, it has been passed to <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a> but has not yet been returned by <a class="link" href="scheduling.html#algorithm_pick_next"><code class="computeroutput">algorithm::pick_next()</code></a>. Typically a <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> implementation would call <code class="computeroutput"><span class="identifier">attach</span><span class="special">()</span></code> with the <code class="computeroutput"><span class="identifier">context</span><span class="special">*</span></code> it is about to return. It must first remove <code class="computeroutput"><span class="identifier">f</span></code> from its ready queue. You should never pass a <code class="computeroutput"><span class="identifier">pinned_context</span></code> to <code class="computeroutput"><span class="identifier">attach</span><span class="special">()</span></code> because you should never have called its <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> method in the first place. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_detach_bridgehead"></a> <span><a name="context_detach"></a></span> <a class="link" href="scheduling.html#context_detach">Member function <code class="computeroutput">detach</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">detach</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="special">(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_scheduler</span><span class="special">()</span> <span class="special">!=</span> <span class="keyword">nullptr</span><span class="special">)</span> <span class="special">&amp;&amp;</span> <span class="special">!</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">is_context</span><span class="special">(</span><span class="identifier">pinned_context</span><span class="special">)</span></code> </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Detach fiber <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> from its scheduler running <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_scheduler</span><span class="special">()</span> <span class="special">==</span> <span class="keyword">nullptr</span></code> </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This method must be called on the thread with which the fiber is currently associated. <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> must not be the running fiber&#8217;s context. It must not be <a class="link" href="overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> or terminated. It must not be a <code class="computeroutput"><span class="identifier">pinned_context</span></code>. It must not be detached already. It must not already be linked into an <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementation&#8217;s ready queue. Most of these conditions are implied by <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> being passed to <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a>; an <code class="computeroutput"><span class="identifier">awakened</span><span class="special">()</span></code> implementation must, however, test for <code class="computeroutput"><span class="identifier">pinned_context</span></code>. It must call <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> <span class="emphasis"><em>before</em></span> linking <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> into its ready queue. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> In particular, it is erroneous to attempt to migrate a fiber from one thread to another by calling both <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">attach</span><span class="special">()</span></code> in the <a class="link" href="scheduling.html#algorithm_pick_next"><code class="computeroutput">algorithm::pick_next()</code></a> method. <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> is called on the intended destination thread. <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> must be called on the fiber&#8217;s original thread. You must call <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> in the corresponding <code class="computeroutput"><span class="identifier">awakened</span><span class="special">()</span></code> method. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Unless you intend make a fiber available for potential migration to a different thread, you should call neither <code class="computeroutput"><span class="identifier">detach</span><span class="special">()</span></code> nor <code class="computeroutput"><span class="identifier">attach</span><span class="special">()</span></code> with its <code class="computeroutput"><span class="identifier">context</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_is_context_bridgehead"></a> <span><a name="context_is_context"></a></span> <a class="link" href="scheduling.html#context_is_context">Member function <code class="computeroutput">is_context</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">is_context</span><span class="special">(</span> <span class="identifier">type</span> <span class="identifier">t</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is of the specified type. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">type</span><span class="special">::</span><span class="identifier">worker_context</span></code> here means any fiber not special to the library. For <code class="computeroutput"><span class="identifier">type</span><span class="special">::</span><span class="identifier">main_context</span></code> the <code class="computeroutput"><span class="identifier">context</span></code> is associated with the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber of the thread: the one implicitly created by the thread itself, rather than one explicitly created by <span class="bold"><strong>Boost.Fiber</strong></span>. For <code class="computeroutput"><span class="identifier">type</span><span class="special">::</span><span class="identifier">dispatcher_context</span></code> the <code class="computeroutput"><span class="identifier">context</span></code> is associated with a <span class="quote">&#8220;<span class="quote">dispatching</span>&#8221;</span> fiber, responsible for dispatching awakened fibers to a scheduler&#8217;s ready-queue. The <span class="quote">&#8220;<span class="quote">dispatching</span>&#8221;</span> fiber is an implementation detail of the fiber manager. The context of the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> or <span class="quote">&#8220;<span class="quote">dispatching</span>&#8221;</span> fiber &#8212; any fiber for which <code class="computeroutput"><span class="identifier">is_context</span><span class="special">(</span><span class="identifier">pinned_context</span><span class="special">)</span></code> is <code class="computeroutput"><span class="keyword">true</span></code> &#8212; must never be passed to <a class="link" href="scheduling.html#context_detach"><code class="computeroutput">context::detach()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_is_terminated_bridgehead"></a> <span><a name="context_is_terminated"></a></span> <a class="link" href="scheduling.html#context_is_terminated">Member function <code class="computeroutput">is_terminated</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">is_terminated</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is no longer a valid context. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The <code class="computeroutput"><span class="identifier">context</span></code> has returned from its fiber-function and is no longer considered a valid context. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_ready_is_linked_bridgehead"></a> <span><a name="context_ready_is_linked"></a></span> <a class="link" href="scheduling.html#context_ready_is_linked">Member function <code class="computeroutput">ready_is_linked</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">ready_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is stored in an <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementation&#8217;s ready-queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Specifically, this method indicates whether <a class="link" href="scheduling.html#context_ready_link"><code class="computeroutput">context::ready_link()</code></a> has been called on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. <code class="computeroutput"><span class="identifier">ready_is_linked</span><span class="special">()</span></code> has no information about participation in any other containers. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_remote_ready_is_linked_bridgehead"></a> <span><a name="context_remote_ready_is_linked"></a></span> <a class="link" href="scheduling.html#context_remote_ready_is_linked">Member function <code class="computeroutput">remote_ready_is_linked</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">remote_ready_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is stored in the fiber manager&#8217;s remote-ready-queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">context</span></code> signaled as ready by another thread is first stored in the fiber manager&#8217;s remote-ready-queue. This is the mechanism by which the fiber manager protects an <a class="link" href="scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a> implementation from cross-thread <a class="link" href="scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a> calls. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_wait_is_linked_bridgehead"></a> <span><a name="context_wait_is_linked"></a></span> <a class="link" href="scheduling.html#context_wait_is_linked">Member function <code class="computeroutput">wait_is_linked</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">wait_is_linked</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is stored in the wait-queue of some synchronization object. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The <code class="computeroutput"><span class="identifier">context</span></code> of a fiber waiting on a synchronization object (e.g. <code class="computeroutput"><span class="identifier">mutex</span></code>, <code class="computeroutput"><span class="identifier">condition_variable</span></code> etc.) is stored in the wait-queue of that synchronization object. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_ready_link_bridgehead"></a> <span><a name="context_ready_link"></a></span> <a class="link" href="scheduling.html#context_ready_link">Member function <code class="computeroutput">ready_link</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">ready_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;</span> <span class="identifier">lst</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Stores <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in ready-queue <code class="computeroutput"><span class="identifier">lst</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Argument <code class="computeroutput"><span class="identifier">lst</span></code> must be a doubly-linked list from <a href="http://www.boost.org/doc/libs/release/libs/intrusive/index.html" target="_top">Boost.Intrusive</a>, e.g. an instance of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">scheduler</span><span class="special">::</span><span class="identifier">ready_queue_t</span></code>. Specifically, it must be a <a href="http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive</span><span class="special">::</span><span class="identifier">list</span></code></a> compatible with the <code class="computeroutput"><span class="identifier">list_member_hook</span></code> stored in the <code class="computeroutput"><span class="identifier">context</span></code> object. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_remote_ready_link_bridgehead"></a> <span><a name="context_remote_ready_link"></a></span> <a class="link" href="scheduling.html#context_remote_ready_link">Member function <code class="computeroutput">remote_ready_link</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">remote_ready_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;</span> <span class="identifier">lst</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Stores <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in remote-ready-queue <code class="computeroutput"><span class="identifier">lst</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Argument <code class="computeroutput"><span class="identifier">lst</span></code> must be a doubly-linked list from <a href="http://www.boost.org/doc/libs/release/libs/intrusive/index.html" target="_top">Boost.Intrusive</a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_wait_link_bridgehead"></a> <span><a name="context_wait_link"></a></span> <a class="link" href="scheduling.html#context_wait_link">Member function <code class="computeroutput">wait_link</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">List</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_link</span><span class="special">(</span> <span class="identifier">List</span> <span class="special">&amp;</span> <span class="identifier">lst</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Stores <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in wait-queue <code class="computeroutput"><span class="identifier">lst</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Argument <code class="computeroutput"><span class="identifier">lst</span></code> must be a doubly-linked list from <a href="http://www.boost.org/doc/libs/release/libs/intrusive/index.html" target="_top">Boost.Intrusive</a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_ready_unlink_bridgehead"></a> <span><a name="context_ready_unlink"></a></span> <a class="link" href="scheduling.html#context_ready_unlink">Member function <code class="computeroutput">ready_unlink</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">ready_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Removes <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> from ready-queue: undoes the effect of <a class="link" href="scheduling.html#context_ready_link"><code class="computeroutput">context::ready_link()</code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="context_remote_ready_unlink_bridgehead"></a> <span><a name="context_remote_ready_unlink"></a></span> <a class="link" href="scheduling.html#context_remote_ready_unlink">Member function <code class="computeroutput">remote_ready_unlink</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">remote_ready_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Removes <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> from remote-ready-queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="context_wait_unlink_bridgehead"></a> <span><a name="context_wait_unlink"></a></span> <a class="link" href="scheduling.html#context_wait_unlink">Member function <code class="computeroutput">wait_unlink</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">wait_unlink</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Removes <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> from wait-queue. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="context_suspend_bridgehead"></a> <span><a name="context_suspend"></a></span> <a class="link" href="scheduling.html#context_suspend">Member function <code class="computeroutput">suspend</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">suspend</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Suspends the running fiber (the fiber associated with <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>) until some other fiber passes <code class="computeroutput"><span class="keyword">this</span></code> to <a class="link" href="scheduling.html#context_schedule"><code class="computeroutput">context::schedule()</code></a>. <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is marked as not-ready, and control passes to the scheduler to select another fiber to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This is a low-level API potentially useful for integration with other frameworks. It is not intended to be directly invoked by a typical application program. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The burden is on the caller to arrange for a call to <code class="computeroutput"><span class="identifier">schedule</span><span class="special">()</span></code> with a pointer to <code class="computeroutput"><span class="keyword">this</span></code> at some future time. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_schedule_bridgehead"></a> <span><a name="context_schedule"></a></span> <a class="link" href="scheduling.html#context_schedule">Member function <code class="computeroutput">schedule</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">schedule</span><span class="special">(</span> <span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx</span> <span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Mark the fiber associated with context <code class="computeroutput"><span class="special">*</span><span class="identifier">ctx</span></code> as being ready to run. This does not immediately resume that fiber; rather it passes the fiber to the scheduler for subsequent resumption. If the scheduler is idle (has not returned from a call to <a class="link" href="scheduling.html#algorithm_suspend_until"><code class="computeroutput">algorithm::suspend_until()</code></a>), <a class="link" href="scheduling.html#algorithm_notify"><code class="computeroutput">algorithm::notify()</code></a> is called to wake it up. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This is a low-level API potentially useful for integration with other frameworks. It is not intended to be directly invoked by a typical application program. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> It is explicitly supported to call <code class="computeroutput"><span class="identifier">schedule</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">)</span></code> from a thread other than the one on which <code class="computeroutput"><span class="special">*</span><span class="identifier">ctx</span></code> is currently suspended. The corresponding fiber will be resumed on its original thread in due course. </p></dd> </dl> </div> <p> </p> <h5> <a name="context_less_bridgehead"></a> <span><a name="context_less"></a></span> <a class="link" href="scheduling.html#context_less">Non-member function <code class="computeroutput">operator&lt;()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">context</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">l</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> is <code class="computeroutput"><span class="keyword">true</span></code>, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fiber_mgmt/this_fiber.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html
repos/fiber/doc/html/fiber/fiber_mgmt.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Fiber management</title> <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="prev" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html" title="Implementations: fcontext_t, ucontext_t and WinFiber"> <link rel="next" href="fiber_mgmt/fiber.html" title="Class fiber"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> <td align="center"><a href="../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="fiber_mgmt/fiber.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="fiber.fiber_mgmt"></a><a class="link" href="fiber_mgmt.html" title="Fiber management">Fiber management</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="fiber_mgmt/fiber.html">Class <code class="computeroutput"><span class="identifier">fiber</span></code></a></span></dt> <dt><span class="section"><a href="fiber_mgmt/id.html">Class fiber::id</a></span></dt> <dt><span class="section"><a href="fiber_mgmt/this_fiber.html">Namespace this_fiber</a></span></dt> </dl></div> <h4> <a name="fiber.fiber_mgmt.h0"></a> <span><a name="fiber.fiber_mgmt.synopsis"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.synopsis">Synopsis</a> </h4> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">all</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">fiber</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">SchedAlgo</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">use_scheduling_algorithm</span><span class="special">(</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">();</span> <span class="keyword">namespace</span> <span class="identifier">algo</span> <span class="special">{</span> <span class="keyword">struct</span> <span class="identifier">algorithm</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">algorithm_with_properties</span><span class="special">;</span> <span class="keyword">class</span> <span class="identifier">round_robin</span><span class="special">;</span> <span class="keyword">class</span> <span class="identifier">shared_round_robin</span><span class="special">;</span> <span class="special">}}</span> <span class="keyword">namespace</span> <span class="identifier">this_fiber</span> <span class="special">{</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">yield</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">();</span> <span class="special">}</span> </pre> <h4> <a name="fiber.fiber_mgmt.h1"></a> <span><a name="fiber.fiber_mgmt.tutorial"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.tutorial">Tutorial</a> </h4> <p> Each <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> represents a micro-thread which will be launched and managed cooperatively by a scheduler. Objects of type <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> are move-only. </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f1</span><span class="special">;</span> <span class="comment">// not-a-fiber</span> <span class="keyword">void</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f2</span><span class="special">(</span> <span class="identifier">some_fn</span><span class="special">);</span> <span class="identifier">f1</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">f2</span><span class="special">);</span> <span class="comment">// f2 moved to f1</span> <span class="special">}</span> </pre> <h4> <a name="fiber.fiber_mgmt.h2"></a> <span><a name="fiber.fiber_mgmt.launching"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.launching">Launching</a> </h4> <p> A new fiber is launched by passing an object of a callable type that can be invoked with no parameters. If the object must not be copied or moved, then <span class="emphasis"><em>std::ref</em></span> can be used to pass in a reference to the function object. In this case, the user must ensure that the referenced object outlives the newly-created fiber. </p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">callable</span> <span class="special">{</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()();</span> <span class="special">};</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">copies_are_safe</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">callable</span> <span class="identifier">x</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">x</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// x is destroyed, but the newly-created fiber has a copy, so this is OK</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">oops</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">callable</span> <span class="identifier">x</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="comment">// x is destroyed, but the newly-created fiber still has a reference</span> <span class="comment">// this leads to undefined behaviour</span> </pre> <p> The spawned <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> does not immediately start running. It is enqueued in the list of ready-to-run fibers, and will run when the scheduler gets around to it. </p> <a name="exceptions"></a><h4> <a name="fiber.fiber_mgmt.h3"></a> <span><a name="fiber.fiber_mgmt.exceptions"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.exceptions">Exceptions</a> </h4> <p> An exception escaping from the function or callable object passed to the <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> constructor calls <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">terminate</span><span class="special">()</span></code>. If you need to know which exception was thrown, use <a class="link" href="synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> or <a class="link" href="synchronization/futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a>. </p> <h4> <a name="fiber.fiber_mgmt.h4"></a> <span><a name="fiber.fiber_mgmt.detaching"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.detaching">Detaching</a> </h4> <p> A <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> can be detached by explicitly invoking the <a class="link" href="fiber_mgmt/fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a> member function. After <a class="link" href="fiber_mgmt/fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a> is called on a fiber object, that object represents <span class="emphasis"><em>not-a-fiber</em></span>. The fiber object may then safely be destroyed. </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">some_fn</span><span class="special">).</span><span class="identifier">detach</span><span class="special">();</span> </pre> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides a number of ways to wait for a running fiber to complete. You can coordinate even with a detached fiber using a <a class="link" href="synchronization/mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>, or <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a>, or any of the other <a class="link" href="synchronization.html#synchronization">synchronization objects</a> provided by the library. </p> <p> If a detached fiber is still running when the thread&#8217;s main fiber terminates, the thread will not shut down. </p> <h4> <a name="fiber.fiber_mgmt.h5"></a> <span><a name="fiber.fiber_mgmt.joining"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.joining">Joining</a> </h4> <p> In order to wait for a fiber to finish, the <a class="link" href="fiber_mgmt/fiber.html#fiber_join"><code class="computeroutput">fiber::join()</code></a> member function of the <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object can be used. <a class="link" href="fiber_mgmt/fiber.html#fiber_join"><code class="computeroutput">fiber::join()</code></a> will block until the <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object has completed. </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">some_fn</span><span class="special">()</span> <span class="special">{</span> <span class="special">...</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">(</span> <span class="identifier">some_fn</span><span class="special">);</span> <span class="special">...</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> If the fiber has already completed, then <a class="link" href="fiber_mgmt/fiber.html#fiber_join"><code class="computeroutput">fiber::join()</code></a> returns immediately and the joined <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object becomes <span class="emphasis"><em>not-a-fiber</em></span>. </p> <h4> <a name="fiber.fiber_mgmt.h6"></a> <span><a name="fiber.fiber_mgmt.destruction"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.destruction">Destruction</a> </h4> <p> When a <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object representing a valid execution context (the fiber is <a class="link" href="fiber_mgmt/fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>) is destroyed, the program terminates. If you intend the fiber to outlive the <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object that launched it, use the <a class="link" href="fiber_mgmt/fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a> method. </p> <pre class="programlisting"><span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">(</span> <span class="identifier">some_fn</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// std::terminate() will be called</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">some_fn</span><span class="special">);</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> <span class="comment">// okay, program continues</span> </pre> <a name="class_fiber_id"></a><h4> <a name="fiber.fiber_mgmt.h7"></a> <span><a name="fiber.fiber_mgmt.fiber_ids"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.fiber_ids">Fiber IDs</a> </h4> <p> Objects of class <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> can be used to identify fibers. Each running <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> has a unique <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> obtainable from the corresponding <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> by calling the <a class="link" href="fiber_mgmt/fiber.html#fiber_get_id"><code class="computeroutput">fiber::get_id()</code></a> member function. Objects of class <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> can be copied, and used as keys in associative containers: the full range of comparison operators is provided. They can also be written to an output stream using the stream insertion operator, though the output format is unspecified. </p> <p> Each instance of <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> either refers to some fiber, or <span class="emphasis"><em>not-a-fiber</em></span>. Instances that refer to <span class="emphasis"><em>not-a-fiber</em></span> compare equal to each other, but not equal to any instances that refer to an actual fiber. The comparison operators on <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> yield a total order for every non-equal <a class="link" href="fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a>. </p> <a name="class_launch"></a><h4> <a name="fiber.fiber_mgmt.h8"></a> <span><a name="fiber.fiber_mgmt.enumeration__code__phrase_role__identifier__launch__phrase___code_"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt.enumeration__code__phrase_role__identifier__launch__phrase___code_">Enumeration <code class="computeroutput"><span class="identifier">launch</span></code></a> </h4> <p> <code class="computeroutput"><span class="identifier">launch</span></code> specifies whether control passes immediately into a newly-launched fiber. </p> <pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">launch</span> <span class="special">{</span> <span class="identifier">dispatch</span><span class="special">,</span> <span class="identifier">post</span> <span class="special">};</span> </pre> <h4> <a name="fiber.fiber_mgmt.h9"></a> <span><a name="fiber.fiber_mgmt._code__phrase_role__identifier__dispatch__phrase___code_"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt._code__phrase_role__identifier__dispatch__phrase___code_"><code class="computeroutput"><span class="identifier">dispatch</span></code></a> </h4> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> A fiber launched with <code class="computeroutput"><span class="identifier">launch</span> <span class="special">==</span> <span class="identifier">dispatch</span></code> is entered immediately. In other words, launching a fiber with <code class="computeroutput"><span class="identifier">dispatch</span></code> suspends the caller (the previously-running fiber) until the fiber scheduler has a chance to resume it later. </p></dd> </dl> </div> <h4> <a name="fiber.fiber_mgmt.h10"></a> <span><a name="fiber.fiber_mgmt._code__phrase_role__identifier__post__phrase___code_"></a></span><a class="link" href="fiber_mgmt.html#fiber.fiber_mgmt._code__phrase_role__identifier__post__phrase___code_"><code class="computeroutput"><span class="identifier">post</span></code></a> </h4> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> A fiber launched with <code class="computeroutput"><span class="identifier">launch</span> <span class="special">==</span> <span class="identifier">post</span></code> is passed to the fiber scheduler as ready, but it is not yet entered. The caller (the previously-running fiber) continues executing. The newly-launched fiber will be entered when the fiber scheduler has a chance to resume it later. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> If <code class="computeroutput"><span class="identifier">launch</span></code> is not explicitly specified, <code class="computeroutput"><span class="identifier">post</span></code> is the default. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview/implementations__fcontext_t__ucontext_t_and_winfiber.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="fiber_mgmt/fiber.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/when_any/when_all_functionality.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_all functionality</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any / when_all functionality"> <link rel="prev" href="when_any/when_any__a_dubious_alternative.html" title="when_any, a dubious alternative"> <link rel="next" href="when_all_functionality/when_all__simple_completion.html" title="when_all, simple completion"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any/when_any__a_dubious_alternative.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all_functionality/when_all__simple_completion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.when_any.when_all_functionality"></a><a class="link" href="when_all_functionality.html" title="when_all functionality">when_all functionality</a> </h3></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="when_all_functionality/when_all__simple_completion.html">when_all, simple completion</a></span></dt> <dt><span class="section"><a href="when_all_functionality/when_all__return_values.html">when_all, return values</a></span></dt> <dt><span class="section"><a href="when_all_functionality/when_all_until_first_exception.html">when_all until first exception</a></span></dt> <dt><span class="section"><a href="when_all_functionality/wait_all__collecting_all_exceptions.html">wait_all, collecting all exceptions</a></span></dt> <dt><span class="section"><a href="when_all_functionality/when_all__heterogeneous_types.html">when_all, heterogeneous types</a></span></dt> </dl></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any/when_any__a_dubious_alternative.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all_functionality/when_all__simple_completion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/when_any/when_any.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any / when_all functionality"> <link rel="prev" href="../when_any.html" title="when_any / when_all functionality"> <link rel="next" href="when_any/when_any__simple_completion.html" title="when_any, simple completion"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_any.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any/when_any__simple_completion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.when_any.when_any"></a><a class="link" href="when_any.html" title="when_any">when_any</a> </h3></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="when_any/when_any__simple_completion.html">when_any, simple completion</a></span></dt> <dt><span class="section"><a href="when_any/when_any__return_value.html">when_any, return value</a></span></dt> <dt><span class="section"><a href="when_any/when_any__produce_first_outcome__whether_result_or_exception.html">when_any, produce first outcome, whether result or exception</a></span></dt> <dt><span class="section"><a href="when_any/when_any__produce_first_success.html">when_any, produce first success</a></span></dt> <dt><span class="section"><a href="when_any/when_any__heterogeneous_types.html">when_any, heterogeneous types</a></span></dt> <dt><span class="section"><a href="when_any/when_any__a_dubious_alternative.html">when_any, a dubious alternative</a></span></dt> </dl></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_any.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any/when_any__simple_completion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_all_functionality/when_all_until_first_exception.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_all until first exception</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_all_functionality.html" title="when_all functionality"> <link rel="prev" href="when_all__return_values.html" title="when_all, return values"> <link rel="next" href="wait_all__collecting_all_exceptions.html" title="wait_all, collecting all exceptions"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all__return_values.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="wait_all__collecting_all_exceptions.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_all_functionality.when_all_until_first_exception"></a><a class="link" href="when_all_until_first_exception.html" title="when_all until first exception">when_all until first exception</a> </h4></div></div></div> <p> Naturally, just as with <a class="link" href="../when_any/when_any__produce_first_outcome__whether_result_or_exception.html#wait_first_outcome"><code class="computeroutput"><span class="identifier">wait_first_outcome</span><span class="special">()</span></code></a>, we can elaborate <a class="link" href="when_all__return_values.html#wait_all_values"><code class="computeroutput"><span class="identifier">wait_all_values</span><span class="special">()</span></code></a> and <a class="link" href="when_all__return_values.html#wait_all_values_source"><code class="computeroutput"><span class="identifier">wait_all_values_source</span><span class="special">()</span></code></a> by passing <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span></code> instead of plain <code class="computeroutput"><span class="identifier">T</span></code>. </p> <p> <a name="wait_all_until_error"></a><code class="computeroutput"><span class="identifier">wait_all_until_error</span><span class="special">()</span></code> pops that <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span></code> and calls its <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a>: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span> <span class="identifier">wait_all_until_error</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">vector_t</span><span class="special">;</span> <span class="identifier">vector_t</span> <span class="identifier">results</span><span class="special">;</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span> <span class="identifier">count</span><span class="special">);</span> <span class="comment">// get channel</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span><span class="special">(</span> <span class="identifier">wait_all_until_error_source</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">)</span> <span class="special">);</span> <span class="comment">// fill results vector</span> <span class="identifier">future_t</span> <span class="identifier">future</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">(</span> <span class="identifier">future</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">);</span> <span class="special">}</span> <span class="comment">// return vector to caller</span> <span class="keyword">return</span> <span class="identifier">results</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> For example: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">thrown</span><span class="special">;</span> <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">values</span> <span class="special">=</span> <span class="identifier">wait_all_until_error</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"waue_late"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"waue_middle"</span><span class="special">,</span> <span class="number">100</span><span class="special">,</span> <span class="keyword">true</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"waue_early"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">thrown</span> <span class="special">=</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">();</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_all_until_error(fail) threw '"</span> <span class="special">&lt;&lt;</span> <span class="identifier">thrown</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> </pre> <p> </p> <p> <a name="wait_all_until_error_source"></a>Naturally this complicates the API for <code class="computeroutput"><span class="identifier">wait_all_until_error_source</span><span class="special">()</span></code>. The caller must both retrieve a <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span></code> and call its <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> method. It would, of course, be possible to return a fa&#231;ade over the consumer end of the queue that would implicitly perform the <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> and return a simple <code class="computeroutput"><span class="identifier">T</span></code> (or throw). </p> <p> The implementation is just as you would expect. Notice, however, that we can reuse <a class="link" href="../when_any/when_any__produce_first_outcome__whether_result_or_exception.html#wait_first_outcome_impl"><code class="computeroutput"><span class="identifier">wait_first_outcome_impl</span><span class="special">()</span></code></a>, passing the <code class="computeroutput"><span class="identifier">nqueue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> rather than <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>. </p> <p> </p> <pre class="programlisting"><span class="comment">// Return a shared_ptr&lt;buffered_channel&lt;future&lt;T&gt;&gt;&gt; from which the caller can</span> <span class="comment">// get() each new result as it arrives, until 'closed'.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">wait_all_until_error_source</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="comment">// make the channel</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// and make an nchannel facade to close it after 'count' items</span> <span class="keyword">auto</span> <span class="identifier">ncp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">nchannel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// pass that nchannel facade to all the relevant fibers</span> <span class="identifier">wait_first_outcome_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">ncp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// then return the channel for consumer</span> <span class="keyword">return</span> <span class="identifier">chanp</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> For example: </p> <p> </p> <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span> <span class="special">=</span> <span class="identifier">wait_all_until_error_source</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wauess_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wauess_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wauess_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">future_t</span> <span class="identifier">future</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">(</span> <span class="identifier">future</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">value</span><span class="special">(</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_all_until_error_source(success) =&gt; '"</span> <span class="special">&lt;&lt;</span> <span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all__return_values.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="wait_all__collecting_all_exceptions.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_all_functionality/when_all__return_values.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_all, return values</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_all_functionality.html" title="when_all functionality"> <link rel="prev" href="when_all__simple_completion.html" title="when_all, simple completion"> <link rel="next" href="when_all_until_first_exception.html" title="when_all until first exception"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all__simple_completion.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all_until_first_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_all_functionality.when_all__return_values"></a><a class="link" href="when_all__return_values.html" title="when_all, return values">when_all, return values</a> </h4></div></div></div> <p> As soon as we want to collect return values from all the task functions, we can see right away how to reuse <a class="link" href="../when_any/when_any__return_value.html#wait_first_value"><code class="computeroutput"><span class="identifier">wait_first_value</span><span class="special">()</span></code></a>'s queue&lt;T&gt; for the purpose. All we have to do is avoid closing it after the first value! </p> <p> But in fact, collecting multiple values raises an interesting question: do we <span class="emphasis"><em>really</em></span> want to wait until the slowest of them has arrived? Wouldn't we rather process each result as soon as it becomes available? </p> <p> Fortunately we can present both APIs. Let's define <code class="computeroutput"><span class="identifier">wait_all_values_source</span><span class="special">()</span></code> to return <code class="computeroutput"><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;</span></code>. </p> <p> <a name="wait_all_values"></a>Given <code class="computeroutput"><span class="identifier">wait_all_values_source</span><span class="special">()</span></code>, it's straightforward to implement <code class="computeroutput"><span class="identifier">wait_all_values</span><span class="special">()</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span> <span class="identifier">wait_all_values</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">vector_t</span><span class="special">;</span> <span class="identifier">vector_t</span> <span class="identifier">results</span><span class="special">;</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span> <span class="identifier">count</span><span class="special">);</span> <span class="comment">// get channel</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span> <span class="special">=</span> <span class="identifier">wait_all_values_source</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// fill results vector</span> <span class="identifier">return_t</span> <span class="identifier">value</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">value</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span> <span class="identifier">value</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// return vector to caller</span> <span class="keyword">return</span> <span class="identifier">results</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> It might be called like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">values</span> <span class="special">=</span> <span class="identifier">wait_all_values</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wav_late"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wav_middle"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wav_early"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> </pre> <p> </p> <p> As you can see from the loop in <code class="computeroutput"><span class="identifier">wait_all_values</span><span class="special">()</span></code>, instead of requiring its caller to count values, we define <code class="computeroutput"><span class="identifier">wait_all_values_source</span><span class="special">()</span></code> to <a class="link" href="../../synchronization/channels/buffered_channel.html#buffered_channel_close"><code class="computeroutput">buffered_channel::close()</code></a> the queue when done. But how do we do that? Each producer fiber is independent. It has no idea whether it is the last one to <a class="link" href="../../synchronization/channels/buffered_channel.html#buffered_channel_push"><code class="computeroutput">buffered_channel::push()</code></a> a value. </p> <p> <a name="wait_nqueue"></a>We can address that problem with a counting fa&#231;ade for the <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;&gt;</span></code>. In fact, our fa&#231;ade need only support the producer end of the queue. </p> <p> [wait_nqueue] </p> <p> <a name="wait_all_values_source"></a>Armed with <code class="computeroutput"><span class="identifier">nqueue</span><span class="special">&lt;&gt;</span></code>, we can implement <code class="computeroutput"><span class="identifier">wait_all_values_source</span><span class="special">()</span></code>. It starts just like <a class="link" href="../when_any/when_any__return_value.html#wait_first_value"><code class="computeroutput"><span class="identifier">wait_first_value</span><span class="special">()</span></code></a>. The difference is that we wrap the <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> with an <code class="computeroutput"><span class="identifier">nqueue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> to pass to the producer fibers. </p> <p> Then, of course, instead of popping the first value, closing the queue and returning it, we simply return the <code class="computeroutput"><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">queue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;</span></code>. </p> <p> </p> <pre class="programlisting"><span class="comment">// Return a shared_ptr&lt;buffered_channel&lt;T&gt;&gt; from which the caller can</span> <span class="comment">// retrieve each new result as it arrives, until 'closed'.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">wait_all_values_source</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="comment">// make the channel</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// and make an nchannel facade to close it after 'count' items</span> <span class="keyword">auto</span> <span class="identifier">ncp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">nchannel</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// pass that nchannel facade to all the relevant fibers</span> <span class="identifier">wait_all_values_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">ncp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// then return the channel for consumer</span> <span class="keyword">return</span> <span class="identifier">chanp</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> For example: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span> <span class="special">=</span> <span class="identifier">wait_all_values_source</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wavs_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wavs_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wavs_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">value</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">value</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_all_values_source() =&gt; '"</span> <span class="special">&lt;&lt;</span> <span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> <a name="wait_all_values_impl"></a><code class="computeroutput"><span class="identifier">wait_all_values_impl</span><span class="special">()</span></code> really is just like <a class="link" href="../when_any/when_any__return_value.html#wait_first_value_impl"><code class="computeroutput"><span class="identifier">wait_first_value_impl</span><span class="special">()</span></code></a> except for the use of <code class="computeroutput"><span class="identifier">nqueue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> rather than <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_all_values_impl</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">nchannel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="special">[</span><span class="identifier">chan</span><span class="special">,</span> <span class="identifier">function</span><span class="special">](){</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">push</span><span class="special">(</span><span class="identifier">function</span><span class="special">());</span> <span class="special">}).</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all__simple_completion.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all_until_first_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_all_functionality/when_all__heterogeneous_types.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_all, heterogeneous types</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_all_functionality.html" title="when_all functionality"> <link rel="prev" href="wait_all__collecting_all_exceptions.html" title="wait_all, collecting all exceptions"> <link rel="next" href="../../integration.html" title="Sharing a Thread with Another Main Loop"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="wait_all__collecting_all_exceptions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../integration.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_all_functionality.when_all__heterogeneous_types"></a><a class="link" href="when_all__heterogeneous_types.html" title="when_all, heterogeneous types">when_all, heterogeneous types</a> </h4></div></div></div> <p> But what about the case when we must wait for all results of different types? </p> <p> We can present an API that is frankly quite cool. Consider a sample struct: </p> <p> </p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Data</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">str</span><span class="special">;</span> <span class="keyword">double</span> <span class="identifier">inexact</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">exact</span><span class="special">;</span> <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">out</span><span class="special">,</span> <span class="identifier">Data</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">);</span> <span class="special">...</span> <span class="special">};</span> </pre> <p> </p> <p> Let's fill its members from task functions all running concurrently: </p> <p> </p> <pre class="programlisting"><span class="identifier">Data</span> <span class="identifier">data</span> <span class="special">=</span> <span class="identifier">wait_all_members</span><span class="special">&lt;</span> <span class="identifier">Data</span> <span class="special">&gt;(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wams_left"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="number">3.14</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="number">17</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_all_members&lt;Data&gt;(success) =&gt; "</span> <span class="special">&lt;&lt;</span> <span class="identifier">data</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> </pre> <p> </p> <p> Note that for this case, we abandon the notion of capturing the earliest result first, and so on: we must fill exactly the passed struct in left-to-right order. </p> <p> That permits a beautifully simple implementation: </p> <p> </p> <pre class="programlisting"><span class="comment">// Explicitly pass Result. This can be any type capable of being initialized</span> <span class="comment">// from the results of the passed functions, such as a struct.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Result</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">Result</span> <span class="identifier">wait_all_members</span><span class="special">(</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Run each of the passed functions on a separate fiber, passing all their</span> <span class="comment">// futures to helper function for processing.</span> <span class="keyword">return</span> <span class="identifier">wait_all_members_get</span><span class="special">&lt;</span> <span class="identifier">Result</span> <span class="special">&gt;(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">async</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Result</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Futures</span> <span class="special">&gt;</span> <span class="identifier">Result</span> <span class="identifier">wait_all_members_get</span><span class="special">(</span> <span class="identifier">Futures</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">futures</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Fetch the results from the passed futures into Result's initializer</span> <span class="comment">// list. It's true that the get() calls here will block the implicit</span> <span class="comment">// iteration over futures -- but that doesn't matter because we won't be</span> <span class="comment">// done until the slowest of them finishes anyway. As results are</span> <span class="comment">// processed in argument-list order rather than order of completion, the</span> <span class="comment">// leftmost get() to throw an exception will cause that exception to</span> <span class="comment">// propagate to the caller.</span> <span class="keyword">return</span> <span class="identifier">Result</span><span class="special">{</span> <span class="identifier">futures</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">...</span> <span class="special">};</span> <span class="special">}</span> </pre> <p> </p> <p> It is tempting to try to implement <code class="computeroutput"><span class="identifier">wait_all_members</span><span class="special">()</span></code> as a one-liner like this: </p> <pre class="programlisting"><span class="keyword">return</span> <span class="identifier">Result</span><span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">async</span><span class="special">(</span><span class="identifier">functions</span><span class="special">).</span><span class="identifier">get</span><span class="special">()...</span> <span class="special">};</span> </pre> <p> The trouble with this tactic is that it would serialize all the task functions. The runtime makes a single pass through <code class="computeroutput"><span class="identifier">functions</span></code>, calling <a class="link" href="../../synchronization/futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a> for each and then immediately calling <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> on its returned <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code>. That blocks the implicit loop. The above is almost equivalent to writing: </p> <pre class="programlisting"><span class="keyword">return</span> <span class="identifier">Result</span><span class="special">{</span> <span class="identifier">functions</span><span class="special">()...</span> <span class="special">};</span> </pre> <p> in which, of course, there is no concurrency at all. </p> <p> Passing the argument pack through a function-call boundary (<code class="computeroutput"><span class="identifier">wait_all_members_get</span><span class="special">()</span></code>) forces the runtime to make <span class="emphasis"><em>two</em></span> passes: one in <code class="computeroutput"><span class="identifier">wait_all_members</span><span class="special">()</span></code> to collect the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code>s from all the <code class="computeroutput"><span class="identifier">async</span><span class="special">()</span></code> calls, the second in <code class="computeroutput"><span class="identifier">wait_all_members_get</span><span class="special">()</span></code> to fetch each of the results. </p> <p> As noted in comments, within the <code class="computeroutput"><span class="identifier">wait_all_members_get</span><span class="special">()</span></code> parameter pack expansion pass, the blocking behavior of <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> becomes irrelevant. Along the way, we will hit the <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> for the slowest task function; after that every subsequent <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> will complete in trivial time. </p> <p> By the way, we could also use this same API to fill a vector or other collection: </p> <p> </p> <pre class="programlisting"><span class="comment">// If we don't care about obtaining results as soon as they arrive, and we</span> <span class="comment">// prefer a result vector in passed argument order rather than completion</span> <span class="comment">// order, wait_all_members() is another possible implementation of</span> <span class="comment">// wait_all_until_error().</span> <span class="keyword">auto</span> <span class="identifier">strings</span> <span class="special">=</span> <span class="identifier">wait_all_members</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="special">&gt;(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wamv_left"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wamv_middle"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wamv_right"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_all_members&lt;vector&gt;() =&gt;"</span><span class="special">;</span> <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">str</span> <span class="special">:</span> <span class="identifier">strings</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">" '"</span> <span class="special">&lt;&lt;</span> <span class="identifier">str</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="wait_all__collecting_all_exceptions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../integration.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_all_functionality/when_all__simple_completion.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_all, simple completion</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_all_functionality.html" title="when_all functionality"> <link rel="prev" href="../when_all_functionality.html" title="when_all functionality"> <link rel="next" href="when_all__return_values.html" title="when_all, return values"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all__return_values.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_all_functionality.when_all__simple_completion"></a><a class="link" href="when_all__simple_completion.html" title="when_all, simple completion">when_all, simple completion</a> </h4></div></div></div> <p> For the case in which we must wait for <span class="emphasis"><em>all</em></span> task functions to complete &#8212; but we don't need results (or expect exceptions) from any of them &#8212; we can write <code class="computeroutput"><span class="identifier">wait_all_simple</span><span class="special">()</span></code> that looks remarkably like <a class="link" href="../when_any/when_any__simple_completion.html#wait_first_simple"><code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code></a>. The difference is that instead of our <a class="link" href="../when_any/when_any__simple_completion.html#wait_done"><code class="computeroutput"><span class="identifier">Done</span></code></a> class, we instantiate a <a class="link" href="../../synchronization/barriers.html#class_barrier"><code class="computeroutput">barrier</code></a> and call its <a class="link" href="../../synchronization/barriers.html#barrier_wait"><code class="computeroutput">barrier::wait()</code></a>. </p> <p> We initialize the <code class="computeroutput"><span class="identifier">barrier</span></code> with <code class="computeroutput"><span class="special">(</span><span class="identifier">count</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code> because we are launching <code class="computeroutput"><span class="identifier">count</span></code> fibers, plus the <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> call within <code class="computeroutput"><span class="identifier">wait_all_simple</span><span class="special">()</span></code> itself. </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_all_simple</span><span class="special">(</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// Initialize a barrier(count+1) because we'll immediately wait on it. We</span> <span class="comment">// don't want to wake up until 'count' more fibers wait on it. Even though</span> <span class="comment">// we'll stick around until the last of them completes, use shared_ptr</span> <span class="comment">// anyway because it's easier to be confident about lifespan issues.</span> <span class="keyword">auto</span> <span class="identifier">barrier</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">barrier</span> <span class="special">&gt;(</span> <span class="identifier">count</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">wait_all_simple_impl</span><span class="special">(</span> <span class="identifier">barrier</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="identifier">barrier</span><span class="special">-&gt;</span><span class="identifier">wait</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> As stated above, the only difference between <code class="computeroutput"><span class="identifier">wait_all_simple_impl</span><span class="special">()</span></code> and <a class="link" href="../when_any/when_any__simple_completion.html#wait_first_simple_impl"><code class="computeroutput"><span class="identifier">wait_first_simple_impl</span><span class="special">()</span></code></a> is that the former calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code> rather than <code class="computeroutput"><span class="identifier">Done</span><span class="special">::</span><span class="identifier">notify</span><span class="special">()</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_all_simple_impl</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">barrier</span> <span class="special">&gt;</span> <span class="identifier">barrier</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="special">[](</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">barrier</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">barrier</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&amp;</span> <span class="identifier">function</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="identifier">function</span><span class="special">();</span> <span class="identifier">barrier</span><span class="special">-&gt;</span><span class="identifier">wait</span><span class="special">();</span> <span class="special">},</span> <span class="identifier">barrier</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">)).</span><span class="identifier">detach</span><span class="special">();</span> <span class="identifier">wait_all_simple_impl</span><span class="special">(</span> <span class="identifier">barrier</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <p> You might call it like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">wait_all_simple</span><span class="special">(</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"was_long"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"was_medium"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"was_short"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> </pre> <p> </p> <p> Control will not return from the <code class="computeroutput"><span class="identifier">wait_all_simple</span><span class="special">()</span></code> call until the last of its task functions has completed. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all__return_values.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_all_functionality/wait_all__collecting_all_exceptions.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>wait_all, collecting all exceptions</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_all_functionality.html" title="when_all functionality"> <link rel="prev" href="when_all_until_first_exception.html" title="when_all until first exception"> <link rel="next" href="when_all__heterogeneous_types.html" title="when_all, heterogeneous types"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all_until_first_exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_all_functionality.wait_all__collecting_all_exceptions"></a><a class="link" href="wait_all__collecting_all_exceptions.html" title="wait_all, collecting all exceptions">wait_all, collecting all exceptions</a> </h4></div></div></div> <p> <a name="wait_all_collect_errors"></a>Given <a class="link" href="when_all_until_first_exception.html#wait_all_until_error_source"><code class="computeroutput"><span class="identifier">wait_all_until_error_source</span><span class="special">()</span></code></a>, it might be more reasonable to make a <code class="computeroutput"><span class="identifier">wait_all_</span><span class="special">...()</span></code> that collects <span class="emphasis"><em>all</em></span> errors instead of presenting only the first: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span> <span class="identifier">wait_all_collect_errors</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">vector_t</span><span class="special">;</span> <span class="identifier">vector_t</span> <span class="identifier">results</span><span class="special">;</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span> <span class="identifier">count</span><span class="special">);</span> <span class="identifier">exception_list</span> <span class="identifier">exceptions</span><span class="special">(</span><span class="string">"wait_all_collect_errors() exceptions"</span><span class="special">);</span> <span class="comment">// get channel</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span><span class="special">(</span> <span class="identifier">wait_all_until_error_source</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">)</span> <span class="special">);</span> <span class="comment">// fill results and/or exceptions vectors</span> <span class="identifier">future_t</span> <span class="identifier">future</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">(</span> <span class="identifier">future</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">exp</span> <span class="special">=</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get_exception_ptr</span><span class="special">();</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">exp</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">);</span> <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span> <span class="identifier">exceptions</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span> <span class="identifier">exp</span><span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="comment">// if there were any exceptions, throw</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">exceptions</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">throw</span> <span class="identifier">exceptions</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// no exceptions: return vector to caller</span> <span class="keyword">return</span> <span class="identifier">results</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> The implementation is a simple variation on <a class="link" href="../when_any/when_any__produce_first_success.html#wait_first_success"><code class="computeroutput"><span class="identifier">wait_first_success</span><span class="special">()</span></code></a>, using the same <a class="link" href="../when_any/when_any__produce_first_success.html#exception_list"><code class="computeroutput"><span class="identifier">exception_list</span></code></a> exception class. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_all_until_first_exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_all__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__simple_completion.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, simple completion</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="../when_any.html" title="when_any"> <link rel="next" href="when_any__return_value.html" title="when_any, return value"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_any.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__return_value.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__simple_completion"></a><a name="wait_first_simple_section"></a><a class="link" href="when_any__simple_completion.html" title="when_any, simple completion">when_any, simple completion</a> </h4></div></div></div> <p> The simplest case is when you only need to know that the first of a set of asynchronous tasks has completed &#8212; but you don't need to obtain a return value, and you're confident that they will not throw exceptions. </p> <p> <a name="wait_done"></a>For this we introduce a <code class="computeroutput"><span class="identifier">Done</span></code> class to wrap a <code class="computeroutput"><span class="keyword">bool</span></code> variable with a <a class="link" href="../../synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> and a <a class="link" href="../../synchronization/mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>: </p> <p> </p> <pre class="programlisting"><span class="comment">// Wrap canonical pattern for condition_variable + bool flag</span> <span class="keyword">struct</span> <span class="identifier">Done</span> <span class="special">{</span> <span class="keyword">private</span><span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable</span> <span class="identifier">cond</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mutex</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">ready</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">Done</span> <span class="special">&gt;</span> <span class="identifier">ptr</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lock</span><span class="special">(</span> <span class="identifier">mutex</span><span class="special">);</span> <span class="identifier">cond</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lock</span><span class="special">,</span> <span class="special">[</span><span class="keyword">this</span><span class="special">](){</span> <span class="keyword">return</span> <span class="identifier">ready</span><span class="special">;</span> <span class="special">});</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="special">{</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lock</span><span class="special">(</span> <span class="identifier">mutex</span><span class="special">);</span> <span class="identifier">ready</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// release mutex</span> <span class="identifier">cond</span><span class="special">.</span><span class="identifier">notify_one</span><span class="special">();</span> <span class="special">}</span> <span class="special">};</span> </pre> <p> </p> <p> The pattern we follow throughout this section is to pass a <a href="http://www.cplusplus.com/reference/memory/shared_ptr/" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;&gt;</span></code></a> to the relevant synchronization object to the various tasks' fiber functions. This eliminates nagging questions about the lifespan of the synchronization object relative to the last of the fibers. </p> <p> <a name="wait_first_simple"></a><code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code> uses that tactic for <a class="link" href="when_any__simple_completion.html#wait_done"><code class="computeroutput"><span class="identifier">Done</span></code></a>: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_first_simple</span><span class="special">(</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Use shared_ptr because each function's fiber will bind it separately,</span> <span class="comment">// and we're going to return before the last of them completes.</span> <span class="keyword">auto</span> <span class="identifier">done</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">Done</span> <span class="special">&gt;()</span> <span class="special">);</span> <span class="identifier">wait_first_simple_impl</span><span class="special">(</span> <span class="identifier">done</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="identifier">done</span><span class="special">-&gt;</span><span class="identifier">wait</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> <a name="wait_first_simple_impl"></a><code class="computeroutput"><span class="identifier">wait_first_simple_impl</span><span class="special">()</span></code> is an ordinary recursion over the argument pack, capturing <code class="computeroutput"><span class="identifier">Done</span><span class="special">::</span><span class="identifier">ptr</span></code> for each new fiber: </p> <p> </p> <pre class="programlisting"><span class="comment">// Degenerate case: when there are no functions to wait for, return</span> <span class="comment">// immediately.</span> <span class="keyword">void</span> <span class="identifier">wait_first_simple_impl</span><span class="special">(</span> <span class="identifier">Done</span><span class="special">::</span><span class="identifier">ptr</span><span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="comment">// When there's at least one function to wait for, launch it and recur to</span> <span class="comment">// process the rest.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_first_simple_impl</span><span class="special">(</span> <span class="identifier">Done</span><span class="special">::</span><span class="identifier">ptr</span> <span class="identifier">done</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="special">[</span><span class="identifier">done</span><span class="special">,</span> <span class="identifier">function</span><span class="special">](){</span> <span class="identifier">function</span><span class="special">();</span> <span class="identifier">done</span><span class="special">-&gt;</span><span class="identifier">notify</span><span class="special">();</span> <span class="special">}).</span><span class="identifier">detach</span><span class="special">();</span> <span class="identifier">wait_first_simple_impl</span><span class="special">(</span> <span class="identifier">done</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <p> The body of the fiber's lambda is extremely simple, as promised: call the function, notify <a class="link" href="when_any__simple_completion.html#wait_done"><code class="computeroutput"><span class="identifier">Done</span></code></a> when it returns. The first fiber to do so allows <code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code> to return &#8212; which is why it's useful to have <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">Done</span><span class="special">&gt;</span></code> manage the lifespan of our <code class="computeroutput"><span class="identifier">Done</span></code> object rather than declaring it as a stack variable in <code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code>. </p> <p> This is how you might call it: </p> <p> </p> <pre class="programlisting"><span class="identifier">wait_first_simple</span><span class="special">(</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfs_long"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfs_medium"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfs_short"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> </pre> <p> </p> <p> In this example, control resumes after <code class="computeroutput"><span class="identifier">wait_first_simple</span><span class="special">()</span></code> when <a class="link" href="../../when_any.html#wait_sleeper"><code class="computeroutput"><span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfs_short"</span><span class="special">,</span> <span class="number">50</span><span class="special">)</span></code></a> completes &#8212; even though the other two <code class="computeroutput"><span class="identifier">sleeper</span><span class="special">()</span></code> fibers are still running. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../when_any.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__return_value.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__heterogeneous_types.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, heterogeneous types</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="when_any__produce_first_success.html" title="when_any, produce first success"> <link rel="next" href="when_any__a_dubious_alternative.html" title="when_any, a dubious alternative"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__produce_first_success.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__a_dubious_alternative.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__heterogeneous_types"></a><a class="link" href="when_any__heterogeneous_types.html" title="when_any, heterogeneous types">when_any, heterogeneous types</a> </h4></div></div></div> <p> We would be remiss to ignore the case in which the various task functions have distinct return types. That means that the value returned by the first of them might have any one of those types. We can express that with <a href="http://www.boost.org/doc/libs/release/doc/html/variant.html" target="_top">Boost.Variant</a>. </p> <p> To keep the example simple, we'll revert to pretending that none of them can throw an exception. That makes <code class="computeroutput"><span class="identifier">wait_first_value_het</span><span class="special">()</span></code> strongly resemble <a class="link" href="when_any__return_value.html#wait_first_value"><code class="computeroutput"><span class="identifier">wait_first_value</span><span class="special">()</span></code></a>. We can actually reuse <a class="link" href="when_any__return_value.html#wait_first_value_impl"><code class="computeroutput"><span class="identifier">wait_first_value_impl</span><span class="special">()</span></code></a>, merely passing <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">T0</span><span class="special">,</span> <span class="identifier">T1</span><span class="special">,</span> <span class="special">...&gt;</span></code> as the queue's value type rather than the common <code class="computeroutput"><span class="identifier">T</span></code>! </p> <p> Naturally this could be extended to use <a class="link" href="when_any__produce_first_success.html#wait_first_success"><code class="computeroutput"><span class="identifier">wait_first_success</span><span class="special">()</span></code></a> semantics instead. </p> <p> </p> <pre class="programlisting"><span class="comment">// No need to break out the first Fn for interface function: let the compiler</span> <span class="comment">// complain if empty.</span> <span class="comment">// Our functions have different return types, and we might have to return any</span> <span class="comment">// of them. Use a variant, expanding std::result_of&lt;Fn()&gt;::type for each Fn in</span> <span class="comment">// parameter pack.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">variant</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fns</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">...</span> <span class="special">&gt;</span> <span class="identifier">wait_first_value_het</span><span class="special">(</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Use buffered_channel&lt;boost::variant&lt;T1, T2, ...&gt;&gt;; see remarks above.</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">variant</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fns</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">...</span> <span class="special">&gt;</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// launch all the relevant fibers</span> <span class="identifier">wait_first_value_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// retrieve the first value</span> <span class="identifier">return_t</span> <span class="identifier">value</span><span class="special">(</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// close the channel: no subsequent push() has to succeed</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">();</span> <span class="keyword">return</span> <span class="identifier">value</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> It might be called like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">variant</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">wait_first_value_het</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfvh_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="number">3.14</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="number">17</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_first_value_het() =&gt; "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">==</span> <span class="number">17</span><span class="special">);</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__produce_first_success.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__a_dubious_alternative.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__produce_first_success.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, produce first success</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="when_any__produce_first_outcome__whether_result_or_exception.html" title="when_any, produce first outcome, whether result or exception"> <link rel="next" href="when_any__heterogeneous_types.html" title="when_any, heterogeneous types"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__produce_first_outcome__whether_result_or_exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__produce_first_success"></a><a class="link" href="when_any__produce_first_success.html" title="when_any, produce first success">when_any, produce first success</a> </h4></div></div></div> <p> One scenario for <span class="quote">&#8220;<span class="quote">when_any</span>&#8221;</span> functionality is when we're redundantly contacting some number of possibly-unreliable web services. Not only might they be slow &#8212; any one of them might produce a failure rather than the desired result. </p> <p> In such a case, <a class="link" href="when_any__produce_first_outcome__whether_result_or_exception.html#wait_first_outcome"><code class="computeroutput"><span class="identifier">wait_first_outcome</span><span class="special">()</span></code></a> isn't the right approach. If one of the services produces an error quickly, while another follows up with a real answer, we don't want to prefer the error just because it arrived first! </p> <p> Given the <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&gt;</span></code> we already constructed for <code class="computeroutput"><span class="identifier">wait_first_outcome</span><span class="special">()</span></code>, though, we can readily recast the interface function to deliver the first <span class="emphasis"><em>successful</em></span> result. </p> <p> That does beg the question: what if <span class="emphasis"><em>all</em></span> the task functions throw an exception? In that case we'd probably better know about it. </p> <p> <a name="exception_list"></a>The <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4407.html#parallel.exceptions.synopsis" target="_top">C++ Parallelism Draft Technical Specification</a> proposes a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_list</span></code> exception capable of delivering a collection of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>s. Until that becomes universally available, let's fake up an <code class="computeroutput"><span class="identifier">exception_list</span></code> of our own: </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">exception_list</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">exception_list</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">what</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span> <span class="identifier">what</span><span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="special">&gt;</span> <span class="identifier">bundle_t</span><span class="special">;</span> <span class="comment">// N4407 proposed std::exception_list API</span> <span class="keyword">typedef</span> <span class="identifier">bundle_t</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">iterator</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">bundle_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span> <span class="special">}</span> <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">bundle_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> <span class="special">}</span> <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">bundle_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> <span class="special">}</span> <span class="comment">// extension to populate</span> <span class="keyword">void</span> <span class="identifier">add</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">ep</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">bundle_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span> <span class="identifier">ep</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">private</span><span class="special">:</span> <span class="identifier">bundle_t</span> <span class="identifier">bundle_</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> Now we can build <code class="computeroutput"><span class="identifier">wait_first_success</span><span class="special">()</span></code>, using <a class="link" href="when_any__produce_first_outcome__whether_result_or_exception.html#wait_first_outcome_impl"><code class="computeroutput"><span class="identifier">wait_first_outcome_impl</span><span class="special">()</span></code></a>. </p> <p> Instead of retrieving only the first <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> from the queue, we must now loop over <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> items. Of course we must limit that iteration! If we launch only <code class="computeroutput"><span class="identifier">count</span></code> producer fibers, the <code class="computeroutput"><span class="special">(</span><span class="identifier">count</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code><sup>st</sup> <a class="link" href="../../synchronization/channels/buffered_channel.html#buffered_channel_pop"><code class="computeroutput">buffered_channel::pop()</code></a> call would block forever. </p> <p> Given a ready <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code>, we can distinguish failure by calling <a class="link" href="../../synchronization/futures/future.html#future_get_exception_ptr"><code class="computeroutput">future::get_exception_ptr()</code></a>. If the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> in fact contains a result rather than an exception, <code class="computeroutput"><span class="identifier">get_exception_ptr</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">nullptr</span></code>. In that case, we can confidently call <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> to return that result to our caller. </p> <p> If the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code> is <span class="emphasis"><em>not</em></span> <code class="computeroutput"><span class="keyword">nullptr</span></code>, though, we collect it into our pending <code class="computeroutput"><span class="identifier">exception_list</span></code> and loop back for the next <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> from the queue. </p> <p> If we fall out of the loop &#8212; if every single task fiber threw an exception &#8212; we throw the <code class="computeroutput"><span class="identifier">exception_list</span></code> exception into which we've been collecting those <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>s. </p> <p> <a name="wait_first_success"></a> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">wait_first_success</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">count</span><span class="special">(</span> <span class="number">1</span> <span class="special">+</span> <span class="keyword">sizeof</span> <span class="special">...</span> <span class="special">(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// In this case, the value we pass through the channel is actually a</span> <span class="comment">// future -- which is already ready. future can carry either a value or an</span> <span class="comment">// exception.</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;::</span><span class="identifier">type</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// launch all the relevant fibers</span> <span class="identifier">wait_first_outcome_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// instantiate exception_list, just in case</span> <span class="identifier">exception_list</span> <span class="identifier">exceptions</span><span class="special">(</span><span class="string">"wait_first_success() produced only errors"</span><span class="special">);</span> <span class="comment">// retrieve up to 'count' results -- but stop there!</span> <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">count</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// retrieve the next future</span> <span class="identifier">future_t</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// retrieve exception_ptr if any</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">error</span><span class="special">(</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get_exception_ptr</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// if no error, then yay, return value</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">error</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// close the channel: no subsequent push() has to succeed</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">();</span> <span class="comment">// show caller the value we got</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> <span class="comment">// error is non-null: collect</span> <span class="identifier">exceptions</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span> <span class="identifier">error</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// We only arrive here when every passed function threw an exception.</span> <span class="comment">// Throw our collection to inform caller.</span> <span class="keyword">throw</span> <span class="identifier">exceptions</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> A call might look like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">wait_first_success</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfss_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">,</span> <span class="keyword">true</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfss_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfss_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_first_success(success) =&gt; "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="string">"wfss_second"</span><span class="special">);</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__produce_first_outcome__whether_result_or_exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__produce_first_outcome__whether_result_or_exception.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, produce first outcome, whether result or exception</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="when_any__return_value.html" title="when_any, return value"> <link rel="next" href="when_any__produce_first_success.html" title="when_any, produce first success"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__return_value.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__produce_first_success.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__produce_first_outcome__whether_result_or_exception"></a><a class="link" href="when_any__produce_first_outcome__whether_result_or_exception.html" title="when_any, produce first outcome, whether result or exception">when_any, produce first outcome, whether result or exception</a> </h4></div></div></div> <p> We may not be running in an environment in which we can guarantee no exception will be thrown by any of our task functions. In that case, the above implementations of <code class="computeroutput"><span class="identifier">wait_first_something</span><span class="special">()</span></code> would be na&#239;ve: as mentioned in <a class="link" href="../../fiber_mgmt.html#exceptions">the section on Fiber Management</a>, an uncaught exception in one of our task fibers would cause <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">terminate</span><span class="special">()</span></code> to be called. </p> <p> Let's at least ensure that such an exception would propagate to the fiber awaiting the first result. We can use <a class="link" href="../../synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> to transport either a return value or an exception. Therefore, we will change <a class="link" href="when_any__return_value.html#wait_first_value"><code class="computeroutput"><span class="identifier">wait_first_value</span><span class="special">()</span></code></a>'s <a class="link" href="../../synchronization/channels/buffered_channel.html#class_buffered_channel"><code class="computeroutput">buffered_channel&lt;&gt;</code></a> to hold <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span></code> items instead of simply <code class="computeroutput"><span class="identifier">T</span></code>. </p> <p> Once we have a <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> in hand, all we need do is call <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a>, which will either return the value or rethrow the exception. </p> <p> <a name="wait_first_outcome"></a> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">wait_first_outcome</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// In this case, the value we pass through the channel is actually a</span> <span class="comment">// future -- which is already ready. future can carry either a value or an</span> <span class="comment">// exception.</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">future_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">future_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// launch all the relevant fibers</span> <span class="identifier">wait_first_outcome_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// retrieve the first future</span> <span class="identifier">future_t</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// close the channel: no subsequent push() has to succeed</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">();</span> <span class="comment">// either return value or throw exception</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> So far so good &#8212; but there's a timing issue. How should we obtain the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> to <a class="link" href="../../synchronization/channels/buffered_channel.html#buffered_channel_push"><code class="computeroutput">buffered_channel::push()</code></a> on the queue? </p> <p> We could call <a class="link" href="../../synchronization/futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a>. That would certainly produce a <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> for the task function. The trouble is that it would return too quickly! We only want <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> items for <span class="emphasis"><em>completed</em></span> tasks on our <code class="computeroutput"><span class="identifier">queue</span><span class="special">&lt;&gt;</span></code>. In fact, we only want the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> for the one that completes first. If each fiber launched by <code class="computeroutput"><span class="identifier">wait_first_outcome</span><span class="special">()</span></code> were to <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code> the result of calling <code class="computeroutput"><span class="identifier">async</span><span class="special">()</span></code>, the queue would only ever report the result of the leftmost task item &#8212; <span class="emphasis"><em>not</em></span> the one that completes most quickly. </p> <p> Calling <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> on the future returned by <code class="computeroutput"><span class="identifier">async</span><span class="special">()</span></code> wouldn't be right. You can only call <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code> once per <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> instance! And if there were an exception, it would be rethrown inside the helper fiber at the producer end of the queue, rather than propagated to the consumer end. </p> <p> We could call <a class="link" href="../../synchronization/futures/future.html#future_wait"><code class="computeroutput">future::wait()</code></a>. That would block the helper fiber until the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> became ready, at which point we could <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code> it to be retrieved by <code class="computeroutput"><span class="identifier">wait_first_outcome</span><span class="special">()</span></code>. </p> <p> That would work &#8212; but there's a simpler tactic that avoids creating an extra fiber. We can wrap the task function in a <a class="link" href="../../synchronization/futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a>. While one naturally thinks of passing a <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code> to a new fiber &#8212; that is, in fact, what <code class="computeroutput"><span class="identifier">async</span><span class="special">()</span></code> does &#8212; in this case, we're already running in the helper fiber at the producer end of the queue! We can simply <span class="emphasis"><em>call</em></span> the <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code>. On return from that call, the task function has completed, meaning that the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> obtained from the <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code> is certain to be ready. At that point we can simply <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code> it to the queue. </p> <p> <a name="wait_first_outcome_impl"></a> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">CHANP</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_first_outcome_impl</span><span class="special">(</span> <span class="identifier">CHANP</span> <span class="identifier">chan</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="comment">// Use std::bind() here for C++11 compatibility. C++11 lambda capture</span> <span class="comment">// can't move a move-only Fn type, but bind() can. Let bind() move the</span> <span class="comment">// channel pointer and the function into the bound object, passing</span> <span class="comment">// references into the lambda.</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="special">[](</span> <span class="identifier">CHANP</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&amp;</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Instantiate a packaged_task to capture any exception thrown by</span> <span class="comment">// function.</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">T</span><span class="special">()</span> <span class="special">&gt;</span> <span class="identifier">task</span><span class="special">(</span> <span class="identifier">function</span><span class="special">);</span> <span class="comment">// Immediately run this packaged_task on same fiber. We want</span> <span class="comment">// function() to have completed BEFORE we push the future.</span> <span class="identifier">task</span><span class="special">();</span> <span class="comment">// Pass the corresponding future to consumer. Ignore</span> <span class="comment">// channel_op_status returned by push(): might be closed; we</span> <span class="comment">// simply don't care.</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">push</span><span class="special">(</span> <span class="identifier">task</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">()</span> <span class="special">);</span> <span class="special">},</span> <span class="identifier">chan</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">)).</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> Calling it might look like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">wait_first_outcome</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfos_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfos_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfos_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_first_outcome(success) =&gt; "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="string">"wfos_first"</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">thrown</span><span class="special">;</span> <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">wait_first_outcome</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfof_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">,</span> <span class="keyword">true</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfof_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfof_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">});</span> <span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">thrown</span> <span class="special">=</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">();</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_first_outcome(fail) threw '"</span> <span class="special">&lt;&lt;</span> <span class="identifier">thrown</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">thrown</span> <span class="special">==</span> <span class="string">"wfof_first"</span><span class="special">);</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__return_value.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__produce_first_success.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__return_value.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, return value</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="when_any__simple_completion.html" title="when_any, simple completion"> <link rel="next" href="when_any__produce_first_outcome__whether_result_or_exception.html" title="when_any, produce first outcome, whether result or exception"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__simple_completion.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__produce_first_outcome__whether_result_or_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__return_value"></a><a class="link" href="when_any__return_value.html" title="when_any, return value">when_any, return value</a> </h4></div></div></div> <p> It seems more useful to add the ability to capture the return value from the first of the task functions to complete. Again, we assume that none will throw an exception. </p> <p> One tactic would be to adapt our <a class="link" href="when_any__simple_completion.html#wait_done"><code class="computeroutput"><span class="identifier">Done</span></code></a> class to store the first of the return values, rather than a simple <code class="computeroutput"><span class="keyword">bool</span></code>. However, we choose instead to use a <a class="link" href="../../synchronization/channels/buffered_channel.html#class_buffered_channel"><code class="computeroutput">buffered_channel&lt;&gt;</code></a>. We'll only need to enqueue the first value, so we'll <a class="link" href="../../synchronization/channels/buffered_channel.html#buffered_channel_close"><code class="computeroutput">buffered_channel::close()</code></a> it once we've retrieved that value. Subsequent <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code> calls will return <code class="computeroutput"><span class="identifier">closed</span></code>. </p> <p> <a name="wait_first_value"></a> </p> <pre class="programlisting"><span class="comment">// Assume that all passed functions have the same return type. The return type</span> <span class="comment">// of wait_first_value() is the return type of the first passed function. It is</span> <span class="comment">// simply invalid to pass NO functions.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Fns</span> <span class="special">&gt;</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">wait_first_value</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">,</span> <span class="identifier">Fns</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of</span><span class="special">&lt;</span> <span class="identifier">Fn</span><span class="special">()</span> <span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">return_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">auto</span> <span class="identifier">chanp</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">channel_t</span> <span class="special">&gt;(</span> <span class="number">64</span><span class="special">)</span> <span class="special">);</span> <span class="comment">// launch all the relevant fibers</span> <span class="identifier">wait_first_value_impl</span><span class="special">&lt;</span> <span class="identifier">return_t</span> <span class="special">&gt;(</span> <span class="identifier">chanp</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fn</span> <span class="special">&gt;(</span> <span class="identifier">function</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span> <span class="identifier">Fns</span> <span class="special">&gt;(</span> <span class="identifier">functions</span><span class="special">)</span> <span class="special">...</span> <span class="special">);</span> <span class="comment">// retrieve the first value</span> <span class="identifier">return_t</span> <span class="identifier">value</span><span class="special">(</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// close the channel: no subsequent push() has to succeed</span> <span class="identifier">chanp</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">();</span> <span class="keyword">return</span> <span class="identifier">value</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <p> <a name="wait_first_value_impl"></a>The meat of the <code class="computeroutput"><span class="identifier">wait_first_value_impl</span><span class="special">()</span></code> function is as you might expect: </p> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait_first_value_impl</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">chan</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">function</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="special">[</span><span class="identifier">chan</span><span class="special">,</span> <span class="identifier">function</span><span class="special">](){</span> <span class="comment">// Ignore channel_op_status returned by push():</span> <span class="comment">// might be closed; we simply don't care.</span> <span class="identifier">chan</span><span class="special">-&gt;</span><span class="identifier">push</span><span class="special">(</span> <span class="identifier">function</span><span class="special">()</span> <span class="special">);</span> <span class="special">}).</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> It calls the passed function, pushes its return value and ignores the <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code> result. You might call it like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">wait_first_value</span><span class="special">(</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfv_third"</span><span class="special">,</span> <span class="number">150</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfv_second"</span><span class="special">,</span> <span class="number">100</span><span class="special">);</span> <span class="special">},</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">sleeper</span><span class="special">(</span><span class="string">"wfv_first"</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"wait_first_value() =&gt; "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="string">"wfv_first"</span><span class="special">);</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__simple_completion.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="when_any__produce_first_outcome__whether_result_or_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/when_any
repos/fiber/doc/html/fiber/when_any/when_any/when_any__a_dubious_alternative.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>when_any, a dubious alternative</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../when_any.html" title="when_any"> <link rel="prev" href="when_any__heterogeneous_types.html" title="when_any, heterogeneous types"> <link rel="next" href="../when_all_functionality.html" title="when_all functionality"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.when_any.when_any.when_any__a_dubious_alternative"></a><a class="link" href="when_any__a_dubious_alternative.html" title="when_any, a dubious alternative">when_any, a dubious alternative</a> </h4></div></div></div> <p> Certain topics in C++ can arouse strong passions, and exceptions are no exception. We cannot resist mentioning &#8212; for purely informational purposes &#8212; that when you need only the <span class="emphasis"><em>first</em></span> result from some number of concurrently-running fibers, it would be possible to pass a <code class="literal">shared_ptr&lt;<a class="link" href="../../synchronization/futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a>&gt;</code> to the participating fibers, then cause the initiating fiber to call <a class="link" href="../../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> on its <a class="link" href="../../synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a>. The first fiber to call <a class="link" href="../../synchronization/futures/promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> on that shared <code class="computeroutput"><span class="identifier">promise</span></code> will succeed; subsequent <code class="computeroutput"><span class="identifier">set_value</span><span class="special">()</span></code> calls on the same <code class="computeroutput"><span class="identifier">promise</span></code> instance will throw <code class="computeroutput"><span class="identifier">future_error</span></code>. </p> <p> Use this information at your own discretion. Beware the dark side. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="when_any__heterogeneous_types.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../when_any.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../when_all_functionality.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/gpu_computing/hip.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>ROCm/HIP</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../gpu_computing.html" title="GPU computing"> <link rel="prev" href="cuda.html" title="CUDA"> <link rel="next" href="../worker.html" title="Running with worker threads"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="cuda.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../worker.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.gpu_computing.hip"></a><a name="hip"></a><a class="link" href="hip.html" title="ROCm/HIP">ROCm/HIP</a> </h3></div></div></div> <p> <a href="http://github.com/ROCm-Developer-Tools/HIP/tree/roc-1.6.0/" target="_top">HIP</a> is part of the <a href="http://rocm.github.io/" target="_top">ROC (Radeon Open Compute)</a> platform for parallel computing on AMD and NVIDIA GPUs. The application programming interface of HIP gives access to GPU's instruction set and computation resources (Execution of compute kernels). </p> <h5> <a name="fiber.gpu_computing.hip.h0"></a> <span><a name="fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams"></a></span><a class="link" href="hip.html#fiber.gpu_computing.hip.synchronization_with_rocm_hip_streams">Synchronization with ROCm/HIP streams</a> </h5> <p> HIP operation such as compute kernels or memory transfer (between host and device) can be grouped/queued by HIP streams. are executed on the GPUs. Boost.Fiber enables a fiber to sleep (suspend) till a HIP stream has completed its operations. This enables applications to run other fibers on the CPU without the need to spawn an additional OS-threads. And resume the fiber when the HIP streams has finished. </p> <pre class="programlisting"><span class="identifier">__global__</span> <span class="keyword">void</span> <span class="identifier">kernel</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">size</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">idx</span> <span class="special">=</span> <span class="identifier">threadIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">+</span> <span class="identifier">blockIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">blockDim</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">idx</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">idx1</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">idx2</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">2</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span> <span class="keyword">float</span> <span class="identifier">as</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">a</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span> <span class="keyword">float</span> <span class="identifier">bs</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span> <span class="identifier">c</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">as</span> <span class="special">+</span> <span class="identifier">bs</span><span class="special">)</span> <span class="special">/</span> <span class="number">2</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">([&amp;</span><span class="identifier">done</span><span class="special">]{</span> <span class="identifier">hipStream_t</span> <span class="identifier">stream</span><span class="special">;</span> <span class="identifier">hipStreamCreate</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">);</span> <span class="keyword">int</span> <span class="identifier">size</span> <span class="special">=</span> <span class="number">1024</span> <span class="special">*</span> <span class="number">1024</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">full_size</span> <span class="special">=</span> <span class="number">20</span> <span class="special">*</span> <span class="identifier">size</span><span class="special">;</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_c</span><span class="special">;</span> <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span> <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span> <span class="identifier">hipHostMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_c</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipHostMallocDefault</span><span class="special">);</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_c</span><span class="special">;</span> <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">hipMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">minstd_rand</span> <span class="identifier">generator</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">distribution</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">6</span><span class="special">);</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">host_a</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span> <span class="identifier">host_b</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">+=</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">host_a</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">host_b</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="identifier">hipLaunchKernel</span><span class="special">(</span><span class="identifier">kernel</span><span class="special">,</span> <span class="identifier">dim3</span><span class="special">(</span><span class="identifier">size</span> <span class="special">/</span> <span class="number">256</span><span class="special">),</span> <span class="identifier">dim3</span><span class="special">(</span><span class="number">256</span><span class="special">),</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">,</span> <span class="identifier">size</span><span class="special">,</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">);</span> <span class="identifier">hipMemcpyAsync</span><span class="special">(</span> <span class="identifier">host_c</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">hipMemcpyDeviceToHost</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">auto</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">hip</span><span class="special">::</span><span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span> <span class="comment">// suspend fiber till HIP stream has finished</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">stream</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">0</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">hipSuccess</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">1</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"f1: GPU computation finished"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_a</span><span class="special">);</span> <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_b</span><span class="special">);</span> <span class="identifier">hipHostFree</span><span class="special">(</span> <span class="identifier">host_c</span><span class="special">);</span> <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">);</span> <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">);</span> <span class="identifier">hipFree</span><span class="special">(</span> <span class="identifier">dev_c</span><span class="special">);</span> <span class="identifier">hipStreamDestroy</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <h5> <a name="fiber.gpu_computing.hip.h1"></a> <span><a name="fiber.gpu_computing.hip.synopsis"></a></span><a class="link" href="hip.html#fiber.gpu_computing.hip.synopsis">Synopsis</a> </h5> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">hip</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">hip</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="identifier">st</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="hip_waitfor_bridgehead"></a> <span><a name="hip_waitfor"></a></span> <a class="link" href="hip.html#hip_waitfor">Non-member function <code class="computeroutput">hip::waitfor()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">hip</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">hip</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="identifier">st</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">hipStream_t</span><span class="special">,</span> <span class="identifier">hipError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">hipStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span> <span class="special">}}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Suspends active fiber till HIP stream has finished its operations. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> tuple of stream reference and the HIP stream status </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="cuda.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../worker.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/gpu_computing/cuda.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>CUDA</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../gpu_computing.html" title="GPU computing"> <link rel="prev" href="../gpu_computing.html" title="GPU computing"> <link rel="next" href="hip.html" title="ROCm/HIP"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hip.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.gpu_computing.cuda"></a><a name="cuda"></a><a class="link" href="cuda.html" title="CUDA">CUDA</a> </h3></div></div></div> <p> <a href="http://developer.nvidia.com/cuda-zone/" target="_top">CUDA (Compute Unified Device Architecture)</a> is a platform for parallel computing on NVIDIA GPUs. The application programming interface of CUDA gives access to GPU's instruction set and computation resources (Execution of compute kernels). </p> <h5> <a name="fiber.gpu_computing.cuda.h0"></a> <span><a name="fiber.gpu_computing.cuda.synchronization_with_cuda_streams"></a></span><a class="link" href="cuda.html#fiber.gpu_computing.cuda.synchronization_with_cuda_streams">Synchronization with CUDA streams</a> </h5> <p> CUDA operation such as compute kernels or memory transfer (between host and device) can be grouped/queued by CUDA streams. are executed on the GPUs. Boost.Fiber enables a fiber to sleep (suspend) till a CUDA stream has completed its operations. This enables applications to run other fibers on the CPU without the need to spawn an additional OS-threads. And resume the fiber when the CUDA streams has finished. </p> <pre class="programlisting"><span class="identifier">__global__</span> <span class="keyword">void</span> <span class="identifier">kernel</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">size</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">idx</span> <span class="special">=</span> <span class="identifier">threadIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">+</span> <span class="identifier">blockIdx</span><span class="special">.</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">blockDim</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">idx</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">idx1</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">idx2</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">idx</span> <span class="special">+</span> <span class="number">2</span><span class="special">)</span> <span class="special">%</span> <span class="number">256</span><span class="special">;</span> <span class="keyword">float</span> <span class="identifier">as</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">a</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">a</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span> <span class="keyword">float</span> <span class="identifier">bs</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx1</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">b</span><span class="special">[</span><span class="identifier">idx2</span><span class="special">])</span> <span class="special">/</span> <span class="number">3.0f</span><span class="special">;</span> <span class="identifier">c</span><span class="special">[</span><span class="identifier">idx</span><span class="special">]</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">as</span> <span class="special">+</span> <span class="identifier">bs</span><span class="special">)</span> <span class="special">/</span> <span class="number">2</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">([&amp;</span><span class="identifier">done</span><span class="special">]{</span> <span class="identifier">cudaStream_t</span> <span class="identifier">stream</span><span class="special">;</span> <span class="identifier">cudaStreamCreate</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">);</span> <span class="keyword">int</span> <span class="identifier">size</span> <span class="special">=</span> <span class="number">1024</span> <span class="special">*</span> <span class="number">1024</span><span class="special">;</span> <span class="keyword">int</span> <span class="identifier">full_size</span> <span class="special">=</span> <span class="number">20</span> <span class="special">*</span> <span class="identifier">size</span><span class="special">;</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">host_c</span><span class="special">;</span> <span class="identifier">cudaHostAlloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_a</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaHostAllocDefault</span><span class="special">);</span> <span class="identifier">cudaHostAlloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_b</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaHostAllocDefault</span><span class="special">);</span> <span class="identifier">cudaHostAlloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">host_c</span><span class="special">,</span> <span class="identifier">full_size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaHostAllocDefault</span><span class="special">);</span> <span class="keyword">int</span> <span class="special">*</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">dev_c</span><span class="special">;</span> <span class="identifier">cudaMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">cudaMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">cudaMalloc</span><span class="special">(</span> <span class="special">&amp;</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">minstd_rand</span> <span class="identifier">generator</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">distribution</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">6</span><span class="special">);</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">host_a</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span> <span class="identifier">host_b</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">distribution</span><span class="special">(</span> <span class="identifier">generator</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">full_size</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">+=</span> <span class="identifier">size</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">cudaMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">host_a</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="identifier">cudaMemcpyAsync</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">host_b</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaMemcpyHostToDevice</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="identifier">kernel</span><span class="special">&lt;&lt;&lt;</span> <span class="identifier">size</span> <span class="special">/</span> <span class="number">256</span><span class="special">,</span> <span class="number">256</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">stream</span> <span class="special">&gt;&gt;&gt;(</span> <span class="identifier">size</span><span class="special">,</span> <span class="identifier">dev_a</span><span class="special">,</span> <span class="identifier">dev_b</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">);</span> <span class="identifier">cudaMemcpyAsync</span><span class="special">(</span> <span class="identifier">host_c</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">dev_c</span><span class="special">,</span> <span class="identifier">size</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span> <span class="keyword">int</span><span class="special">),</span> <span class="identifier">cudaMemcpyDeviceToHost</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">auto</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">cuda</span><span class="special">::</span><span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span> <span class="comment">// suspend fiber till CUDA stream has finished</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">stream</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">0</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="identifier">cudaSuccess</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span> <span class="number">1</span> <span class="special">&gt;(</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"f1: GPU computation finished"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="identifier">cudaFreeHost</span><span class="special">(</span> <span class="identifier">host_a</span><span class="special">);</span> <span class="identifier">cudaFreeHost</span><span class="special">(</span> <span class="identifier">host_b</span><span class="special">);</span> <span class="identifier">cudaFreeHost</span><span class="special">(</span> <span class="identifier">host_c</span><span class="special">);</span> <span class="identifier">cudaFree</span><span class="special">(</span> <span class="identifier">dev_a</span><span class="special">);</span> <span class="identifier">cudaFree</span><span class="special">(</span> <span class="identifier">dev_b</span><span class="special">);</span> <span class="identifier">cudaFree</span><span class="special">(</span> <span class="identifier">dev_c</span><span class="special">);</span> <span class="identifier">cudaStreamDestroy</span><span class="special">(</span> <span class="identifier">stream</span><span class="special">);</span> <span class="special">});</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <h5> <a name="fiber.gpu_computing.cuda.h1"></a> <span><a name="fiber.gpu_computing.cuda.synopsis"></a></span><a class="link" href="cuda.html#fiber.gpu_computing.cuda.synopsis">Synopsis</a> </h5> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">cuda</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">cuda</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">cudaStream_t</span><span class="special">,</span> <span class="identifier">cudaError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">cudaStream_t</span> <span class="identifier">st</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">cudaStream_t</span><span class="special">,</span> <span class="identifier">cudaError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">cudaStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span> <span class="special">}}}</span> </pre> <p> </p> <h5> <a name="cuda_waitfor_bridgehead"></a> <span><a name="cuda_waitfor"></a></span> <a class="link" href="cuda.html#cuda_waitfor">Non-member function <code class="computeroutput">cuda::waitfor()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">cuda</span><span class="special">/</span><span class="identifier">waitfor</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">cuda</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">cudaStream_t</span><span class="special">,</span> <span class="identifier">cudaError_t</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">cudaStream_t</span> <span class="identifier">st</span><span class="special">);</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span> <span class="identifier">cudaStream_t</span><span class="special">,</span> <span class="identifier">cudaError_t</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">waitfor_all</span><span class="special">(</span> <span class="identifier">cudaStream_t</span> <span class="special">...</span> <span class="identifier">st</span><span class="special">);</span> <span class="special">}}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Suspends active fiber till CUDA stream has finished its operations. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> tuple of stream reference and the CUDA stream status </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gpu_computing.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hip.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/overview/implementations__fcontext_t__ucontext_t_and_winfiber.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Implementations: fcontext_t, ucontext_t and WinFiber</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../overview.html" title="Overview"> <link rel="prev" href="../overview.html" title="Overview"> <link rel="next" href="../fiber_mgmt.html" title="Fiber management"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber"></a><a name="implementation"></a><a class="link" href="implementations__fcontext_t__ucontext_t_and_winfiber.html" title="Implementations: fcontext_t, ucontext_t and WinFiber">Implementations: fcontext_t, ucontext_t and WinFiber</a> </h3></div></div></div> <p> <span class="bold"><strong>Boost.Fiber</strong></span> uses <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html" target="_top"><span class="emphasis"><em>call/cc</em></span></a> from <a href="http://www.boost.org/doc/libs/release/libs/context/index.html" target="_top">Boost.Context</a> as building-block. </p> <h5> <a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h0"></a> <span><a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t"></a></span><a class="link" href="implementations__fcontext_t__ucontext_t_and_winfiber.html#fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t">fcontext_t</a> </h5> <p> The implementation uses <code class="computeroutput"><span class="identifier">fcontext_t</span></code> per default. fcontext_t is based on assembler and not available for all platforms. It provides a much better performance than <code class="computeroutput"><span class="identifier">ucontext_t</span></code> (the context switch takes two magnitudes of order less CPU cycles; see section <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/performance.html" target="_top"><span class="emphasis"><em>performance</em></span></a>) and <code class="computeroutput"><span class="identifier">WinFiber</span></code>. </p> <h5> <a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h1"></a> <span><a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t"></a></span><a class="link" href="implementations__fcontext_t__ucontext_t_and_winfiber.html#fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t">ucontext_t</a> </h5> <p> As an alternative, <a href="https://en.wikipedia.org/wiki/Setcontext" target="_top"><code class="computeroutput"><span class="identifier">ucontext_t</span></code></a> can be used by compiling with <code class="computeroutput"><span class="identifier">BOOST_USE_UCONTEXT</span></code> and b2 property <code class="computeroutput"><span class="identifier">context</span><span class="special">-</span><span class="identifier">impl</span><span class="special">=</span><span class="identifier">ucontext</span></code>. <code class="computeroutput"><span class="identifier">ucontext_t</span></code> might be available on a broader range of POSIX-platforms but has some <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/rational.html#ucontext" target="_top"><span class="emphasis"><em>disadvantages</em></span></a> (for instance deprecated since POSIX.1-2003, not C99 conform). </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html" target="_top"><span class="emphasis"><em>call/cc</em></span></a> supports <a class="link" href="../stack.html#segmented"><span class="emphasis"><em>Segmented stacks</em></span></a> only with <code class="computeroutput"><span class="identifier">ucontext_t</span></code> as its implementation. </p></td></tr> </table></div> <h5> <a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.h2"></a> <span><a name="fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber"></a></span><a class="link" href="implementations__fcontext_t__ucontext_t_and_winfiber.html#fiber.overview.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber">WinFiber</a> </h5> <p> With <code class="computeroutput"><span class="identifier">BOOST_USE_WINFIB</span></code> and b2 property <code class="computeroutput"><span class="identifier">context</span><span class="special">-</span><span class="identifier">impl</span><span class="special">=</span><span class="identifier">winfib</span></code> Win32-Fibers are used as implementation for <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html" target="_top"><span class="emphasis"><em>call/cc</em></span></a>. </p> <p> Because the TIB (thread information block) is not fully described in the MSDN, it might be possible that not all required TIB-parts are swapped. </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> The first call of <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/cc.html" target="_top"><span class="emphasis"><em>call/cc</em></span></a> converts the thread into a Windows fiber by invoking <code class="computeroutput"><span class="identifier">ConvertThreadToFiber</span><span class="special">()</span></code>. If desired, <code class="computeroutput"><span class="identifier">ConvertFiberToThread</span><span class="special">()</span></code> has to be called by the user explicitly in order to release resources allocated by <code class="computeroutput"><span class="identifier">ConvertThreadToFiber</span><span class="special">()</span></code> (e.g. after using boost.context). </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/synchronization/mutex_types.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Mutex Types</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../synchronization.html" title="Synchronization"> <link rel="prev" href="../synchronization.html" title="Synchronization"> <link rel="next" href="conditions.html" title="Condition Variables"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../synchronization.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="conditions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.synchronization.mutex_types"></a><a class="link" href="mutex_types.html" title="Mutex Types">Mutex Types</a> </h3></div></div></div> <p> </p> <h5> <a name="class_mutex_bridgehead"></a> <span><a name="class_mutex"></a></span> <a class="link" href="mutex_types.html#class_mutex">Class <code class="computeroutput">mutex</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">mutex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">mutex</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">mutex</span><span class="special">();</span> <span class="special">~</span><span class="identifier">mutex</span><span class="special">();</span> <span class="identifier">mutex</span><span class="special">(</span> <span class="identifier">mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">mutex</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> <span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> <span class="special">};</span> <span class="special">}}</span> </pre> <p> <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a> provides an exclusive-ownership mutex. At most one fiber can own the lock on a given instance of <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a> at any time. Multiple concurrent calls to <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> shall be permitted. </p> <p> Any fiber blocked in <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code> is suspended until the owning fiber releases the lock by calling <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code>. </p> <p> </p> <h5> <a name="mutex_lock_bridgehead"></a> <span><a name="mutex_lock"></a></span> <a class="link" href="mutex_types.html#mutex_lock">Member function <code class="computeroutput">lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> The current fiber blocks until ownership can be obtained. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="mutex_try_lock_bridgehead"></a> <span><a name="mutex_try_lock"></a></span> <a class="link" href="mutex_types.html#mutex_try_lock">Member function <code class="computeroutput">try_lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber without blocking. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="mutex_unlock_bridgehead"></a> <span><a name="mutex_unlock"></a></span> <a class="link" href="mutex_types.html#mutex_unlock">Member function <code class="computeroutput">unlock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The current fiber owns <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Releases a lock on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>operation_not_permitted</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> does not own the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_timed_mutex_bridgehead"></a> <span><a name="class_timed_mutex"></a></span> <a class="link" href="mutex_types.html#class_timed_mutex">Class <code class="computeroutput">timed_mutex</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">timed_mutex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">timed_mutex</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">timed_mutex</span><span class="special">();</span> <span class="special">~</span><span class="identifier">timed_mutex</span><span class="special">();</span> <span class="identifier">timed_mutex</span><span class="special">(</span> <span class="identifier">timed_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">timed_mutex</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">timed_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> <span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <p> <a class="link" href="mutex_types.html#class_timed_mutex"><code class="computeroutput">timed_mutex</code></a> provides an exclusive-ownership mutex. At most one fiber can own the lock on a given instance of <a class="link" href="mutex_types.html#class_timed_mutex"><code class="computeroutput">timed_mutex</code></a> at any time. Multiple concurrent calls to <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock_until</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock_for</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> shall be permitted. </p> <p> </p> <h5> <a name="timed_mutex_lock_bridgehead"></a> <span><a name="timed_mutex_lock"></a></span> <a class="link" href="mutex_types.html#timed_mutex_lock">Member function <code class="computeroutput">lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> The current fiber blocks until ownership can be obtained. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="timed_mutex_try_lock_bridgehead"></a> <span><a name="timed_mutex_try_lock"></a></span> <a class="link" href="mutex_types.html#timed_mutex_try_lock">Member function <code class="computeroutput">try_lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber without blocking. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="timed_mutex_unlock_bridgehead"></a> <span><a name="timed_mutex_unlock"></a></span> <a class="link" href="mutex_types.html#timed_mutex_unlock">Member function <code class="computeroutput">unlock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The current fiber owns <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Releases a lock on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>operation_not_permitted</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> does not own the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="timed_mutex_try_lock_until_bridgehead"></a> <span><a name="timed_mutex_try_lock_until"></a></span> <a class="link" href="mutex_types.html#timed_mutex_try_lock_until">Templated member function <code class="computeroutput">try_lock_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <a class="link" href="mutex_types.html#timed_mutex_try_lock"><code class="computeroutput">timed_mutex::try_lock()</code></a>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code>, timeout-related exceptions. </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="timed_mutex_try_lock_for_bridgehead"></a> <span><a name="timed_mutex_try_lock_for"></a></span> <a class="link" href="mutex_types.html#timed_mutex_try_lock_for">Templated member function <code class="computeroutput">try_lock_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> The calling fiber doesn't own the mutex. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <a class="link" href="mutex_types.html#timed_mutex_try_lock"><code class="computeroutput">timed_mutex::try_lock()</code></a>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code>, timeout-related exceptions. </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> already owns the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_recursive_mutex_bridgehead"></a> <span><a name="class_recursive_mutex"></a></span> <a class="link" href="mutex_types.html#class_recursive_mutex">Class <code class="computeroutput">recursive_mutex</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">recursive_mutex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">recursive_mutex</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">recursive_mutex</span><span class="special">();</span> <span class="special">~</span><span class="identifier">recursive_mutex</span><span class="special">();</span> <span class="identifier">recursive_mutex</span><span class="special">(</span> <span class="identifier">recursive_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">recursive_mutex</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">recursive_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> <span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> <span class="special">};</span> <span class="special">}}</span> </pre> <p> <a class="link" href="mutex_types.html#class_recursive_mutex"><code class="computeroutput">recursive_mutex</code></a> provides an exclusive-ownership recursive mutex. At most one fiber can own the lock on a given instance of <a class="link" href="mutex_types.html#class_recursive_mutex"><code class="computeroutput">recursive_mutex</code></a> at any time. Multiple concurrent calls to <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> shall be permitted. A fiber that already has exclusive ownership of a given <a class="link" href="mutex_types.html#class_recursive_mutex"><code class="computeroutput">recursive_mutex</code></a> instance can call <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code> to acquire an additional level of ownership of the mutex. <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> must be called once for each level of ownership acquired by a single fiber before ownership can be acquired by another fiber. </p> <p> </p> <h5> <a name="recursive_mutex_lock_bridgehead"></a> <span><a name="recursive_mutex_lock"></a></span> <a class="link" href="mutex_types.html#recursive_mutex_lock">Member function <code class="computeroutput">lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The current fiber blocks until ownership can be obtained. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_mutex_try_lock_bridgehead"></a> <span><a name="recursive_mutex_try_lock"></a></span> <a class="link" href="mutex_types.html#recursive_mutex_try_lock">Member function <code class="computeroutput">try_lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber without blocking. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_mutex_unlock_bridgehead"></a> <span><a name="recursive_mutex_unlock"></a></span> <a class="link" href="mutex_types.html#recursive_mutex_unlock">Member function <code class="computeroutput">unlock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Releases a lock on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>operation_not_permitted</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> does not own the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_recursive_timed_mutex_bridgehead"></a> <span><a name="class_recursive_timed_mutex"></a></span> <a class="link" href="mutex_types.html#class_recursive_timed_mutex">Class <code class="computeroutput">recursive_timed_mutex</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">recursive_timed_mutex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">recursive_timed_mutex</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">recursive_timed_mutex</span><span class="special">();</span> <span class="special">~</span><span class="identifier">recursive_timed_mutex</span><span class="special">();</span> <span class="identifier">recursive_timed_mutex</span><span class="special">(</span> <span class="identifier">recursive_timed_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">recursive_timed_mutex</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">recursive_timed_mutex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> <span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <p> <a class="link" href="mutex_types.html#class_recursive_timed_mutex"><code class="computeroutput">recursive_timed_mutex</code></a> provides an exclusive-ownership recursive mutex. At most one fiber can own the lock on a given instance of <a class="link" href="mutex_types.html#class_recursive_timed_mutex"><code class="computeroutput">recursive_timed_mutex</code></a> at any time. Multiple concurrent calls to <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock_for</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock_until</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> shall be permitted. A fiber that already has exclusive ownership of a given <a class="link" href="mutex_types.html#class_recursive_timed_mutex"><code class="computeroutput">recursive_timed_mutex</code></a> instance can call <code class="computeroutput"><span class="identifier">lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">try_lock_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">try_lock_until</span><span class="special">()</span></code> to acquire an additional level of ownership of the mutex. <code class="computeroutput"><span class="identifier">unlock</span><span class="special">()</span></code> must be called once for each level of ownership acquired by a single fiber before ownership can be acquired by another fiber. </p> <p> </p> <h5> <a name="recursive_timed_mutex_lock_bridgehead"></a> <span><a name="recursive_timed_mutex_lock"></a></span> <a class="link" href="mutex_types.html#recursive_timed_mutex_lock">Member function <code class="computeroutput">lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">lock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The current fiber blocks until ownership can be obtained. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_timed_mutex_try_lock_bridgehead"></a> <span><a name="recursive_timed_mutex_try_lock"></a></span> <a class="link" href="mutex_types.html#recursive_timed_mutex_try_lock">Member function <code class="computeroutput">try_lock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">try_lock</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber without blocking. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_timed_mutex_unlock_bridgehead"></a> <span><a name="recursive_timed_mutex_unlock"></a></span> <a class="link" href="mutex_types.html#recursive_timed_mutex_unlock">Member function <code class="computeroutput">unlock</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">unlock</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Releases a lock on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lock_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>operation_not_permitted</strong></span>: if <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code> does not own the mutex. </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_timed_mutex_try_lock_until_bridgehead"></a> <span><a name="recursive_timed_mutex_try_lock_until"></a></span> <a class="link" href="mutex_types.html#recursive_timed_mutex_try_lock_until">Templated member function <code class="computeroutput">try_lock_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <a class="link" href="mutex_types.html#recursive_timed_mutex_try_lock"><code class="computeroutput">recursive_timed_mutex::try_lock()</code></a>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Timeout-related exceptions. </p></dd> </dl> </div> <p> </p> <h5> <a name="recursive_timed_mutex_try_lock_for_bridgehead"></a> <span><a name="recursive_timed_mutex_try_lock_for"></a></span> <a class="link" href="mutex_types.html#recursive_timed_mutex_try_lock_for">Templated member function <code class="computeroutput">try_lock_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">try_lock_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Attempt to obtain ownership for the current fiber. Blocks until ownership can be obtained, or the specified time is reached. If the specified time has already passed, behaves as <a class="link" href="mutex_types.html#recursive_timed_mutex_try_lock"><code class="computeroutput">recursive_timed_mutex::try_lock()</code></a>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if ownership was obtained for the current fiber, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Timeout-related exceptions. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../synchronization.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="conditions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/synchronization/channels.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Channels</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../synchronization.html" title="Synchronization"> <link rel="prev" href="barriers.html" title="Barriers"> <link rel="next" href="channels/buffered_channel.html" title="Buffered Channel"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="barriers.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="channels/buffered_channel.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.synchronization.channels"></a><a class="link" href="channels.html" title="Channels">Channels</a> </h3></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="channels/buffered_channel.html">Buffered Channel</a></span></dt> <dt><span class="section"><a href="channels/unbuffered_channel.html">Unbuffered Channel</a></span></dt> </dl></div> <p> A channel is a model to communicate and synchronize <code class="computeroutput"><span class="identifier">Threads</span> <span class="identifier">of</span> <span class="identifier">Execution</span></code> <sup>[<a name="fiber.synchronization.channels.f0" href="#ftn.fiber.synchronization.channels.f0" class="footnote">2</a>]</sup> via message passing. </p> <a name="class_channel_op_status"></a><h5> <a name="fiber.synchronization.channels.h0"></a> <span><a name="fiber.synchronization.channels.enumeration__code__phrase_role__identifier__channel_op_status__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels.enumeration__code__phrase_role__identifier__channel_op_status__phrase___code_">Enumeration <code class="computeroutput"><span class="identifier">channel_op_status</span></code></a> </h5> <p> channel operations return the state of the channel. </p> <pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">channel_op_status</span> <span class="special">{</span> <span class="identifier">success</span><span class="special">,</span> <span class="identifier">empty</span><span class="special">,</span> <span class="identifier">full</span><span class="special">,</span> <span class="identifier">closed</span><span class="special">,</span> <span class="identifier">timeout</span> <span class="special">};</span> </pre> <h5> <a name="fiber.synchronization.channels.h1"></a> <span><a name="fiber.synchronization.channels._code__phrase_role__identifier__success__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels._code__phrase_role__identifier__success__phrase___code_"><code class="computeroutput"><span class="identifier">success</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Operation was successful. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.channels.h2"></a> <span><a name="fiber.synchronization.channels._code__phrase_role__identifier__empty__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels._code__phrase_role__identifier__empty__phrase___code_"><code class="computeroutput"><span class="identifier">empty</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> channel is empty, operation failed. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.channels.h3"></a> <span><a name="fiber.synchronization.channels._code__phrase_role__identifier__full__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels._code__phrase_role__identifier__full__phrase___code_"><code class="computeroutput"><span class="identifier">full</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> channel is full, operation failed. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.channels.h4"></a> <span><a name="fiber.synchronization.channels._code__phrase_role__identifier__closed__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels._code__phrase_role__identifier__closed__phrase___code_"><code class="computeroutput"><span class="identifier">closed</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> channel is closed, operation failed. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.channels.h5"></a> <span><a name="fiber.synchronization.channels._code__phrase_role__identifier__timeout__phrase___code_"></a></span><a class="link" href="channels.html#fiber.synchronization.channels._code__phrase_role__identifier__timeout__phrase___code_"><code class="computeroutput"><span class="identifier">timeout</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The operation did not become ready before specified timeout elapsed. </p></dd> </dl> </div> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.synchronization.channels.f0" href="#fiber.synchronization.channels.f0" class="para">2</a>] </sup> The smallest ordered sequence of instructions that can be managed independently by a scheduler is called a <code class="computeroutput"><span class="identifier">Thread</span> <span class="identifier">of</span> <span class="identifier">Execution</span></code>. </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="barriers.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="channels/buffered_channel.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/synchronization/futures.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Futures</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../synchronization.html" title="Synchronization"> <link rel="prev" href="channels/unbuffered_channel.html" title="Unbuffered Channel"> <link rel="next" href="futures/future.html" title="Future"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="channels/unbuffered_channel.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="futures/future.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.synchronization.futures"></a><a class="link" href="futures.html" title="Futures">Futures</a> </h3></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="futures/future.html">Future</a></span></dt> <dt><span class="section"><a href="futures/promise.html">Template <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code></a></span></dt> <dt><span class="section"><a href="futures/packaged_task.html">Template <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code></a></span></dt> </dl></div> <h5> <a name="fiber.synchronization.futures.h0"></a> <span><a name="fiber.synchronization.futures.overview"></a></span><a class="link" href="futures.html#fiber.synchronization.futures.overview">Overview</a> </h5> <p> The futures library provides a means of handling asynchronous future values, whether those values are generated by another fiber, or on a single fiber in response to external stimuli, or on-demand. </p> <p> This is done through the provision of four class templates: <a class="link" href="futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> and <a class="link" href="futures/future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> which are used to retrieve the asynchronous results, and <a class="link" href="futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> and <a class="link" href="futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> which are used to generate the asynchronous results. </p> <p> An instance of <a class="link" href="futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> holds the one and only reference to a result. Ownership can be transferred between instances using the move constructor or move-assignment operator, but at most one instance holds a reference to a given asynchronous result. When the result is ready, it is returned from <a class="link" href="futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> by rvalue-reference to allow the result to be moved or copied as appropriate for the type. </p> <p> On the other hand, many instances of <a class="link" href="futures/future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> may reference the same result. Instances can be freely copied and assigned, and <a class="link" href="futures/future.html#shared_future_get"><code class="computeroutput">shared_future::get()</code></a> returns a <code class="computeroutput"><span class="keyword">const</span></code> reference so that multiple calls to <a class="link" href="futures/future.html#shared_future_get"><code class="computeroutput">shared_future::get()</code></a> are safe. You can move an instance of <a class="link" href="futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> into an instance of <a class="link" href="futures/future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a>, thus transferring ownership of the associated asynchronous result, but not vice-versa. </p> <p> <a class="link" href="futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a> is a simple way of running asynchronous tasks. A call to <code class="computeroutput"><span class="identifier">async</span><span class="special">()</span></code> spawns a fiber and returns a <a class="link" href="futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> that will deliver the result of the fiber function. </p> <h5> <a name="fiber.synchronization.futures.h1"></a> <span><a name="fiber.synchronization.futures.creating_asynchronous_values"></a></span><a class="link" href="futures.html#fiber.synchronization.futures.creating_asynchronous_values">Creating asynchronous values</a> </h5> <p> You can set the value in a future with either a <a class="link" href="futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> or a <a class="link" href="futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a>. A <a class="link" href="futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> is a callable object with <code class="computeroutput"><span class="keyword">void</span></code> return that wraps a function or callable object returning the specified type. When the <a class="link" href="futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> is invoked, it invokes the contained function in turn, and populates a future with the contained function's return value. This is an answer to the perennial question: <span class="quote">&#8220;<span class="quote">How do I return a value from a fiber?</span>&#8221;</span> Package the function you wish to run as a <a class="link" href="futures/packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> and pass the packaged task to the fiber constructor. The future retrieved from the packaged task can then be used to obtain the return value. If the function throws an exception, that is stored in the future in place of the return value. </p> <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">calculate_the_answer_to_life_the_universe_and_everything</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="number">42</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">packaged_task</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">()&gt;</span> <span class="identifier">pt</span><span class="special">(</span><span class="identifier">calculate_the_answer_to_life_the_universe_and_everything</span><span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">fi</span><span class="special">=</span><span class="identifier">pt</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">();</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">pt</span><span class="special">)).</span><span class="identifier">detach</span><span class="special">();</span> <span class="comment">// launch task on a fiber</span> <span class="identifier">fi</span><span class="special">.</span><span class="identifier">wait</span><span class="special">();</span> <span class="comment">// wait for it to finish</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">is_ready</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">has_value</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">has_exception</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">get</span><span class="special">()==</span><span class="number">42</span><span class="special">);</span> </pre> <p> A <a class="link" href="futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> is a bit more low level: it just provides explicit functions to store a value or an exception in the associated future. A promise can therefore be used where the value might come from more than one possible source. </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">pi</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">fi</span><span class="special">;</span> <span class="identifier">fi</span><span class="special">=</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">();</span> <span class="identifier">pi</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span><span class="number">42</span><span class="special">);</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">is_ready</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">has_value</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">has_exception</span><span class="special">());</span> <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fi</span><span class="special">.</span><span class="identifier">get</span><span class="special">()==</span><span class="number">42</span><span class="special">);</span> </pre> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="channels/unbuffered_channel.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="futures/future.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/synchronization/conditions.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Condition Variables</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../synchronization.html" title="Synchronization"> <link rel="prev" href="mutex_types.html" title="Mutex Types"> <link rel="next" href="barriers.html" title="Barriers"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="mutex_types.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="barriers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.synchronization.conditions"></a><a class="link" href="conditions.html" title="Condition Variables">Condition Variables</a> </h3></div></div></div> <h5> <a name="fiber.synchronization.conditions.h0"></a> <span><a name="fiber.synchronization.conditions.synopsis"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.synopsis">Synopsis</a> </h5> <pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">cv_status</span><span class="special">;</span> <span class="special">{</span> <span class="identifier">no_timeout</span><span class="special">,</span> <span class="identifier">timeout</span> <span class="special">};</span> <span class="keyword">class</span> <span class="identifier">condition_variable</span><span class="special">;</span> <span class="keyword">class</span> <span class="identifier">condition_variable_any</span><span class="special">;</span> </pre> <p> The class <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> provides a mechanism for a fiber to wait for notification from another fiber. When the fiber awakens from the wait, then it checks to see if the appropriate condition is now true, and continues if so. If the condition is not true, then the fiber calls <code class="computeroutput"><span class="identifier">wait</span></code> again to resume waiting. In the simplest case, this condition is just a boolean variable: </p> <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable</span> <span class="identifier">cond</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">data_ready</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">process_data</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">wait_for_data_to_process</span><span class="special">()</span> <span class="special">{</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span> <span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">data_ready</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">cond</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="comment">// release lk</span> <span class="identifier">process_data</span><span class="special">();</span> <span class="special">}</span> </pre> <p> Notice that the <code class="computeroutput"><span class="identifier">lk</span></code> is passed to <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>: <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> will atomically add the fiber to the set of fibers waiting on the condition variable, and unlock the <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>. When the fiber is awakened, the <code class="computeroutput"><span class="identifier">mutex</span></code> will be locked again before the call to <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> returns. This allows other fibers to acquire the <code class="computeroutput"><span class="identifier">mutex</span></code> in order to update the shared data, and ensures that the data associated with the condition is correctly synchronized. </p> <p> <code class="computeroutput"><span class="identifier">wait_for_data_to_process</span><span class="special">()</span></code> could equivalently be written: </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">wait_for_data_to_process</span><span class="special">()</span> <span class="special">{</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span> <span class="comment">// make condition_variable::wait() perform the loop</span> <span class="identifier">cond</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="special">[](){</span> <span class="keyword">return</span> <span class="identifier">data_ready</span><span class="special">;</span> <span class="special">});</span> <span class="special">}</span> <span class="comment">// release lk</span> <span class="identifier">process_data</span><span class="special">();</span> <span class="special">}</span> </pre> <p> In the meantime, another fiber sets <code class="computeroutput"><span class="identifier">data_ready</span></code> to <code class="computeroutput"><span class="keyword">true</span></code>, and then calls either <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a> or <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> on the <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> <code class="computeroutput"><span class="identifier">cond</span></code> to wake one waiting fiber or all the waiting fibers respectively. </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">retrieve_data</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">prepare_data</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">prepare_data_for_processing</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">retrieve_data</span><span class="special">();</span> <span class="identifier">prepare_data</span><span class="special">();</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx</span><span class="special">);</span> <span class="identifier">data_ready</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">cond</span><span class="special">.</span><span class="identifier">notify_one</span><span class="special">();</span> <span class="special">}</span> </pre> <p> Note that the same <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a> is locked before the shared data is updated, but that the <code class="computeroutput"><span class="identifier">mutex</span></code> does not have to be locked across the call to <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a>. </p> <p> Locking is important because the synchronization objects provided by <span class="bold"><strong>Boost.Fiber</strong></span> can be used to synchronize fibers running on different threads. </p> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides both <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> and <a class="link" href="conditions.html#class_condition_variable_any"><code class="computeroutput">condition_variable_any</code></a>. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable</span></code> can only wait on <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a><code class="computeroutput"><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span></code><a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a><code class="computeroutput"> <span class="special">&gt;</span></code> while <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span></code> can wait on user-defined lock types. </p> <a name="condition_variable_spurious_wakeups"></a><h5> <a name="fiber.synchronization.conditions.h1"></a> <span><a name="fiber.synchronization.conditions.no_spurious_wakeups"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.no_spurious_wakeups">No Spurious Wakeups</a> </h5> <p> Neither <a class="link" href="conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> nor <a class="link" href="conditions.html#class_condition_variable_any"><code class="computeroutput">condition_variable_any</code></a> are subject to spurious wakeup: <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a> can only wake up when <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a> or <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> is called. Even so, it is prudent to use one of the <code class="computeroutput"><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lock</span><span class="special">,</span> <span class="identifier">predicate</span> <span class="special">)</span></code> overloads. </p> <p> Consider a set of consumer fibers processing items from a <a href="http://en.cppreference.com/w/cpp/container/queue" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">queue</span></code></a>. The queue is continually populated by a set of producer fibers. </p> <p> The consumer fibers might reasonably wait on a <code class="computeroutput"><span class="identifier">condition_variable</span></code> as long as the queue remains <a href="http://en.cppreference.com/w/cpp/container/queue/empty" target="_top"><code class="computeroutput"><span class="identifier">empty</span><span class="special">()</span></code></a>. </p> <p> Because producer fibers might <a href="http://en.cppreference.com/w/cpp/container/queue/push" target="_top"><code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code></a> items to the queue in bursts, they call <a class="link" href="conditions.html#condition_variable_notify_all"><code class="computeroutput">condition_variable::notify_all()</code></a> rather than <a class="link" href="conditions.html#condition_variable_notify_one"><code class="computeroutput">condition_variable::notify_one()</code></a>. </p> <p> But a given consumer fiber might well wake up from <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a> and find the queue <code class="computeroutput"><span class="identifier">empty</span><span class="special">()</span></code>, because other consumer fibers might already have processed all pending items. </p> <p> (See also <a class="link" href="../rationale.html#spurious_wakeup">spurious wakeup</a>.) </p> <a name="class_cv_status"></a><h5> <a name="fiber.synchronization.conditions.h2"></a> <span><a name="fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.enumeration__code__phrase_role__identifier__cv_status__phrase___code_">Enumeration <code class="computeroutput"><span class="identifier">cv_status</span></code></a> </h5> <p> A timed wait operation might return because of timeout or not. </p> <pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">cv_status</span> <span class="special">{</span> <span class="identifier">no_timeout</span><span class="special">,</span> <span class="identifier">timeout</span> <span class="special">};</span> </pre> <h5> <a name="fiber.synchronization.conditions.h3"></a> <span><a name="fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions._code__phrase_role__identifier__no_timeout__phrase___code_"><code class="computeroutput"><span class="identifier">no_timeout</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The condition variable was awakened with <code class="computeroutput"><span class="identifier">notify_one</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span></code>. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.conditions.h4"></a> <span><a name="fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions._code__phrase_role__identifier__timeout__phrase___code_"><code class="computeroutput"><span class="identifier">timeout</span></code></a> </h5> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The condition variable was awakened by timeout. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_condition_variable_any_bridgehead"></a> <span><a name="class_condition_variable_any"></a></span> <a class="link" href="conditions.html#class_condition_variable_any">Class <code class="computeroutput">condition_variable_any</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">condition_variable</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> condition_variable_any <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> condition_variable_any<span class="special">();</span> <span class="special">~</span>condition_variable_any<span class="special">();</span> condition_variable_any<span class="special">(</span> condition_variable_any <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> condition_variable_any <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> condition_variable_any <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> template&lt; typename LockType &gt; void <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h5> <a name="fiber.synchronization.conditions.h5"></a> <span><a name="fiber.synchronization.conditions.constructor"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.constructor">Constructor</a> </h5> <pre class="programlisting">condition_variable_any<span class="special">()</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates the object. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.conditions.h6"></a> <span><a name="fiber.synchronization.conditions.destructor"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.destructor">Destructor</a> </h5> <pre class="programlisting"><span class="special">~</span>condition_variable_any<span class="special">()</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> All fibers waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> have been notified by a call to <code class="computeroutput"><span class="identifier">notify_one</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span></code> (though the respective calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> need not have returned). </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys the object. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_any_notify_one_bridgehead"></a> <span><a name="condition_variable_any_notify_one"></a></span> <a class="link" href="conditions.html#condition_variable_any_notify_one">Member function <code class="computeroutput">notify_one</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in a call to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks one of those fibers. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> It is arbitrary which waiting fiber is resumed. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_any_notify_all_bridgehead"></a> <span><a name="condition_variable_any_notify_all"></a></span> <a class="link" href="conditions.html#condition_variable_any_notify_all">Member function <code class="computeroutput">notify_all</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in a call to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks all of those fibers. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This is why a waiting fiber must <span class="emphasis"><em>also</em></span> check for the desired program state using a mechanism external to the <code class="computeroutput">condition_variable_any</code>, and retry the wait until that state is reached. A fiber waiting on a <code class="computeroutput">condition_variable_any</code> might well wake up a number of times before the desired state is reached. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_any_wait_bridgehead"></a> <span><a name="condition_variable_any_wait"></a></span> <a class="link" href="conditions.html#condition_variable_any_wait">Templated member function <code class="computeroutput">wait</code>()</a> </h5> <p> </p> <pre class="programlisting">template&lt; typename LockType &gt; void <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <a href="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex" target="_top"><code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code></a> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">-&gt;</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span> <span class="special">}</span> </pre> <p> </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The Precondition is a bit dense. It merely states that all the fibers concurrently calling <code class="computeroutput"><span class="identifier">wait</span></code> on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> must wait on <code class="computeroutput"><span class="identifier">lk</span></code> objects governing the <span class="emphasis"><em>same</em></span> <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>. Three distinct objects are involved in any <code class="computeroutput">condition_variable_any::wait()</code> call: the <code class="computeroutput">condition_variable_any</code> itself, the <code class="computeroutput"><span class="identifier">mutex</span></code> coordinating access between fibers and a local lock object (e.g. <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a>). In general, you can partition the lifespan of a given <code class="computeroutput">condition_variable_any</code> instance into periods with one or more fibers waiting on it, separated by periods when no fibers are waiting on it. When more than one fiber is waiting on that <code class="computeroutput">condition_variable_any</code>, all must pass lock objects referencing the <span class="emphasis"><em>same</em></span> <code class="computeroutput"><span class="identifier">mutex</span></code> instance. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_any_wait_until_bridgehead"></a> <span><a name="condition_variable_any_wait_until"></a></span> <a class="link" href="conditions.html#condition_variable_any_wait_until">Templated member function <code class="computeroutput">wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait_until</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when the system time would be equal to or later than the specified <code class="computeroutput"><span class="identifier">abs_time</span></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait_until</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="special">)</span> <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> </pre> <p> That is, even if <code class="computeroutput"><span class="identifier">wait_until</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code> at that time. </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs or timeout-related exceptions. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload without <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>, or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because the system time is past <code class="computeroutput"><span class="identifier">abs_time</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="keyword">false</span></code> if the call is returning because the time specified by <code class="computeroutput"><span class="identifier">abs_time</span></code> was reached and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>, <code class="computeroutput"><span class="keyword">true</span></code> otherwise. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_any_wait"><code class="computeroutput">condition_variable_any::wait()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_any_wait_for_bridgehead"></a> <span><a name="condition_variable_any_wait_for"></a></span> <a class="link" href="conditions.html#condition_variable_any_wait_for">Templated member function <code class="computeroutput">wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename LockType, typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> LockType <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait_for</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when a time interval equal to or greater than the specified <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">rel_time</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> </pre> <p> (except of course that <code class="computeroutput"><span class="identifier">rel_time</span></code> is adjusted for each iteration). The point is that, even if <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code> at that time. </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs or timeout-related exceptions. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload without <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>, or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because at least <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="keyword">false</span></code> if the call is returning because at least <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>, <code class="computeroutput"><span class="keyword">true</span></code> otherwise. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_any_wait"><code class="computeroutput">condition_variable_any::wait()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_condition_variable_bridgehead"></a> <span><a name="class_condition_variable"></a></span> <a class="link" href="conditions.html#class_condition_variable">Class <code class="computeroutput">condition_variable</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">condition_variable</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> condition_variable <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> condition_variable<span class="special">();</span> <span class="special">~</span>condition_variable<span class="special">();</span> condition_variable<span class="special">(</span> condition_variable <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> condition_variable <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> condition_variable <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> void <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">Pred</span><span class="special">);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h5> <a name="fiber.synchronization.conditions.h7"></a> <span><a name="fiber.synchronization.conditions.constructor0"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.constructor0">Constructor</a> </h5> <pre class="programlisting">condition_variable<span class="special">()</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates the object. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h5> <a name="fiber.synchronization.conditions.h8"></a> <span><a name="fiber.synchronization.conditions.destructor0"></a></span><a class="link" href="conditions.html#fiber.synchronization.conditions.destructor0">Destructor</a> </h5> <pre class="programlisting"><span class="special">~</span>condition_variable<span class="special">()</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> All fibers waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> have been notified by a call to <code class="computeroutput"><span class="identifier">notify_one</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span></code> (though the respective calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> need not have returned). </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys the object. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_notify_one_bridgehead"></a> <span><a name="condition_variable_notify_one"></a></span> <a class="link" href="conditions.html#condition_variable_notify_one">Member function <code class="computeroutput">notify_one</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_one</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in a call to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks one of those fibers. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> It is arbitrary which waiting fiber is resumed. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_notify_all_bridgehead"></a> <span><a name="condition_variable_notify_all"></a></span> <a class="link" href="conditions.html#condition_variable_notify_all">Member function <code class="computeroutput">notify_all</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify_all</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If any fibers are currently <a class="link" href="../overview.html#blocking"><span class="emphasis"><em>blocked</em></span></a> waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> in a call to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code>, unblocks all of those fibers. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> This is why a waiting fiber must <span class="emphasis"><em>also</em></span> check for the desired program state using a mechanism external to the <code class="computeroutput">condition_variable</code>, and retry the wait until that state is reached. A fiber waiting on a <code class="computeroutput">condition_variable</code> might well wake up a number of times before the desired state is reached. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_wait_bridgehead"></a> <span><a name="condition_variable_wait"></a></span> <a class="link" href="conditions.html#condition_variable_wait">Templated member function <code class="computeroutput">wait</code>()</a> </h5> <p> </p> <pre class="programlisting">void <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <a href="http://en.cppreference.com/w/cpp/thread/unique_lock/mutex" target="_top"><code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code></a> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">-&gt;</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span> <span class="special">}</span> </pre> <p> </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The Precondition is a bit dense. It merely states that all the fibers concurrently calling <code class="computeroutput"><span class="identifier">wait</span></code> on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> must wait on <code class="computeroutput"><span class="identifier">lk</span></code> objects governing the <span class="emphasis"><em>same</em></span> <a class="link" href="mutex_types.html#class_mutex"><code class="computeroutput">mutex</code></a>. Three distinct objects are involved in any <code class="computeroutput">condition_variable::wait()</code> call: the <code class="computeroutput">condition_variable</code> itself, the <code class="computeroutput"><span class="identifier">mutex</span></code> coordinating access between fibers and a local lock object (e.g. <a href="http://en.cppreference.com/w/cpp/thread/unique_lock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span></code></a>). In general, you can partition the lifespan of a given <code class="computeroutput">condition_variable</code> instance into periods with one or more fibers waiting on it, separated by periods when no fibers are waiting on it. When more than one fiber is waiting on that <code class="computeroutput">condition_variable</code>, all must pass lock objects referencing the <span class="emphasis"><em>same</em></span> <code class="computeroutput"><span class="identifier">mutex</span></code> instance. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_wait_until_bridgehead"></a> <span><a name="condition_variable_wait_until"></a></span> <a class="link" href="conditions.html#condition_variable_wait_until">Templated member function <code class="computeroutput">wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_until</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait_until</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when the system time would be equal to or later than the specified <code class="computeroutput"><span class="identifier">abs_time</span></code>. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait_until</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="special">)</span> <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> </pre> <p> That is, even if <code class="computeroutput"><span class="identifier">wait_until</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code> at that time. </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs or timeout-related exceptions. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload without <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>, or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because the system time is past <code class="computeroutput"><span class="identifier">abs_time</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="keyword">false</span></code> if the call is returning because the time specified by <code class="computeroutput"><span class="identifier">abs_time</span></code> was reached and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>, <code class="computeroutput"><span class="keyword">true</span></code> otherwise. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="condition_variable_wait_for_bridgehead"></a> <span><a name="condition_variable_wait_for"></a></span> <a class="link" href="conditions.html#condition_variable_wait_for">Templated member function <code class="computeroutput">wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">cv_status</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> typename <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span> <span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">wait_for</span><span class="special">(</span> std::unique_lock&lt; mutex &gt; <span class="special">&amp;</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber, and either no other fiber is currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>, or the execution of the <code class="computeroutput"><span class="identifier">mutex</span><span class="special">()</span></code> member function on the <code class="computeroutput"><span class="identifier">lk</span></code> objects supplied in the calls to <code class="computeroutput"><span class="identifier">wait</span></code>, <code class="computeroutput"><span class="identifier">wait_for</span></code> or <code class="computeroutput"><span class="identifier">wait_until</span></code> in all the fibers currently waiting on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> would return the same value as <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">mutex</span><span class="special">()</span></code> for this call to <code class="computeroutput"><span class="identifier">wait_for</span></code>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd> <p> Atomically call <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">()</span></code> and blocks the current fiber. The fiber will unblock when notified by a call to <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">notify_all</span><span class="special">()</span></code>, when a time interval equal to or greater than the specified <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed. When the fiber is unblocked (for whatever reason), the lock is reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> before the call to <code class="computeroutput"><span class="identifier">wait</span></code> returns. The lock is also reacquired by invoking <code class="computeroutput"><span class="identifier">lk</span><span class="special">.</span><span class="identifier">lock</span><span class="special">()</span></code> if the function exits with an exception. The <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> member function accepting <code class="computeroutput"><span class="identifier">pred</span></code> is shorthand for: </p> <pre class="programlisting"><span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">pred</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span> <span class="special">==</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">,</span> <span class="identifier">rel_time</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">pred</span><span class="special">();</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> </pre> <p> (except of course that <code class="computeroutput"><span class="identifier">rel_time</span></code> is adjusted for each iteration). The point is that, even if <code class="computeroutput"><span class="identifier">wait_for</span><span class="special">()</span></code> times out, it can still return <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">pred</span><span class="special">()</span></code> returns <code class="computeroutput"><span class="keyword">true</span></code> at that time. </p> </dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">lk</span></code> is locked by the current fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs or timeout-related exceptions. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload without <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">no_timeout</span></code> if awakened by <code class="computeroutput"><span class="identifier">notify_one</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">notify_all</span><span class="special">()</span></code>, or <code class="computeroutput"><span class="identifier">cv_status</span><span class="special">::</span><span class="identifier">timeout</span></code> if awakened because at least <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> The overload accepting <code class="computeroutput"><span class="identifier">pred</span></code> returns <code class="computeroutput"><span class="keyword">false</span></code> if the call is returning because at least <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed and the predicate returns <code class="computeroutput"><span class="keyword">false</span></code>, <code class="computeroutput"><span class="keyword">true</span></code> otherwise. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> See <span class="bold"><strong>Note</strong></span> for <a class="link" href="conditions.html#condition_variable_wait"><code class="computeroutput">condition_variable::wait()</code></a>. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="mutex_types.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="barriers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/synchronization/barriers.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Barriers</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../synchronization.html" title="Synchronization"> <link rel="prev" href="conditions.html" title="Condition Variables"> <link rel="next" href="channels.html" title="Channels"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="conditions.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="channels.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.synchronization.barriers"></a><a class="link" href="barriers.html" title="Barriers">Barriers</a> </h3></div></div></div> <p> A barrier is a concept also known as a <span class="emphasis"><em>rendezvous</em></span>, it is a synchronization point between multiple contexts of execution (fibers). The barrier is configured for a particular number of fibers (<code class="computeroutput"><span class="identifier">n</span></code>), and as fibers reach the barrier they must wait until all <code class="computeroutput"><span class="identifier">n</span></code> fibers have arrived. Once the <code class="computeroutput"><span class="identifier">n</span></code>-th fiber has reached the barrier, all the waiting fibers can proceed, and the barrier is reset. </p> <p> The fact that the barrier automatically resets is significant. Consider a case in which you launch some number of fibers and want to wait only until the first of them has completed. You might be tempted to use a <code class="computeroutput"><span class="identifier">barrier</span><span class="special">(</span><span class="number">2</span><span class="special">)</span></code> as the synchronization mechanism, making each new fiber call its <a class="link" href="barriers.html#barrier_wait"><code class="computeroutput">barrier::wait()</code></a> method, then calling <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> in the launching fiber to wait until the first other fiber completes. </p> <p> That will in fact unblock the launching fiber. The unfortunate part is that it will continue blocking the <span class="emphasis"><em>remaining</em></span> fibers. </p> <p> Consider the following scenario: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> Fiber <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> launches fibers A, B, C and D, then calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. </li> <li class="listitem"> Fiber C finishes first and likewise calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. </li> <li class="listitem"> Fiber <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> is unblocked, as desired. </li> <li class="listitem"> Fiber B calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. Fiber B is <span class="emphasis"><em>blocked!</em></span> </li> <li class="listitem"> Fiber A calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. Fibers A and B are unblocked. </li> <li class="listitem"> Fiber D calls <code class="computeroutput"><span class="identifier">barrier</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. Fiber D is blocked indefinitely. </li> </ol></div> <p> (See also <a class="link" href="../when_any/when_any/when_any__simple_completion.html#wait_first_simple_section">when_any, simple completion</a>.) </p> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> It is unwise to tie the lifespan of a barrier to any one of its participating fibers. Although conceptually all waiting fibers awaken <span class="quote">&#8220;<span class="quote">simultaneously,</span>&#8221;</span> because of the nature of fibers, in practice they will awaken one by one in indeterminate order.<sup>[<a name="fiber.synchronization.barriers.f0" href="#ftn.fiber.synchronization.barriers.f0" class="footnote">1</a>]</sup> The rest of the waiting fibers will still be blocked in <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code>, which must, before returning, access data members in the barrier object. </p></td></tr> </table></div> <p> </p> <h5> <a name="class_barrier_bridgehead"></a> <span><a name="class_barrier"></a></span> <a class="link" href="barriers.html#class_barrier">Class <code class="computeroutput">barrier</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">barrier</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">barrier</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">explicit</span> <span class="identifier">barrier</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">);</span> <span class="identifier">barrier</span><span class="special">(</span> <span class="identifier">barrier</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">barrier</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">barrier</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">wait</span><span class="special">();</span> <span class="special">};</span> <span class="special">}}</span> </pre> <p> Instances of <a class="link" href="barriers.html#class_barrier"><code class="computeroutput">barrier</code></a> are not copyable or movable. </p> <h5> <a name="fiber.synchronization.barriers.h0"></a> <span><a name="fiber.synchronization.barriers.constructor"></a></span><a class="link" href="barriers.html#fiber.synchronization.barriers.constructor">Constructor</a> </h5> <pre class="programlisting"><span class="keyword">explicit</span> <span class="identifier">barrier</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">initial</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Construct a barrier for <code class="computeroutput"><span class="identifier">initial</span></code> fibers. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>invalid_argument</strong></span>: if <code class="computeroutput"><span class="identifier">initial</span></code> is zero. </p></dd> </dl> </div> <p> </p> <h5> <a name="barrier_wait_bridgehead"></a> <span><a name="barrier_wait"></a></span> <a class="link" href="barriers.html#barrier_wait">Member function <code class="computeroutput">wait</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">wait</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Block until <code class="computeroutput"><span class="identifier">initial</span></code> fibers have called <code class="computeroutput"><span class="identifier">wait</span></code> on <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. When the <code class="computeroutput"><span class="identifier">initial</span></code>-th fiber calls <code class="computeroutput"><span class="identifier">wait</span></code>, all waiting fibers are unblocked, and the barrier is reset. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> for exactly one fiber from each batch of waiting fibers, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> </p></dd> </dl> </div> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.synchronization.barriers.f0" href="#fiber.synchronization.barriers.f0" class="para">1</a>] </sup> The current implementation wakes fibers in FIFO order: the first to call <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> wakes first, and so forth. But it is perilous to rely on the order in which the various fibers will reach the <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> call. </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="conditions.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../synchronization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="channels.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/synchronization
repos/fiber/doc/html/fiber/synchronization/futures/future.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Future</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../futures.html" title="Futures"> <link rel="prev" href="../futures.html" title="Futures"> <link rel="next" href="promise.html" title="Template promise&lt;&gt;"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../futures.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="promise.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.synchronization.futures.future"></a><a class="link" href="future.html" title="Future">Future</a> </h4></div></div></div> <p> A future provides a mechanism to access the result of an asynchronous operation. </p> <a name="shared_state"></a><h6> <a name="fiber.synchronization.futures.future.h0"></a> <span><a name="fiber.synchronization.futures.future.shared_state"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.shared_state">shared state</a> </h6> <p> Behind a <a class="link" href="promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> and its <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> lies an unspecified object called their <span class="emphasis"><em>shared state</em></span>. The shared state is what will actually hold the async result (or the exception). </p> <p> The shared state is instantiated along with the <a class="link" href="promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a>. </p> <p> Aside from its originating <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code>, a <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> holds a unique reference to a particular shared state. However, multiple <a class="link" href="future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> instances can reference the same underlying shared state. </p> <p> As <a class="link" href="packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> and <a class="link" href="future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a> are implemented using <a class="link" href="promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a>, discussions of shared state apply to them as well. </p> <a name="class_future_status"></a><h6> <a name="fiber.synchronization.futures.future.h1"></a> <span><a name="fiber.synchronization.futures.future.enumeration__code__phrase_role__identifier__future_status__phrase___code_"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.enumeration__code__phrase_role__identifier__future_status__phrase___code_">Enumeration <code class="computeroutput"><span class="identifier">future_status</span></code></a> </h6> <p> Timed wait-operations (<a class="link" href="future.html#future_wait_for"><code class="computeroutput">future::wait_for()</code></a> and <a class="link" href="future.html#future_wait_until"><code class="computeroutput">future::wait_until()</code></a>) return the state of the future. </p> <pre class="programlisting"><span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">future_status</span> <span class="special">{</span> <span class="identifier">ready</span><span class="special">,</span> <span class="identifier">timeout</span><span class="special">,</span> <span class="identifier">deferred</span> <span class="comment">// not supported yet</span> <span class="special">};</span> </pre> <h6> <a name="fiber.synchronization.futures.future.h2"></a> <span><a name="fiber.synchronization.futures.future._code__phrase_role__identifier__ready__phrase___code_"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future._code__phrase_role__identifier__ready__phrase___code_"><code class="computeroutput"><span class="identifier">ready</span></code></a> </h6> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The <a class="link" href="future.html#shared_state">shared state</a> is ready. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h3"></a> <span><a name="fiber.synchronization.futures.future._code__phrase_role__identifier__timeout__phrase___code_"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future._code__phrase_role__identifier__timeout__phrase___code_"><code class="computeroutput"><span class="identifier">timeout</span></code></a> </h6> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The <a class="link" href="future.html#shared_state">shared state</a> did not become ready before timeout has passed. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Deferred futures are not supported. </p></td></tr> </table></div> <p> </p> <h5> <a name="class_future_bridgehead"></a> <span><a name="class_future"></a></span> <a class="link" href="future.html#class_future">Template <code class="computeroutput">future&lt;&gt;</code></a> </h5> <p> </p> <p> A <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> contains a <a class="link" href="future.html#shared_state">shared state</a> which is not shared with any other future. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">future</span><span class="special">/</span><span class="identifier">future</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">future</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">future</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">~</span><span class="identifier">future</span><span class="special">();</span> <span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">share</span><span class="special">();</span> <span class="identifier">R</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of generic future template</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of future&lt; R &amp; &gt; template specialization</span> <span class="keyword">void</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of future&lt; void &gt; template specialization</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">get_exception_ptr</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h6> <a name="fiber.synchronization.futures.future.h4"></a> <span><a name="fiber.synchronization.futures.future.default_constructor"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.default_constructor">Default constructor</a> </h6> <pre class="programlisting"><span class="identifier">future</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a future with no <a class="link" href="future.html#shared_state">shared state</a>. After construction <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h5"></a> <span><a name="fiber.synchronization.futures.future.move_constructor"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.move_constructor">Move constructor</a> </h6> <pre class="programlisting"><span class="identifier">future</span><span class="special">(</span> <span class="identifier">future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs a future with the <a class="link" href="future.html#shared_state">shared state</a> of other. After construction <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">valid</span><span class="special">()</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h6"></a> <span><a name="fiber.synchronization.futures.future.destructor"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.destructor">Destructor</a> </h6> <pre class="programlisting"><span class="special">~</span><span class="identifier">future</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys the future; ownership is abandoned. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput">~future()</code> does <span class="emphasis"><em>not</em></span> block the calling fiber. </p></dd> </dl> </div> <p> Consider a sequence such as: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> instantiate <a class="link" href="promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> </li> <li class="listitem"> obtain its <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> via <a class="link" href="promise.html#promise_get_future"><code class="computeroutput">promise::get_future()</code></a> </li> <li class="listitem"> launch <a class="link" href="../../fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a>, capturing <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code> </li> <li class="listitem"> destroy <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> </li> <li class="listitem"> call <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> </li> </ol></div> <p> The final <code class="computeroutput"><span class="identifier">set_value</span><span class="special">()</span></code> call succeeds, but the value is silently discarded: no additional <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> can be obtained from that <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code>. </p> <p> </p> <h5> <a name="future_operator_assign_bridgehead"></a> <span><a name="future_operator_assign"></a></span> <a class="link" href="future.html#future_operator_assign">Member function <code class="computeroutput">operator=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Moves the <a class="link" href="future.html#shared_state">shared state</a> of other to <code class="computeroutput"><span class="keyword">this</span></code>. After the assignment, <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">valid</span><span class="special">()</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_valid_bridgehead"></a> <span><a name="future_valid"></a></span> <a class="link" href="future.html#future_valid">Member function <code class="computeroutput">valid</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Returns <code class="computeroutput"><span class="keyword">true</span></code> if future contains a <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_share_bridgehead"></a> <span><a name="future_share"></a></span> <a class="link" href="future.html#future_share">Member function <code class="computeroutput">share</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">shared_future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">share</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Move the state to a <a class="link" href="future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> a <a class="link" href="future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> containing the <a class="link" href="future.html#shared_state">shared state</a> formerly belonging to <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_get_bridgehead"></a> <span><a name="future_get"></a></span> <a class="link" href="future.html#future_get">Member function <code class="computeroutput">get</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">R</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of generic future template</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of future&lt; R &amp; &gt; template specialization</span> <span class="keyword">void</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of future&lt; void &gt; template specialization</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. If <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> is called, returns the value. If <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, throws the indicated exception. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>, <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span></code>. Any exception passed to <code class="computeroutput"><span class="identifier">promise</span><span class="special">::</span><span class="identifier">set_exception</span><span class="special">()</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_get_exception_ptr_bridgehead"></a> <span><a name="future_get_exception_ptr"></a></span> <a class="link" href="future.html#future_get_exception_ptr">Member function <code class="computeroutput">get_exception_ptr</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">get_exception_ptr</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. If <code class="computeroutput"><span class="identifier">set_value</span><span class="special">()</span></code> is called, returns a default-constructed <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>. If <code class="computeroutput"><span class="identifier">set_exception</span><span class="special">()</span></code> is called, returns the passed <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">get_exception_ptr</span><span class="special">()</span></code> does <span class="emphasis"><em>not</em></span> invalidate the <code class="computeroutput">future</code>. After calling <code class="computeroutput"><span class="identifier">get_exception_ptr</span><span class="special">()</span></code>, you may still call <a class="link" href="future.html#future_get"><code class="computeroutput">future::get()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_wait_bridgehead"></a> <span><a name="future_wait"></a></span> <a class="link" href="future.html#future_wait">Member function <code class="computeroutput">wait</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">wait</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_wait_for_bridgehead"></a> <span><a name="future_wait_for"></a></span> <a class="link" href="future.html#future_wait_for">Templated member function <code class="computeroutput">wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, or <code class="computeroutput"><span class="identifier">timeout_duration</span></code> has passed. </p></dd> <dt><span class="term">Result:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">future_status</span></code> is returned indicating the reason for returning. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code> or timeout-related exceptions. </p></dd> </dl> </div> <p> </p> <h5> <a name="future_wait_until_bridgehead"></a> <span><a name="future_wait_until"></a></span> <a class="link" href="future.html#future_wait_until">Templated member function <code class="computeroutput">wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, or <code class="computeroutput"><span class="identifier">timeout_time</span></code> has passed. </p></dd> <dt><span class="term">Result:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">future_status</span></code> is returned indicating the reason for returning. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code> or timeout-related exceptions. </p></dd> </dl> </div> <p> </p> <h5> <a name="class_shared_future_bridgehead"></a> <span><a name="class_shared_future"></a></span> <a class="link" href="future.html#class_shared_future">Template <code class="computeroutput">shared_future&lt;&gt;</code></a> </h5> <p> </p> <p> A <a class="link" href="future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> contains a <a class="link" href="future.html#shared_state">shared state</a> which might be shared with other <a class="link" href="future.html#class_shared_future"><code class="computeroutput">shared_future&lt;&gt;</code></a> instances. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">future</span><span class="special">/</span><span class="identifier">future</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">shared_future</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">shared_future</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">~</span><span class="identifier">shared_future</span><span class="special">();</span> <span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">shared_future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">);</span> <span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">shared_future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">shared_future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">shared_future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">R</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of generic shared_future template</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of shared_future&lt; R &amp; &gt; template specialization</span> <span class="keyword">void</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of shared_future&lt; void &gt; template specialization</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">get_exception_ptr</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h6> <a name="fiber.synchronization.futures.future.h7"></a> <span><a name="fiber.synchronization.futures.future.default_constructor0"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.default_constructor0">Default constructor</a> </h6> <pre class="programlisting"><span class="identifier">shared_future</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a shared_future with no <a class="link" href="future.html#shared_state">shared state</a>. After construction <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h8"></a> <span><a name="fiber.synchronization.futures.future.move_constructor0"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.move_constructor0">Move constructor</a> </h6> <pre class="programlisting"><span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">shared_future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs a shared_future with the <a class="link" href="future.html#shared_state">shared state</a> of other. After construction <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">valid</span><span class="special">()</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h9"></a> <span><a name="fiber.synchronization.futures.future.copy_constructor"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.copy_constructor">Copy constructor</a> </h6> <pre class="programlisting"><span class="identifier">shared_future</span><span class="special">(</span> <span class="identifier">shared_future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs a shared_future with the <a class="link" href="future.html#shared_state">shared state</a> of other. After construction <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">valid</span><span class="special">()</span></code> is unchanged. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.future.h10"></a> <span><a name="fiber.synchronization.futures.future.destructor0"></a></span><a class="link" href="future.html#fiber.synchronization.futures.future.destructor0">Destructor</a> </h6> <pre class="programlisting"><span class="special">~</span><span class="identifier">shared_future</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys the shared_future; ownership is abandoned if not shared. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput">~shared_future()</code> does <span class="emphasis"><em>not</em></span> block the calling fiber. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_operator_assign_bridgehead"></a> <span><a name="shared_future_operator_assign"></a></span> <a class="link" href="future.html#shared_future_operator_assign">Member function <code class="computeroutput">operator=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">shared_future</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">shared_future</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">shared_future</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Moves or copies the <a class="link" href="future.html#shared_state">shared state</a> of other to <code class="computeroutput"><span class="keyword">this</span></code>. After the assignment, the state of <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">valid</span><span class="special">()</span></code> depends on which overload was invoked: unchanged for the overload accepting <code class="computeroutput"><span class="identifier">shared_future</span> <span class="keyword">const</span><span class="special">&amp;</span></code>, otherwise <code class="computeroutput"><span class="keyword">false</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_valid_bridgehead"></a> <span><a name="shared_future_valid"></a></span> <a class="link" href="future.html#shared_future_valid">Member function <code class="computeroutput">valid</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Returns <code class="computeroutput"><span class="keyword">true</span></code> if shared_future contains a <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_get_bridgehead"></a> <span><a name="shared_future_get"></a></span> <a class="link" href="future.html#shared_future_get">Member function <code class="computeroutput">get</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">R</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of generic shared_future template</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of shared_future&lt; R &amp; &gt; template specialization</span> <span class="keyword">void</span> <span class="identifier">get</span><span class="special">();</span> <span class="comment">// member only of shared_future&lt; void &gt; template specialization</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. If <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> is called, returns the value. If <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, throws the indicated exception. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">false</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>, <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span></code>. Any exception passed to <code class="computeroutput"><span class="identifier">promise</span><span class="special">::</span><span class="identifier">set_exception</span><span class="special">()</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_get_exception_ptr_bridgehead"></a> <span><a name="shared_future_get_exception_ptr"></a></span> <a class="link" href="future.html#shared_future_get_exception_ptr">Member function <code class="computeroutput">get_exception_ptr</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">get_exception_ptr</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Precondition:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span> <span class="special">==</span> <span class="identifier">valid</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. If <code class="computeroutput"><span class="identifier">set_value</span><span class="special">()</span></code> is called, returns a default-constructed <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>. If <code class="computeroutput"><span class="identifier">set_exception</span><span class="special">()</span></code> is called, returns the passed <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">get_exception_ptr</span><span class="special">()</span></code> does <span class="emphasis"><em>not</em></span> invalidate the <code class="computeroutput">shared_future</code>. After calling <code class="computeroutput"><span class="identifier">get_exception_ptr</span><span class="special">()</span></code>, you may still call <a class="link" href="future.html#shared_future_get"><code class="computeroutput">shared_future::get()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_wait_bridgehead"></a> <span><a name="shared_future_wait"></a></span> <a class="link" href="future.html#shared_future_wait">Member function <code class="computeroutput">wait</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">wait</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_wait_for_bridgehead"></a> <span><a name="shared_future_wait_for"></a></span> <a class="link" href="future.html#shared_future_wait_for">Templated member function <code class="computeroutput">wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, or <code class="computeroutput"><span class="identifier">timeout_duration</span></code> has passed. </p></dd> <dt><span class="term">Result:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">future_status</span></code> is returned indicating the reason for returning. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code> or timeout-related exceptions. </p></dd> </dl> </div> <p> </p> <h5> <a name="shared_future_wait_until_bridgehead"></a> <span><a name="shared_future_wait_until"></a></span> <a class="link" href="future.html#shared_future_wait_until">Templated member function <code class="computeroutput">wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">future_status</span> <span class="identifier">wait_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Waits until <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a> or <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a> is called, or <code class="computeroutput"><span class="identifier">timeout_time</span></code> has passed. </p></dd> <dt><span class="term">Result:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">future_status</span></code> is returned indicating the reason for returning. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code> or timeout-related exceptions. </p></dd> </dl> </div> <p> </p> <h5> <a name="fibers_async_bridgehead"></a> <span><a name="fibers_async"></a></span> <a class="link" href="future.html#fibers_async">Non-member function <code class="computeroutput">fibers::async()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">future</span><span class="special">/</span><span class="identifier">async</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of_t</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Function</span> <span class="special">&gt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">async</span><span class="special">(</span> <span class="identifier">Function</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of_t</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Function</span> <span class="special">&gt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">async</span><span class="special">(</span> <a class="link" href="../../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a> <span class="identifier">policy</span><span class="special">,</span> <span class="identifier">Function</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of_t</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Function</span> <span class="special">&gt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">async</span><span class="special">(</span> <a class="link" href="../../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a> <span class="identifier">policy</span><span class="special">,</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <a class="link" href="../../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a> <span class="identifier">salloc</span><span class="special">,</span> <span class="identifier">Function</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Allocator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of_t</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Function</span> <span class="special">&gt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">async</span><span class="special">(</span> <a class="link" href="../../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a> <span class="identifier">policy</span><span class="special">,</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <a class="link" href="../../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a> <span class="identifier">salloc</span><span class="special">,</span> <span class="identifier">Allocator</span> <span class="identifier">alloc</span><span class="special">,</span> <span class="identifier">Function</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Executes <code class="computeroutput"><span class="identifier">fn</span></code> in a <a class="link" href="../../fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> and returns an associated <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a>. </p></dd> <dt><span class="term">Result:</span></dt> <dd> <p> </p> <pre class="programlisting"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">result_of_t</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Function</span> <span class="special">&gt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">decay_t</span><span class="special">&lt;</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">&gt;</span></pre> <p> representing the <a class="link" href="future.html#shared_state">shared state</a> associated with the asynchronous execution of <code class="computeroutput"><span class="identifier">fn</span></code>. </p> </dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> or <code class="computeroutput"><span class="identifier">future_error</span></code> if an error occurs. </p></dd> <dt><span class="term">Notes:</span></dt> <dd><p> The overloads accepting <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a> use the passed <a class="link" href="../../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a> when constructing the launched <code class="computeroutput"><span class="identifier">fiber</span></code>. The overloads accepting <a class="link" href="../../fiber_mgmt.html#class_launch"><code class="computeroutput">launch</code></a> use the passed <code class="computeroutput"><span class="identifier">launch</span></code> when constructing the launched <code class="computeroutput"><span class="identifier">fiber</span></code>. The default <code class="computeroutput"><span class="identifier">launch</span></code> is <code class="computeroutput"><span class="identifier">post</span></code>, as for the <code class="computeroutput"><span class="identifier">fiber</span></code> constructor. </p></dd> </dl> </div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> Deferred futures are not supported. </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../futures.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="promise.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/synchronization
repos/fiber/doc/html/fiber/synchronization/futures/packaged_task.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Template packaged_task&lt;&gt;</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../futures.html" title="Futures"> <link rel="prev" href="promise.html" title="Template promise&lt;&gt;"> <link rel="next" href="../../fls.html" title="Fiber local storage"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="promise.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../fls.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.synchronization.futures.packaged_task"></a><a name="class_packaged_task"></a><a class="link" href="packaged_task.html" title="Template packaged_task&lt;&gt;">Template <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code></a> </h4></div></div></div> <p> A <a class="link" href="packaged_task.html#class_packaged_task"><code class="computeroutput">packaged_task&lt;&gt;</code></a> wraps a callable target that returns a value so that the return value can be computed asynchronously. </p> <p> Conventional usage of <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code> is like this: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> Instantiate <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code> with template arguments matching the signature of the callable. Pass the callable to the <a class="link" href="packaged_task.html#packaged_task_packaged_task">constructor</a>. </li> <li class="listitem"> Call <a class="link" href="packaged_task.html#packaged_task_get_future"><code class="computeroutput">packaged_task::get_future()</code></a> and capture the returned <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> instance. </li> <li class="listitem"> Launch a <a class="link" href="../../fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> to run the new <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">&lt;&gt;</span></code>, passing any arguments required by the original callable. </li> <li class="listitem"> Call <a class="link" href="../../fiber_mgmt/fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a> on the newly-launched <code class="computeroutput"><span class="identifier">fiber</span></code>. </li> <li class="listitem"> At some later point, retrieve the result from the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code>. </li> </ol></div> <p> This is, in fact, pretty much what <a class="link" href="future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a> encapsulates. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">future</span><span class="special">/</span><span class="identifier">packaged_task</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">R</span><span class="special">(</span> <span class="identifier">Args</span> <span class="special">...</span> <span class="special">)</span> <span class="special">&gt;</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">packaged_task</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">explicit</span> <span class="identifier">packaged_task</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <a href="http://en.cppreference.com/w/cpp/concept/Allocator" target="_top"><code class="computeroutput"><span class="identifier">Allocator</span></code></a> <span class="special">&gt;</span> <span class="identifier">packaged_task</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;);</span> <span class="identifier">packaged_task</span><span class="special">(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">packaged_task</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">packaged_task</span><span class="special">(</span> <span class="identifier">packaged_task</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">packaged_task</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">packaged_task</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="special">~</span><span class="identifier">packaged_task</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">get_future</span><span class="special">();</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">Args</span> <span class="special">...);</span> <span class="keyword">void</span> <span class="identifier">reset</span><span class="special">();</span> <span class="special">};</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="special">&amp;,</span> <span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}</span> </pre> <h6> <a name="fiber.synchronization.futures.packaged_task.h0"></a> <span><a name="fiber.synchronization.futures.packaged_task.default_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_"></a></span><a class="link" href="packaged_task.html#fiber.synchronization.futures.packaged_task.default_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_">Default constructor <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">()</span></code></a> </h6> <pre class="programlisting"><span class="identifier">packaged_task</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs an object of class <code class="computeroutput"><span class="identifier">packaged_task</span></code> with no <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <a name="packaged_task_packaged_task"></a><h6> <a name="fiber.synchronization.futures.packaged_task.h1"></a> <span><a name="fiber.synchronization.futures.packaged_task.templated_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_"></a></span><a class="link" href="packaged_task.html#fiber.synchronization.futures.packaged_task.templated_constructor__code__phrase_role__identifier__packaged_task__phrase__phrase_role__special______phrase___code_">Templated constructor <code class="computeroutput"><span class="identifier">packaged_task</span><span class="special">()</span></code></a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">explicit</span> <span class="identifier">packaged_task</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <a href="http://en.cppreference.com/w/cpp/concept/Allocator" target="_top"><code class="computeroutput"><span class="identifier">Allocator</span></code></a> <span class="special">&gt;</span> <span class="identifier">packaged_task</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs an object of class <code class="computeroutput"><span class="identifier">packaged_task</span></code> with a <a class="link" href="future.html#shared_state">shared state</a> and copies or moves the callable target <code class="computeroutput"><span class="identifier">fn</span></code> to internal storage. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions caused by memory allocation. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The signature of <code class="computeroutput"><span class="identifier">Fn</span></code> should have a return type convertible to <code class="computeroutput"><span class="identifier">R</span></code>. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a> </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.packaged_task.h2"></a> <span><a name="fiber.synchronization.futures.packaged_task.move_constructor"></a></span><a class="link" href="packaged_task.html#fiber.synchronization.futures.packaged_task.move_constructor">Move constructor</a> </h6> <pre class="programlisting"><span class="identifier">packaged_task</span><span class="special">(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a packaged_task by moving the <a class="link" href="future.html#shared_state">shared state</a> from <code class="computeroutput"><span class="identifier">other</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span></code> contains no valid shared state. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.packaged_task.h3"></a> <span><a name="fiber.synchronization.futures.packaged_task.destructor"></a></span><a class="link" href="packaged_task.html#fiber.synchronization.futures.packaged_task.destructor">Destructor</a> </h6> <pre class="programlisting"><span class="special">~</span><span class="identifier">packaged_task</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> and abandons the <a class="link" href="future.html#shared_state">shared state</a> if shared state is ready; otherwise stores <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span></code> as if by <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a>: the shared state is set ready. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_operator_assign_bridgehead"></a> <span><a name="packaged_task_operator_assign"></a></span> <a class="link" href="packaged_task.html#packaged_task_operator_assign">Member function <code class="computeroutput">operator=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">packaged_task</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Transfers the ownership of <a class="link" href="future.html#shared_state">shared state</a> to <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span></code> contains no valid shared state. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_swap_bridgehead"></a> <span><a name="packaged_task_swap"></a></span> <a class="link" href="packaged_task.html#packaged_task_swap">Member function <code class="computeroutput">swap</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">packaged_task</span> <span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Swaps the <a class="link" href="future.html#shared_state">shared state</a> between other and <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_valid_bridgehead"></a> <span><a name="packaged_task_valid"></a></span> <a class="link" href="packaged_task.html#packaged_task_valid">Member function <code class="computeroutput">valid</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Returns <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> contains a <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_get_future_bridgehead"></a> <span><a name="packaged_task_get_future"></a></span> <a class="link" href="packaged_task.html#packaged_task_get_future">Member function <code class="computeroutput">get_future</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">get_future</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> A <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> with the same <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">future_already_retrieved</span></code> or <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_operator_apply_bridgehead"></a> <span><a name="packaged_task_operator_apply"></a></span> <a class="link" href="packaged_task.html#packaged_task_operator_apply">Member function <code class="computeroutput">operator()</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Invokes the stored callable target. Any exception thrown by the callable target <code class="computeroutput"><span class="identifier">fn</span></code> is stored in the <a class="link" href="future.html#shared_state">shared state</a> as if by <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a>. Otherwise, the value returned by <code class="computeroutput"><span class="identifier">fn</span></code> is stored in the shared state as if by <a class="link" href="promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="packaged_task_reset_bridgehead"></a> <span><a name="packaged_task_reset"></a></span> <a class="link" href="packaged_task.html#packaged_task_reset">Member function <code class="computeroutput">reset</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">reset</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Resets the <a class="link" href="future.html#shared_state">shared state</a> and abandons the result of previous executions. A new shared state is constructed. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="swap_for_packaged_task_bridgehead"></a> <span><a name="swap_for_packaged_task"></a></span> <a class="link" href="packaged_task.html#swap_for_packaged_task">Non-member function <code class="computeroutput">swap()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">packaged_task</span><span class="special">&lt;</span> <span class="identifier">Signature</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Same as <code class="computeroutput"><span class="identifier">l</span><span class="special">.</span><span class="identifier">swap</span><span class="special">(</span> <span class="identifier">r</span><span class="special">)</span></code>. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="promise.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../fls.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/synchronization
repos/fiber/doc/html/fiber/synchronization/futures/promise.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Template promise&lt;&gt;</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../futures.html" title="Futures"> <link rel="prev" href="future.html" title="Future"> <link rel="next" href="packaged_task.html" title="Template packaged_task&lt;&gt;"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="future.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="packaged_task.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.synchronization.futures.promise"></a><a name="class_promise"></a><a class="link" href="promise.html" title="Template promise&lt;&gt;">Template <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code></a> </h4></div></div></div> <p> A <a class="link" href="promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> provides a mechanism to store a value (or exception) that can later be retrieved from the corresponding <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> object. <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code> and <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> communicate via their underlying <a class="link" href="future.html#shared_state">shared state</a>. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">future</span><span class="special">/</span><span class="identifier">promise</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">promise</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">promise</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a href="http://en.cppreference.com/w/cpp/concept/Allocator" target="_top"><code class="computeroutput"><span class="identifier">Allocator</span></code></a> <span class="special">&gt;</span> <span class="identifier">promise</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">);</span> <span class="identifier">promise</span><span class="special">(</span> <span class="identifier">promise</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">promise</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">promise</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">promise</span><span class="special">(</span> <span class="identifier">promise</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">promise</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">promise</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="special">~</span><span class="identifier">promise</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">promise</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">get_future</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="comment">// member only of generic promise template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="special">&amp;&amp;);</span> <span class="comment">// member only of generic promise template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="special">&amp;);</span> <span class="comment">// member only of promise&lt; R &amp; &gt; template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">();</span> <span class="comment">// member only of promise&lt; void &gt; template</span> <span class="keyword">void</span> <span class="identifier">set_exception</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span> <span class="identifier">p</span><span class="special">);</span> <span class="special">};</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;,</span> <span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}</span> </pre> <h6> <a name="fiber.synchronization.futures.promise.h0"></a> <span><a name="fiber.synchronization.futures.promise.default_constructor"></a></span><a class="link" href="promise.html#fiber.synchronization.futures.promise.default_constructor">Default constructor</a> </h6> <pre class="programlisting"><span class="identifier">promise</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a promise with an empty <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions caused by memory allocation. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.promise.h1"></a> <span><a name="fiber.synchronization.futures.promise.constructor"></a></span><a class="link" href="promise.html#fiber.synchronization.futures.promise.constructor">Constructor</a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a href="http://en.cppreference.com/w/cpp/concept/Allocator" target="_top"><code class="computeroutput"><span class="identifier">Allocator</span></code></a> <span class="special">&gt;</span> <span class="identifier">promise</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">Allocator</span> <span class="identifier">alloc</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a promise with an empty <a class="link" href="future.html#shared_state">shared state</a> by using <code class="computeroutput"><span class="identifier">alloc</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions caused by memory allocation. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a> </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.promise.h2"></a> <span><a name="fiber.synchronization.futures.promise.move_constructor"></a></span><a class="link" href="promise.html#fiber.synchronization.futures.promise.move_constructor">Move constructor</a> </h6> <pre class="programlisting"><span class="identifier">promise</span><span class="special">(</span> <span class="identifier">promise</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Creates a promise by moving the <a class="link" href="future.html#shared_state">shared state</a> from <code class="computeroutput"><span class="identifier">other</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span></code> contains no valid shared state. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.futures.promise.h3"></a> <span><a name="fiber.synchronization.futures.promise.destructor"></a></span><a class="link" href="promise.html#fiber.synchronization.futures.promise.destructor">Destructor</a> </h6> <pre class="programlisting"><span class="special">~</span><span class="identifier">promise</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Destroys <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> and abandons the <a class="link" href="future.html#shared_state">shared state</a> if shared state is ready; otherwise stores <code class="computeroutput"><span class="identifier">future_error</span></code> with error condition <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">broken_promise</span></code> as if by <a class="link" href="promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a>: the shared state is set ready. </p></dd> </dl> </div> <p> </p> <h5> <a name="promise_operator_assign_bridgehead"></a> <span><a name="promise_operator_assign"></a></span> <a class="link" href="promise.html#promise_operator_assign">Member function <code class="computeroutput">operator=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">promise</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">promise</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Transfers the ownership of <a class="link" href="future.html#shared_state">shared state</a> to <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Postcondition:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span></code> contains no valid shared state. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="promise_swap_bridgehead"></a> <span><a name="promise_swap"></a></span> <a class="link" href="promise.html#promise_swap">Member function <code class="computeroutput">swap</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">promise</span> <span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Swaps the <a class="link" href="future.html#shared_state">shared state</a> between other and <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="promise_get_future_bridgehead"></a> <span><a name="promise_get_future"></a></span> <a class="link" href="promise.html#promise_get_future">Member function <code class="computeroutput">get_future</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="identifier">get_future</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> A <a class="link" href="future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> with the same <a class="link" href="future.html#shared_state">shared state</a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">future_already_retrieved</span></code> or <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="promise_set_value_bridgehead"></a> <span><a name="promise_set_value"></a></span> <a class="link" href="promise.html#promise_set_value">Member function <code class="computeroutput">set_value</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">);</span> <span class="comment">// member only of generic promise template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="special">&amp;&amp;</span> <span class="identifier">value</span><span class="special">);</span> <span class="comment">// member only of generic promise template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">value</span><span class="special">);</span> <span class="comment">// member only of promise&lt; R &amp; &gt; template</span> <span class="keyword">void</span> <span class="identifier">set_value</span><span class="special">();</span> <span class="comment">// member only of promise&lt; void &gt; template</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Store the result in the <a class="link" href="future.html#shared_state">shared state</a> and marks the state as ready. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">future_already_satisfied</span></code> or <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="promise_set_exception_bridgehead"></a> <span><a name="promise_set_exception"></a></span> <a class="link" href="promise.html#promise_set_exception">Member function <code class="computeroutput">set_exception</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">set_exception</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Store an exception pointer in the <a class="link" href="future.html#shared_state">shared state</a> and marks the state as ready. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">future_error</span></code> with <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">future_already_satisfied</span></code> or <code class="computeroutput"><span class="identifier">future_errc</span><span class="special">::</span><span class="identifier">no_state</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="swap_for_promise_bridgehead"></a> <span><a name="swap_for_promise"></a></span> <a class="link" href="promise.html#swap_for_promise">Non-member function <code class="computeroutput">swap()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Same as <code class="computeroutput"><span class="identifier">l</span><span class="special">.</span><span class="identifier">swap</span><span class="special">(</span> <span class="identifier">r</span><span class="special">)</span></code>. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="future.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../futures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="packaged_task.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/synchronization
repos/fiber/doc/html/fiber/synchronization/channels/buffered_channel.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Buffered Channel</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../channels.html" title="Channels"> <link rel="prev" href="../channels.html" title="Channels"> <link rel="next" href="unbuffered_channel.html" title="Unbuffered Channel"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../channels.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../channels.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unbuffered_channel.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.synchronization.channels.buffered_channel"></a><a class="link" href="buffered_channel.html" title="Buffered Channel">Buffered Channel</a> </h4></div></div></div> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides a bounded, buffered channel (MPMC queue) suitable to synchonize fibers (running on same or different threads) via asynchronouss message passing. </p> <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">send</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">5</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="identifier">i</span><span class="special">);</span> <span class="special">}</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">recv</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"received "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="identifier">channel_t</span> <span class="identifier">chan</span><span class="special">{</span> <span class="number">2</span> <span class="special">};</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f1</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="identifier">send</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f2</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="identifier">recv</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="identifier">f1</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> <span class="identifier">f2</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> Class <code class="computeroutput"><span class="identifier">buffered_channel</span></code> supports range-for syntax: </p> <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">1</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">1</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">2</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">3</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">5</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">8</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">12</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">bar</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">value</span> <span class="special">:</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <h5> <a name="class_buffered_channel_bridgehead"></a> <span><a name="class_buffered_channel"></a></span> <a class="link" href="buffered_channel.html#class_buffered_channel">Template <code class="computeroutput">buffered_channel&lt;&gt;</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">buffered_channel</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">buffered_channel</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">value_type</span><span class="special">;</span> <span class="keyword">class</span> <span class="identifier">iterator</span><span class="special">;</span> <span class="keyword">explicit</span> <span class="identifier">buffered_channel</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">capacity</span><span class="special">);</span> <span class="identifier">buffered_channel</span><span class="special">(</span> <span class="identifier">buffered_channel</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">buffered_channel</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">buffered_channel</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">close</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">try_push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">try_push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">value_type</span> <span class="identifier">value_pop</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">try_pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="special">};</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">);</span> <span class="special">}}</span> </pre> <h6> <a name="fiber.synchronization.channels.buffered_channel.h0"></a> <span><a name="fiber.synchronization.channels.buffered_channel.constructor"></a></span><a class="link" href="buffered_channel.html#fiber.synchronization.channels.buffered_channel.constructor">Constructor</a> </h6> <pre class="programlisting"><span class="keyword">explicit</span> <span class="identifier">buffered_channel</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">capacity</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="number">2</span><span class="special">&lt;=</span><span class="identifier">capacity</span> <span class="special">&amp;&amp;</span> <span class="number">0</span><span class="special">==(</span><span class="identifier">capacity</span> <span class="special">&amp;</span> <span class="special">(</span><span class="identifier">capacity</span><span class="special">-</span><span class="number">1</span><span class="special">))</span></code> </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> The constructor constructs an object of class <code class="computeroutput"><span class="identifier">buffered_channel</span></code> with an internal buffer of size <code class="computeroutput"><span class="identifier">capacity</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>invalid_argument</strong></span>: if <code class="computeroutput"><span class="number">0</span><span class="special">==</span><span class="identifier">capacity</span> <span class="special">||</span> <span class="number">0</span><span class="special">!=(</span><span class="identifier">capacity</span> <span class="special">&amp;</span> <span class="special">(</span><span class="identifier">capacity</span><span class="special">-</span><span class="number">1</span><span class="special">))</span></code>. </p></dd> <dt><span class="term">Notes:</span></dt> <dd><p> A <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">push_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">push_wait_until</span><span class="special">()</span></code> will not block until the number of values in the channel becomes equal to <code class="computeroutput"><span class="identifier">capacity</span></code>. The channel can hold only <code class="computeroutput"><span class="identifier">capacity</span> <span class="special">-</span> <span class="number">1</span></code> elements, otherwise it is considered to be full. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_close_bridgehead"></a> <span><a name="buffered_channel_close"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_close">Member function <code class="computeroutput">close</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">close</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Deactivates the channel. No values can be put after calling <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">()</span></code>. Fibers blocked in <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_until</span><span class="special">()</span></code> will return <code class="computeroutput"><span class="identifier">closed</span></code>. Fibers blocked in <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span></code> will receive an exception. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code> is like closing a pipe. It informs waiting consumers that no more values will arrive. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_push_bridgehead"></a> <span><a name="buffered_channel_push"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_push">Member function <code class="computeroutput">push</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If channel is closed, returns <code class="computeroutput"><span class="identifier">closed</span></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_until</span><span class="special">()</span></code> and returns <code class="computeroutput"><span class="identifier">success</span></code>. If the channel is full, the fiber is blocked. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_try_push_bridgehead"></a> <span><a name="buffered_channel_try_push"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_try_push">Member function <code class="computeroutput">try_push</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">try_push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">try_push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If channel is closed, returns <code class="computeroutput"><span class="identifier">closed</span></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_until</span><span class="special">()</span></code> and returns <code class="computeroutput"><span class="identifier">success</span></code>. If the channel is full, it doesn't block and returns <code class="computeroutput"><span class="identifier">full</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_pop_bridgehead"></a> <span><a name="buffered_channel_pop"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_pop">Member function <code class="computeroutput">pop</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value) or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_value_pop_bridgehead"></a> <span><a name="buffered_channel_value_pop"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_value_pop">Member function <code class="computeroutput">value_pop</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">value_type</span> <span class="identifier">value_pop</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (which throws an exception). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is closed or by copy- or move-operations. </p></dd> <dt><span class="term">Error conditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">errc</span><span class="special">::</span><span class="identifier">operation_not_permitted</span></code> </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_try_pop_bridgehead"></a> <span><a name="buffered_channel_try_pop"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_try_pop">Member function <code class="computeroutput">try_pop</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">try_pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If channel is empty, returns <code class="computeroutput"><span class="identifier">empty</span></code>. If channel is closed, returns <code class="computeroutput"><span class="identifier">closed</span></code>. Otherwise it returns <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains the dequeued value. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_pop_wait_for_bridgehead"></a> <span><a name="buffered_channel_pop_wait_for"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_pop_wait_for">Member function <code class="computeroutput">pop_wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Accepts <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span></code> and internally computes a timeout time as (system time + <code class="computeroutput"><span class="identifier">timeout_duration</span></code>). If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value), or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>), or the system time reaches the computed timeout time (return value <code class="computeroutput"><span class="identifier">timeout</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions or by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="buffered_channel_pop_wait_until_bridgehead"></a> <span><a name="buffered_channel_pop_wait_until"></a></span> <a class="link" href="buffered_channel.html#buffered_channel_pop_wait_until">Member function <code class="computeroutput">pop_wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Accepts a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span></code>. If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value), or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>), or the system time reaches the passed <code class="computeroutput"><span class="identifier">time_point</span></code> (return value <code class="computeroutput"><span class="identifier">timeout</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions or by copy- or move-operations. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.channels.buffered_channel.h1"></a> <span><a name="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"></a></span><a class="link" href="buffered_channel.html#fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code class="computeroutput"><span class="identifier">begin</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;)</span></code></a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> Returns a range-iterator (input-iterator). </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.channels.buffered_channel.h2"></a> <span><a name="fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"></a></span><a class="link" href="buffered_channel.html#fiber.synchronization.channels.buffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__buffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code class="computeroutput"><span class="identifier">end</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;)</span></code></a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span> <span class="identifier">buffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> Returns an end range-iterator (input-iterator). </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../channels.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../channels.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unbuffered_channel.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber/synchronization
repos/fiber/doc/html/fiber/synchronization/channels/unbuffered_channel.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Unbuffered Channel</title> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../channels.html" title="Channels"> <link rel="prev" href="buffered_channel.html" title="Buffered Channel"> <link rel="next" href="../futures.html" title="Futures"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="buffered_channel.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../channels.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../futures.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="fiber.synchronization.channels.unbuffered_channel"></a><a class="link" href="unbuffered_channel.html" title="Unbuffered Channel">Unbuffered Channel</a> </h4></div></div></div> <p> <span class="bold"><strong>Boost.Fiber</strong></span> provides template <code class="computeroutput"><span class="identifier">unbuffered_channel</span></code> suitable to synchonize fibers (running on same or different threads) via synchronous message passing. A fiber waiting to consume an value will block until the value is produced. If a fiber attempts to send a value through an unbuffered channel and no fiber is waiting to receive the value, the channel will block the sending fiber. </p> <p> The unbuffered channel acts as an <code class="computeroutput"><span class="identifier">rendezvous</span> <span class="identifier">point</span></code>. </p> <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">send</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">5</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="identifier">i</span><span class="special">);</span> <span class="special">}</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">recv</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">success</span> <span class="special">==</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"received "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="identifier">channel_t</span> <span class="identifier">chan</span><span class="special">{</span> <span class="number">1</span> <span class="special">};</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f1</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="identifier">send</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f2</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="identifier">recv</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="identifier">f1</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> <span class="identifier">f2</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> </pre> <p> Range-for syntax is supported: </p> <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">channel_t</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">1</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">1</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">2</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">3</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">5</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">8</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span> <span class="number">12</span><span class="special">);</span> <span class="identifier">chan</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">bar</span><span class="special">(</span> <span class="identifier">channel_t</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">value</span> <span class="special">:</span> <span class="identifier">chan</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="special">}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> </pre> <p> </p> <h5> <a name="class_unbuffered_channel_bridgehead"></a> <span><a name="class_unbuffered_channel"></a></span> <a class="link" href="unbuffered_channel.html#class_unbuffered_channel">Template <code class="computeroutput">unbuffered_channel&lt;&gt;</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">unbuffered_channel</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">unbuffered_channel</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">value_type</span><span class="special">;</span> <span class="keyword">class</span> <span class="identifier">iterator</span><span class="special">;</span> <span class="identifier">unbuffered_channel</span><span class="special">();</span> <span class="identifier">unbuffered_channel</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">unbuffered_channel</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">unbuffered_channel</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">close</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">push_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">value_type</span> <span class="identifier">value_pop</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">);</span> <span class="special">};</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">chan</span><span class="special">);</span> <span class="special">}}</span> </pre> <h6> <a name="fiber.synchronization.channels.unbuffered_channel.h0"></a> <span><a name="fiber.synchronization.channels.unbuffered_channel.constructor"></a></span><a class="link" href="unbuffered_channel.html#fiber.synchronization.channels.unbuffered_channel.constructor">Constructor</a> </h6> <pre class="programlisting"><span class="identifier">unbuffered_channel</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> The constructor constructs an object of class <code class="computeroutput"><span class="identifier">unbuffered_channel</span></code>. </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_close_bridgehead"></a> <span><a name="unbuffered_channel_close"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_close">Member function <code class="computeroutput">close</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">close</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Deactivates the channel. No values can be put after calling <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">close</span><span class="special">()</span></code>. Fibers blocked in <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_until</span><span class="special">()</span></code> will return <code class="computeroutput"><span class="identifier">closed</span></code>. Fibers blocked in <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span></code> will receive an exception. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code> is like closing a pipe. It informs waiting consumers that no more values will arrive. </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_push_bridgehead"></a> <span><a name="unbuffered_channel_push"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_push">Member function <code class="computeroutput">push</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> <span class="identifier">channel_op_status</span> <span class="identifier">push</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If channel is closed, returns <code class="computeroutput"><span class="identifier">closed</span></code>. Otherwise enqueues the value in the channel, wakes up a fiber blocked on <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">value_pop</span><span class="special">()</span></code>, <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_for</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">pop_wait_until</span><span class="special">()</span></code> and returns <code class="computeroutput"><span class="identifier">success</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_pop_bridgehead"></a> <span><a name="unbuffered_channel_pop"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_pop">Member function <code class="computeroutput">pop</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">channel_op_status</span> <span class="identifier">pop</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value) or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Exceptions thrown by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_value_pop_bridgehead"></a> <span><a name="unbuffered_channel_value_pop"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_value_pop">Member function <code class="computeroutput">value_pop</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">value_type</span> <span class="identifier">value_pop</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Dequeues a value from the channel. If the channel is empty, the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (which throws an exception). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is closed or by copy- or move-operations. </p></dd> <dt><span class="term">Error conditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">errc</span><span class="special">::</span><span class="identifier">operation_not_permitted</span></code> </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_pop_wait_for_bridgehead"></a> <span><a name="unbuffered_channel_pop_wait_for"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_pop_wait_for">Member function <code class="computeroutput">pop_wait_for</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_for</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_duration</span><span class="special">)</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Accepts <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span></code> and internally computes a timeout time as (system time + <code class="computeroutput"><span class="identifier">timeout_duration</span></code>). If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value), or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>), or the system time reaches the computed timeout time (return value <code class="computeroutput"><span class="identifier">timeout</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions or by copy- or move-operations. </p></dd> </dl> </div> <p> </p> <h5> <a name="unbuffered_channel_pop_wait_until_bridgehead"></a> <span><a name="unbuffered_channel_pop_wait_until"></a></span> <a class="link" href="unbuffered_channel.html#unbuffered_channel_pop_wait_until">Member function <code class="computeroutput">pop_wait_until</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="identifier">channel_op_status</span> <span class="identifier">pop_wait_until</span><span class="special">(</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="identifier">va</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">timeout_time</span><span class="special">)</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Accepts a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span></code>. If channel is not empty, immediately dequeues a value from the channel. Otherwise the fiber gets suspended until at least one new item is <code class="computeroutput"><span class="identifier">push</span><span class="special">()</span></code>ed (return value <code class="computeroutput"><span class="identifier">success</span></code> and <code class="computeroutput"><span class="identifier">va</span></code> contains dequeued value), or the channel gets <code class="computeroutput"><span class="identifier">close</span><span class="special">()</span></code>d (return value <code class="computeroutput"><span class="identifier">closed</span></code>), or the system time reaches the passed <code class="computeroutput"><span class="identifier">time_point</span></code> (return value <code class="computeroutput"><span class="identifier">timeout</span></code>). </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions or by copy- or move-operations. </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.channels.unbuffered_channel.h1"></a> <span><a name="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"></a></span><a class="link" href="unbuffered_channel.html#fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__begin__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code class="computeroutput"><span class="identifier">begin</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;)</span></code></a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> Returns a range-iterator (input-iterator). </p></dd> </dl> </div> <h6> <a name="fiber.synchronization.channels.unbuffered_channel.h2"></a> <span><a name="fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_"></a></span><a class="link" href="unbuffered_channel.html#fiber.synchronization.channels.unbuffered_channel.non_member_function__code__phrase_role__identifier__end__phrase__phrase_role__special_____phrase___phrase_role__identifier__unbuffered_channel__phrase__phrase_role__special___lt___phrase___phrase_role__identifier__t__phrase___phrase_role__special___gt___phrase___phrase_role__special___amp____phrase___code_">Non-member function <code class="computeroutput"><span class="identifier">end</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;)</span></code></a> </h6> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">R</span> <span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span> <span class="identifier">unbuffered_channel</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> Returns an end range-iterator (input-iterator). </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="buffered_channel.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../channels.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../futures.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/integration/embedded_main_loop.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Embedded Main Loop</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../integration.html" title="Sharing a Thread with Another Main Loop"> <link rel="prev" href="event_driven_program.html" title="Event-Driven Program"> <link rel="next" href="deeper_dive_into___boost_asio__.html" title="Deeper Dive into Boost.Asio"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="event_driven_program.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="deeper_dive_into___boost_asio__.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.integration.embedded_main_loop"></a><a name="embedded_main_loop"></a><a class="link" href="embedded_main_loop.html" title="Embedded Main Loop">Embedded Main Loop</a> </h3></div></div></div> <p> More challenging is when the application&#8217;s main loop is embedded in some other library or framework. Such an application will typically, after performing all necessary setup, pass control to some form of <code class="computeroutput"><span class="identifier">run</span><span class="special">()</span></code> function from which control does not return until application shutdown. </p> <p> A <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a> program might call <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code></a> in this way. </p> <p> In general, the trick is to arrange to pass control to <a class="link" href="../fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a> frequently. You could use an <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/high_resolution_timer.html" target="_top">Asio timer</a> for that purpose. You could instantiate the timer, arranging to call a handler function when the timer expires. The handler function could call <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code>, then reset the timer and arrange to wake up again on its next expiration. </p> <p> Since, in this thought experiment, we always pass control to the fiber manager via <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code>, the calling fiber is never blocked. Therefore there is always at least one ready fiber. Therefore the fiber manager never calls <a class="link" href="../scheduling.html#algorithm_suspend_until"><code class="computeroutput">algorithm::suspend_until()</code></a>. </p> <p> Using <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/post.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">post</span><span class="special">()</span></code></a> instead of setting a timer for some nonzero interval would be unfriendly to other threads. When all I/O is pending and all fibers are blocked, the io_service and the fiber manager would simply spin the CPU, passing control back and forth to each other. Using a timer allows tuning the responsiveness of this thread relative to others. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="event_driven_program.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="deeper_dive_into___boost_asio__.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/integration/event_driven_program.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Event-Driven Program</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../integration.html" title="Sharing a Thread with Another Main Loop"> <link rel="prev" href="overview.html" title="Overview"> <link rel="next" href="embedded_main_loop.html" title="Embedded Main Loop"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.integration.event_driven_program"></a><a class="link" href="event_driven_program.html" title="Event-Driven Program">Event-Driven Program</a> </h3></div></div></div> <p> Consider a classic event-driven program, organized around a main loop that fetches and dispatches incoming I/O events. You are introducing <span class="bold"><strong>Boost.Fiber</strong></span> because certain asynchronous I/O sequences are logically sequential, and for those you want to write and maintain code that looks and acts sequential. </p> <p> You are launching fibers on the application&#8217;s main thread because certain of their actions will affect its user interface, and the application&#8217;s UI framework permits UI operations only on the main thread. Or perhaps those fibers need access to main-thread data, and it would be too expensive in runtime (or development time) to robustly defend every such data item with thread synchronization primitives. </p> <p> You must ensure that the application&#8217;s main loop <span class="emphasis"><em>itself</em></span> doesn&#8217;t monopolize the processor: that the fibers it launches will get the CPU cycles they need. </p> <p> The solution is the same as for any fiber that might claim the CPU for an extended time: introduce calls to <a class="link" href="../fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. The most straightforward approach is to call <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> on every iteration of your existing main loop. In effect, this unifies the application&#8217;s main loop with <span class="bold"><strong>Boost.Fiber</strong></span>&#8217;s internal main loop. <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> allows the fiber manager to run any fibers that have become ready since the previous iteration of the application&#8217;s main loop. When these fibers have had a turn, control passes to the thread&#8217;s main fiber, which returns from <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> and resumes the application&#8217;s main loop. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/integration/deeper_dive_into___boost_asio__.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Deeper Dive into Boost.Asio</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../integration.html" title="Sharing a Thread with Another Main Loop"> <link rel="prev" href="embedded_main_loop.html" title="Embedded Main Loop"> <link rel="next" href="../speculation.html" title="Specualtive execution"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../speculation.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.integration.deeper_dive_into___boost_asio__"></a><a class="link" href="deeper_dive_into___boost_asio__.html" title="Deeper Dive into Boost.Asio">Deeper Dive into <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a> </h3></div></div></div> <p> By now the alert reader is thinking: but surely, with Asio in particular, we ought to be able to do much better than periodic polling pings! </p> <p> This turns out to be surprisingly tricky. We present a possible approach in <a href="../../../../examples/asio/round_robin.hpp" target="_top"><code class="computeroutput"><span class="identifier">examples</span><span class="special">/</span><span class="identifier">asio</span><span class="special">/</span><span class="identifier">round_robin</span><span class="special">.</span><span class="identifier">hpp</span></code></a>. </p> <p> One consequence of using <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a> is that you must always let Asio suspend the running thread. Since Asio is aware of pending I/O requests, it can arrange to suspend the thread in such a way that the OS will wake it on I/O completion. No one else has sufficient knowledge. </p> <p> So the fiber scheduler must depend on Asio for suspension and resumption. It requires Asio handler calls to wake it. </p> <p> One dismaying implication is that we cannot support multiple threads calling <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code></a> on the same <code class="computeroutput"><span class="identifier">io_service</span></code> instance. The reason is that Asio provides no way to constrain a particular handler to be called only on a specified thread. A fiber scheduler instance is locked to a particular thread: that instance cannot manage any other thread&#8217;s fibers. Yet if we allow multiple threads to call <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code> on the same <code class="computeroutput"><span class="identifier">io_service</span></code> instance, a fiber scheduler which needs to sleep can have no guarantee that it will reawaken in a timely manner. It can set an Asio timer, as described above &#8212; but that timer&#8217;s handler may well execute on a different thread! </p> <p> Another implication is that since an Asio-aware fiber scheduler (not to mention <a class="link" href="../callbacks/then_there_s____boost_asio__.html#callbacks_asio"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">yield</span></code></a>) depends on handler calls from the <code class="computeroutput"><span class="identifier">io_service</span></code>, it is the application&#8217;s responsibility to ensure that <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stop.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">stop</span><span class="special">()</span></code></a> is not called until every fiber has terminated. </p> <p> It is easier to reason about the behavior of the presented <code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code> scheduler if we require that after initial setup, the thread&#8217;s main fiber is the fiber that calls <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code>, so let&#8217;s impose that requirement. </p> <p> Naturally, the first thing we must do on each thread using a custom fiber scheduler is call <a class="link" href="../fiber_mgmt/fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a>. However, since <code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code> requires an <code class="computeroutput"><span class="identifier">io_service</span></code> instance, we must first declare that. </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="special">&gt;</span> <span class="identifier">io_svc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="special">&gt;();</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span> <span class="special">&gt;(</span> <span class="identifier">io_svc</span><span class="special">);</span> </pre> <p> </p> <p> <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code> instantiates <code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code>, which naturally calls its constructor: </p> <p> </p> <pre class="programlisting"><span class="identifier">round_robin</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">io_svc</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">io_svc_</span><span class="special">(</span> <span class="identifier">io_svc</span><span class="special">),</span> <span class="identifier">suspend_timer_</span><span class="special">(</span> <span class="special">*</span> <span class="identifier">io_svc_</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// We use add_service() very deliberately. This will throw</span> <span class="comment">// service_already_exists if you pass the same io_service instance to</span> <span class="comment">// more than one round_robin instance.</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">add_service</span><span class="special">(</span> <span class="special">*</span> <span class="identifier">io_svc_</span><span class="special">,</span> <span class="keyword">new</span> <span class="identifier">service</span><span class="special">(</span> <span class="special">*</span> <span class="identifier">io_svc_</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">io_svc_</span><span class="special">-&gt;</span><span class="identifier">post</span><span class="special">([</span><span class="keyword">this</span><span class="special">]()</span> <span class="keyword">mutable</span> <span class="special">{</span> </pre> <p> </p> <p> <code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code> binds the passed <code class="computeroutput"><span class="identifier">io_service</span></code> pointer and initializes a <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/steady_timer.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">steady_timer</span></code></a>: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="special">&gt;</span> <span class="identifier">io_svc_</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">steady_timer</span> <span class="identifier">suspend_timer_</span><span class="special">;</span> </pre> <p> </p> <p> Then it calls <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/add_service.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">add_service</span><span class="special">()</span></code></a> with a nested <code class="computeroutput"><span class="identifier">service</span></code> struct: </p> <p> </p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">service</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">service</span> <span class="special">{</span> <span class="keyword">static</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">id</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">work</span> <span class="special">&gt;</span> <span class="identifier">work_</span><span class="special">;</span> <span class="identifier">service</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="special">&amp;</span> <span class="identifier">io_svc</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">service</span><span class="special">(</span> <span class="identifier">io_svc</span><span class="special">),</span> <span class="identifier">work_</span><span class="special">{</span> <span class="keyword">new</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">work</span><span class="special">(</span> <span class="identifier">io_svc</span><span class="special">)</span> <span class="special">}</span> <span class="special">{</span> <span class="special">}</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">service</span><span class="special">()</span> <span class="special">{}</span> <span class="identifier">service</span><span class="special">(</span> <span class="identifier">service</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">service</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">service</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">shutdown_service</span><span class="special">()</span> <span class="identifier">override</span> <span class="identifier">final</span> <span class="special">{</span> <span class="identifier">work_</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span> <span class="special">}</span> <span class="special">};</span> </pre> <p> </p> <p> ... [asio_rr_service_bottom] </p> <p> The <code class="computeroutput"><span class="identifier">service</span></code> struct has a couple of roles. </p> <p> Its foremost role is to manage a <code class="literal">std::unique_ptr&lt;<a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service__work.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">work</span></code></a>&gt;</code>. We want the <code class="computeroutput"><span class="identifier">io_service</span></code> instance to continue its main loop even when there is no pending Asio I/O. </p> <p> But when <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service__service/shutdown_service.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">service</span><span class="special">::</span><span class="identifier">shutdown_service</span><span class="special">()</span></code></a> is called, we discard the <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">work</span></code> instance so the <code class="computeroutput"><span class="identifier">io_service</span></code> can shut down properly. </p> <p> Its other purpose is to <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/post.html" target="_top"><code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code></a> a lambda (not yet shown). Let&#8217;s walk further through the example program before coming back to explain that lambda. </p> <p> The <code class="computeroutput"><span class="identifier">service</span></code> constructor returns to <code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code>&#8217;s constructor, which returns to <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code>, which returns to the application code. </p> <p> Once it has called <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code>, the application may now launch some number of fibers: </p> <p> </p> <pre class="programlisting"><span class="comment">// server</span> <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">acceptor</span> <span class="identifier">a</span><span class="special">(</span> <span class="special">*</span> <span class="identifier">io_svc</span><span class="special">,</span> <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">endpoint</span><span class="special">(</span> <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">v4</span><span class="special">(),</span> <span class="number">9999</span><span class="special">)</span> <span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">server</span><span class="special">,</span> <span class="identifier">io_svc</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">a</span><span class="special">)</span> <span class="special">).</span><span class="identifier">detach</span><span class="special">();</span> <span class="comment">// client</span> <span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">iterations</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> <span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">clients</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">barrier</span> <span class="identifier">b</span><span class="special">(</span> <span class="identifier">clients</span><span class="special">);</span> <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">clients</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">client</span><span class="special">,</span> <span class="identifier">io_svc</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">a</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span> <span class="identifier">b</span><span class="special">),</span> <span class="identifier">iterations</span><span class="special">).</span><span class="identifier">detach</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> Since we don&#8217;t specify a <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput">launch</code></a>, these fibers are ready to run, but have not yet been entered. </p> <p> Having set everything up, the application calls <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run</span><span class="special">()</span></code></a>: </p> <p> </p> <pre class="programlisting"><span class="identifier">io_svc</span><span class="special">-&gt;</span><span class="identifier">run</span><span class="special">();</span> </pre> <p> </p> <p> Now what? </p> <p> Because this <code class="computeroutput"><span class="identifier">io_service</span></code> instance owns an <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">work</span></code> instance, <code class="computeroutput"><span class="identifier">run</span><span class="special">()</span></code> does not immediately return. But &#8212; none of the fibers that will perform actual work has even been entered yet! </p> <p> Without that initial <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> call in <code class="computeroutput"><span class="identifier">service</span></code>&#8217;s constructor, <span class="emphasis"><em>nothing</em></span> would happen. The application would hang right here. </p> <p> So, what should the <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> handler execute? Simply <a class="link" href="../fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>? </p> <p> That would be a promising start. But we have no guarantee that any of the other fibers will initiate any Asio operations to keep the ball rolling. For all we know, every other fiber could reach a similar <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">yield</span><span class="special">()</span></code> call first. Control would return to the <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> handler, which would return to Asio, and... the application would hang. </p> <p> The <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> handler could <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> itself again. But as discussed in <a class="link" href="embedded_main_loop.html#embedded_main_loop">the previous section</a>, once there are actual I/O operations in flight &#8212; once we reach a state in which no fiber is ready &#8212; that would cause the thread to spin. </p> <p> We could, of course, set an Asio timer &#8212; again as <a class="link" href="embedded_main_loop.html#embedded_main_loop">previously discussed</a>. But in this <span class="quote">&#8220;<span class="quote">deeper dive,</span>&#8221;</span> we&#8217;re trying to do a little better. </p> <p> The key to doing better is that since we&#8217;re in a fiber, we can run an actual loop &#8212; not just a chain of callbacks. We can wait for <span class="quote">&#8220;<span class="quote">something to happen</span>&#8221;</span> by calling <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/run_one.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run_one</span><span class="special">()</span></code></a> &#8212; or we can execute already-queued Asio handlers by calling <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/poll.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">poll</span><span class="special">()</span></code></a>. </p> <p> Here&#8217;s the body of the lambda passed to the <code class="computeroutput"><span class="identifier">post</span><span class="special">()</span></code> call. </p> <p> </p> <pre class="programlisting"> <span class="keyword">while</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">io_svc_</span><span class="special">-&gt;</span><span class="identifier">stopped</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="comment">// run all pending handlers in round_robin</span> <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">io_svc_</span><span class="special">-&gt;</span><span class="identifier">poll</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// block this fiber till all pending (ready) fibers are processed</span> <span class="comment">// == round_robin::suspend_until() has been called</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="special">&gt;</span> <span class="identifier">lk</span><span class="special">(</span> <span class="identifier">mtx_</span><span class="special">);</span> <span class="identifier">cnd_</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span> <span class="comment">// run one handler inside io_service</span> <span class="comment">// if no handler available, block this thread</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">io_svc_</span><span class="special">-&gt;</span><span class="identifier">run_one</span><span class="special">()</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">break</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="special">}</span> </pre> <p> </p> <p> We want this loop to exit once the <code class="computeroutput"><span class="identifier">io_service</span></code> instance has been <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stopped.html" target="_top"><code class="computeroutput"><span class="identifier">stopped</span><span class="special">()</span></code></a>. </p> <p> As long as there are ready fibers, we interleave running ready Asio handlers with running ready fibers. </p> <p> If there are no ready fibers, we wait by calling <code class="computeroutput"><span class="identifier">run_one</span><span class="special">()</span></code>. Once any Asio handler has been called &#8212; no matter which &#8212; <code class="computeroutput"><span class="identifier">run_one</span><span class="special">()</span></code> returns. That handler may have transitioned some fiber to ready state, so we loop back to check again. </p> <p> (We won&#8217;t describe <code class="computeroutput"><span class="identifier">awakened</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">pick_next</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">has_ready_fibers</span><span class="special">()</span></code>, as these are just like <a class="link" href="../scheduling.html#round_robin_awakened"><code class="computeroutput">round_robin::awakened()</code></a>, <a class="link" href="../scheduling.html#round_robin_pick_next"><code class="computeroutput">round_robin::pick_next()</code></a> and <a class="link" href="../scheduling.html#round_robin_has_ready_fibers"><code class="computeroutput">round_robin::has_ready_fibers()</code></a>.) </p> <p> That leaves <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code>. </p> <p> Doubtless you have been asking yourself: why are we calling <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run_one</span><span class="special">()</span></code> in the lambda loop? Why not call it in <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code>, whose very API was designed for just such a purpose? </p> <p> Under normal circumstances, when the fiber manager finds no ready fibers, it calls <a class="link" href="../scheduling.html#algorithm_suspend_until"><code class="computeroutput">algorithm::suspend_until()</code></a>. Why test <code class="computeroutput"><span class="identifier">has_ready_fibers</span><span class="special">()</span></code> in the lambda loop? Why not leverage the normal mechanism? </p> <p> The answer is: it matters who&#8217;s asking. </p> <p> Consider the lambda loop shown above. The only <span class="bold"><strong>Boost.Fiber</strong></span> APIs it engages are <code class="computeroutput"><span class="identifier">has_ready_fibers</span><span class="special">()</span></code> and <a class="link" href="../fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>. <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> does not <span class="emphasis"><em>block</em></span> the calling fiber: the calling fiber does not become unready. It is immediately passed back to <a class="link" href="../scheduling.html#algorithm_awakened"><code class="computeroutput">algorithm::awakened()</code></a>, to be resumed in its turn when all other ready fibers have had a chance to run. In other words: during a <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> call, <span class="emphasis"><em>there is always at least one ready fiber.</em></span> </p> <p> As long as this lambda loop is still running, the fiber manager does not call <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> because it always has a fiber ready to run. </p> <p> However, the lambda loop <span class="emphasis"><em>itself</em></span> can detect the case when no <span class="emphasis"><em>other</em></span> fibers are ready to run: the running fiber is not <span class="emphasis"><em>ready</em></span> but <span class="emphasis"><em>running.</em></span> </p> <p> That said, <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> are in fact called during orderly shutdown processing, so let&#8217;s try a plausible implementation. </p> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">suspend_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="comment">// Set a timer so at least one handler will eventually fire, causing</span> <span class="comment">// run_one() to eventually return.</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">::</span><span class="identifier">max</span><span class="special">)()</span> <span class="special">!=</span> <span class="identifier">abs_time</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Each expires_at(time_point) call cancels any previous pending</span> <span class="comment">// call. We could inadvertently spin like this:</span> <span class="comment">// dispatcher calls suspend_until() with earliest wake time</span> <span class="comment">// suspend_until() sets suspend_timer_</span> <span class="comment">// lambda loop calls run_one()</span> <span class="comment">// some other asio handler runs before timer expires</span> <span class="comment">// run_one() returns to lambda loop</span> <span class="comment">// lambda loop yields to dispatcher</span> <span class="comment">// dispatcher finds no ready fibers</span> <span class="comment">// dispatcher calls suspend_until() with SAME wake time</span> <span class="comment">// suspend_until() sets suspend_timer_ to same time, canceling</span> <span class="comment">// previous async_wait()</span> <span class="comment">// lambda loop calls run_one()</span> <span class="comment">// asio calls suspend_timer_ handler with operation_aborted</span> <span class="comment">// run_one() returns to lambda loop... etc. etc.</span> <span class="comment">// So only actually set the timer when we're passed a DIFFERENT</span> <span class="comment">// abs_time value.</span> <span class="identifier">suspend_timer_</span><span class="special">.</span><span class="identifier">expires_at</span><span class="special">(</span> <span class="identifier">abs_time</span><span class="special">);</span> <span class="identifier">suspend_timer_</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">([](</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;){</span> <span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">yield</span><span class="special">();</span> <span class="special">});</span> <span class="special">}</span> <span class="identifier">cnd_</span><span class="special">.</span><span class="identifier">notify_one</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> As you might expect, <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> sets an <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/steady_timer.html" target="_top"><code class="computeroutput"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">steady_timer</span></code></a> to <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/expires_at.html" target="_top"><code class="computeroutput"><span class="identifier">expires_at</span><span class="special">()</span></code></a> the passed <a href="http://en.cppreference.com/w/cpp/chrono/steady_clock" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">time_point</span></code></a>. Usually. </p> <p> As indicated in comments, we avoid setting <code class="computeroutput"><span class="identifier">suspend_timer_</span></code> multiple times to the <span class="emphasis"><em>same</em></span> <code class="computeroutput"><span class="identifier">time_point</span></code> value since every <code class="computeroutput"><span class="identifier">expires_at</span><span class="special">()</span></code> call cancels any previous <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/async_wait.html" target="_top"><code class="computeroutput"><span class="identifier">async_wait</span><span class="special">()</span></code></a> call. There is a chance that we could spin. Reaching <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> means the fiber manager intends to yield the processor to Asio. Cancelling the previous <code class="computeroutput"><span class="identifier">async_wait</span><span class="special">()</span></code> call would fire its handler, causing <code class="computeroutput"><span class="identifier">run_one</span><span class="special">()</span></code> to return, potentially causing the fiber manager to call <code class="computeroutput"><span class="identifier">suspend_until</span><span class="special">()</span></code> again with the same <code class="computeroutput"><span class="identifier">time_point</span></code> value... </p> <p> Given that we suspend the thread by calling <code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">run_one</span><span class="special">()</span></code>, what&#8217;s important is that our <code class="computeroutput"><span class="identifier">async_wait</span><span class="special">()</span></code> call will cause a handler to run, which will cause <code class="computeroutput"><span class="identifier">run_one</span><span class="special">()</span></code> to return. It&#8217;s not so important specifically what that handler does. </p> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="comment">// Something has happened that should wake one or more fibers BEFORE</span> <span class="comment">// suspend_timer_ expires. Reset the timer to cause it to fire</span> <span class="comment">// immediately, causing the run_one() call to return. In theory we</span> <span class="comment">// could use cancel() because we don't care whether suspend_timer_'s</span> <span class="comment">// handler is called with operation_aborted or success. However --</span> <span class="comment">// cancel() doesn't change the expiration time, and we use</span> <span class="comment">// suspend_timer_'s expiration time to decide whether it's already</span> <span class="comment">// set. If suspend_until() set some specific wake time, then notify()</span> <span class="comment">// canceled it, then suspend_until() was called again with the same</span> <span class="comment">// wake time, it would match suspend_timer_'s expiration time and we'd</span> <span class="comment">// refrain from setting the timer. So instead of simply calling</span> <span class="comment">// cancel(), reset the timer, which cancels the pending sleep AND sets</span> <span class="comment">// a new expiration time. This will cause us to spin the loop twice --</span> <span class="comment">// once for the operation_aborted handler, once for timer expiration</span> <span class="comment">// -- but that shouldn't be a big problem.</span> <span class="identifier">suspend_timer_</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">([](</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;){</span> <span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">yield</span><span class="special">();</span> <span class="special">});</span> <span class="identifier">suspend_timer_</span><span class="special">.</span><span class="identifier">expires_at</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">steady_clock</span><span class="special">::</span><span class="identifier">now</span><span class="special">()</span> <span class="special">);</span> <span class="special">}</span> </pre> <p> </p> <p> Since an <code class="computeroutput"><span class="identifier">expires_at</span><span class="special">()</span></code> call cancels any previous <code class="computeroutput"><span class="identifier">async_wait</span><span class="special">()</span></code> call, we can make <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> simply call <code class="computeroutput"><span class="identifier">steady_timer</span><span class="special">::</span><span class="identifier">expires_at</span><span class="special">()</span></code>. That should cause the <code class="computeroutput"><span class="identifier">io_service</span></code> to call the <code class="computeroutput"><span class="identifier">async_wait</span><span class="special">()</span></code> handler with <code class="computeroutput"><span class="identifier">operation_aborted</span></code>. </p> <p> The comments in <code class="computeroutput"><span class="identifier">notify</span><span class="special">()</span></code> explain why we call <code class="computeroutput"><span class="identifier">expires_at</span><span class="special">()</span></code> rather than <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/basic_waitable_timer/cancel.html" target="_top"><code class="computeroutput"><span class="identifier">cancel</span><span class="special">()</span></code></a>. </p> <p> This <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">round_robin</span></code> implementation is used in <a href="../../../../examples/asio/autoecho.cpp" target="_top"><code class="computeroutput"><span class="identifier">examples</span><span class="special">/</span><span class="identifier">asio</span><span class="special">/</span><span class="identifier">autoecho</span><span class="special">.</span><span class="identifier">cpp</span></code></a>. </p> <p> It seems possible that you could put together a more elegant Fiber / Asio integration. But as noted at the outset: it&#8217;s tricky. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="embedded_main_loop.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../speculation.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/integration/overview.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Overview</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../integration.html" title="Sharing a Thread with Another Main Loop"> <link rel="prev" href="../integration.html" title="Sharing a Thread with Another Main Loop"> <link rel="next" href="event_driven_program.html" title="Event-Driven Program"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../integration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="event_driven_program.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.integration.overview"></a><a class="link" href="overview.html" title="Overview">Overview</a> </h3></div></div></div> <p> As always with cooperative concurrency, it is important not to let any one fiber monopolize the processor too long: that could <span class="quote">&#8220;<span class="quote">starve</span>&#8221;</span> other ready fibers. This section discusses a couple of solutions. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../integration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../integration.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="event_driven_program.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/stack/valgrind.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Support for valgrind</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../stack.html" title="Stack allocation"> <link rel="prev" href="../stack.html" title="Stack allocation"> <link rel="next" href="../synchronization.html" title="Synchronization"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../stack.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stack.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../synchronization.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.stack.valgrind"></a><a class="link" href="valgrind.html" title="Support for valgrind">Support for valgrind</a> </h3></div></div></div> <p> Running programs that switch stacks under valgrind causes problems. Property (b2 command-line) <code class="computeroutput"><span class="identifier">valgrind</span><span class="special">=</span><span class="identifier">on</span></code> let valgrind treat the memory regions as stack space which suppresses the errors. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../stack.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stack.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../synchronization.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/return_errorcode.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Return Errorcode</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="overview.html" title="Overview"> <link rel="next" href="success_or_exception.html" title="Success or Exception"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="success_or_exception.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.return_errorcode"></a><a class="link" href="return_errorcode.html" title="Return Errorcode">Return Errorcode</a> </h3></div></div></div> <p> The <code class="computeroutput"><span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">init_write</span><span class="special">()</span></code> callback passes only an <code class="computeroutput"><span class="identifier">errorcode</span></code>. If we simply want the blocking wrapper to return that <code class="computeroutput"><span class="identifier">errorcode</span></code>, this is an extremely straightforward use of <a class="link" href="../synchronization/futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> and <a class="link" href="../synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a>: </p> <p> </p> <pre class="programlisting"><span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">write_ec</span><span class="special">(</span> <span class="identifier">AsyncAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="special">&gt;</span> <span class="identifier">promise</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// In general, even though we block waiting for future::get() and therefore</span> <span class="comment">// won't destroy 'promise' until promise::set_value() has been called, we</span> <span class="comment">// are advised that with threads it's possible for ~promise() to be</span> <span class="comment">// entered before promise::set_value() has returned. While that shouldn't</span> <span class="comment">// happen with fibers::promise, a robust way to deal with the lifespan</span> <span class="comment">// issue is to bind 'promise' into our lambda. Since promise is move-only,</span> <span class="comment">// use initialization capture.</span> <span class="preprocessor">#if</span> <span class="special">!</span> <span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span><span class="special">)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_write</span><span class="special">(</span> <span class="identifier">data</span><span class="special">,</span> <span class="special">[</span><span class="identifier">promise</span><span class="special">=</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">)](</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">);</span> <span class="special">});</span> <span class="preprocessor">#else</span> <span class="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_write</span><span class="special">(</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">([](</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">promise</span><span class="special">,</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">);</span> <span class="special">},</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">_1</span><span class="special">)</span> <span class="special">);</span> <span class="preprocessor">#endif</span> <span class="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> All we have to do is: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem"> Instantiate a <code class="computeroutput"><span class="identifier">promise</span><span class="special">&lt;&gt;</span></code> of correct type. </li> <li class="listitem"> Obtain its <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code>. </li> <li class="listitem"> Arrange for the callback to call <a class="link" href="../synchronization/futures/promise.html#promise_set_value"><code class="computeroutput">promise::set_value()</code></a>. </li> <li class="listitem"> Block on <a class="link" href="../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a>. </li> </ol></div> <div class="note"><table border="0" summary="Note"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> <th align="left">Note</th> </tr> <tr><td align="left" valign="top"><p> This tactic for resuming a pending fiber works even if the callback is called on a different thread than the one on which the initiating fiber is running. In fact, <a href="../../../../examples/adapt_callbacks.cpp" target="_top">the example program&#8217;s</a> dummy <code class="computeroutput"><span class="identifier">AsyncAPI</span></code> implementation illustrates that: it simulates async I/O by launching a new thread that sleeps briefly and then calls the relevant callback. </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="overview.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="success_or_exception.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/success_error_virtual_methods.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Success/Error Virtual Methods</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="data_or_exception.html" title="Data or Exception"> <link rel="next" href="then_there_s____boost_asio__.html" title="Then There&#8217;s Boost.Asio"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="data_or_exception.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="then_there_s____boost_asio__.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.success_error_virtual_methods"></a><a class="link" href="success_error_virtual_methods.html" title="Success/Error Virtual Methods">Success/Error Virtual Methods</a> </h3></div></div></div> <p> One classic approach to completion notification is to define an abstract base class with <code class="computeroutput"><span class="identifier">success</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">error</span><span class="special">()</span></code> methods. Code wishing to perform async I/O must derive a subclass, override each of these methods and pass the async operation a pointer to a subclass instance. The abstract base class might look like this: </p> <p> </p> <pre class="programlisting"><span class="comment">// every async operation receives a subclass instance of this abstract base</span> <span class="comment">// class through which to communicate its result</span> <span class="keyword">struct</span> <span class="identifier">Response</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span> <span class="identifier">Response</span> <span class="special">&gt;</span> <span class="identifier">ptr</span><span class="special">;</span> <span class="comment">// called if the operation succeeds</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">success</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="comment">// called if the operation fails</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">error</span><span class="special">(</span> <span class="identifier">AsyncAPIBase</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> Now the <code class="computeroutput"><span class="identifier">AsyncAPI</span></code> operation might look more like this: </p> <p> </p> <pre class="programlisting"><span class="comment">// derive Response subclass, instantiate, pass Response::ptr</span> <span class="keyword">void</span> <span class="identifier">init_read</span><span class="special">(</span> <span class="identifier">Response</span><span class="special">::</span><span class="identifier">ptr</span><span class="special">);</span> </pre> <p> </p> <p> We can address this by writing a one-size-fits-all <code class="computeroutput"><span class="identifier">PromiseResponse</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">PromiseResponse</span><span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Response</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="comment">// called if the operation succeeds</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">success</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">promise_</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">data</span><span class="special">);</span> <span class="special">}</span> <span class="comment">// called if the operation fails</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">error</span><span class="special">(</span> <span class="identifier">AsyncAPIBase</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">promise_</span><span class="special">.</span><span class="identifier">set_exception</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_exception_ptr</span><span class="special">(</span> <span class="identifier">make_exception</span><span class="special">(</span><span class="string">"read"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">get_future</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">promise_</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">();</span> <span class="special">}</span> <span class="keyword">private</span><span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">promise_</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> Now we can simply obtain the <code class="computeroutput"><span class="identifier">future</span><span class="special">&lt;&gt;</span></code> from that <code class="computeroutput"><span class="identifier">PromiseResponse</span></code> and wait on its <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code>: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">read</span><span class="special">(</span> <span class="identifier">AsyncAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Because init_read() requires a shared_ptr, we must allocate our</span> <span class="comment">// ResponsePromise on the heap, even though we know its lifespan.</span> <span class="keyword">auto</span> <span class="identifier">promisep</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span> <span class="identifier">PromiseResponse</span> <span class="special">&gt;()</span> <span class="special">);</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">promisep</span><span class="special">-&gt;</span><span class="identifier">get_future</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// Both 'promisep' and 'future' will survive until our lambda has been</span> <span class="comment">// called.</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_read</span><span class="special">(</span> <span class="identifier">promisep</span><span class="special">);</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> The source code above is found in <a href="../../../../examples/adapt_callbacks.cpp" target="_top">adapt_callbacks.cpp</a> and <a href="../../../../examples/adapt_method_calls.cpp" target="_top">adapt_method_calls.cpp</a>. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="data_or_exception.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="then_there_s____boost_asio__.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/return_errorcode_or_data.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Return Errorcode or Data</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="success_or_exception.html" title="Success or Exception"> <link rel="next" href="data_or_exception.html" title="Data or Exception"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="success_or_exception.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="data_or_exception.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.return_errorcode_or_data"></a><a class="link" href="return_errorcode_or_data.html" title="Return Errorcode or Data">Return Errorcode or Data</a> </h3></div></div></div> <p> Things get a bit more interesting when the async operation&#8217;s callback passes multiple data items of interest. One approach would be to use <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;&gt;</span></code> to capture both: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">read_ec</span><span class="special">(</span> <span class="identifier">AsyncAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">result_pair</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">result_pair</span> <span class="special">&gt;</span> <span class="identifier">promise</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">result_pair</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// We promise that both 'promise' and 'future' will survive until our</span> <span class="comment">// lambda has been called.</span> <span class="preprocessor">#if</span> <span class="special">!</span> <span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span><span class="special">)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_read</span><span class="special">([</span><span class="identifier">promise</span><span class="special">=</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">)](</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">result_pair</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">);</span> <span class="special">});</span> <span class="preprocessor">#else</span> <span class="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_read</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">([](</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">result_pair</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">promise</span><span class="special">,</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">result_pair</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">);</span> <span class="special">},</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">_2</span><span class="special">)</span> <span class="special">);</span> <span class="preprocessor">#endif</span> <span class="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> Once you bundle the interesting data in <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;&gt;</span></code>, the code is effectively identical to <code class="computeroutput"><span class="identifier">write_ec</span><span class="special">()</span></code>. You can call it like this: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tie</span><span class="special">(</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">read_ec</span><span class="special">(</span> <span class="identifier">api</span><span class="special">);</span> </pre> <p> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="success_or_exception.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="data_or_exception.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/overview.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Overview</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="next" href="return_errorcode.html" title="Return Errorcode"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../callbacks.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="return_errorcode.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.overview"></a><a class="link" href="overview.html" title="Overview">Overview</a> </h3></div></div></div> <p> One of the primary benefits of <span class="bold"><strong>Boost.Fiber</strong></span> is the ability to use asynchronous operations for efficiency, while at the same time structuring the calling code <span class="emphasis"><em>as if</em></span> the operations were synchronous. Asynchronous operations provide completion notification in a variety of ways, but most involve a callback function of some kind. This section discusses tactics for interfacing <span class="bold"><strong>Boost.Fiber</strong></span> with an arbitrary async operation. </p> <p> For purposes of illustration, consider the following hypothetical API: </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">AsyncAPI</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="comment">// constructor acquires some resource that can be read and written</span> <span class="identifier">AsyncAPI</span><span class="special">();</span> <span class="comment">// callbacks accept an int error code; 0 == success</span> <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">errorcode</span><span class="special">;</span> <span class="comment">// write callback only needs to indicate success or failure</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">init_write</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">callback</span><span class="special">);</span> <span class="comment">// read callback needs to accept both errorcode and data</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">init_read</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">callback</span><span class="special">);</span> <span class="comment">// ... other operations ...</span> <span class="special">};</span> </pre> <p> </p> <p> The significant points about each of <code class="computeroutput"><span class="identifier">init_write</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">init_read</span><span class="special">()</span></code> are: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"> The <code class="computeroutput"><span class="identifier">AsyncAPI</span></code> method only initiates the operation. It returns immediately, while the requested operation is still pending. </li> <li class="listitem"> The method accepts a callback. When the operation completes, the callback is called with relevant parameters (error code, data if applicable). </li> </ul></div> <p> We would like to wrap these asynchronous methods in functions that appear synchronous by blocking the calling fiber until the operation completes. This lets us use the wrapper function&#8217;s return value to deliver relevant data. </p> <div class="tip"><table border="0" summary="Tip"> <tr> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td> <th align="left">Tip</th> </tr> <tr><td align="left" valign="top"><p> <a class="link" href="../synchronization/futures/promise.html#class_promise"><code class="computeroutput">promise&lt;&gt;</code></a> and <a class="link" href="../synchronization/futures/future.html#class_future"><code class="computeroutput">future&lt;&gt;</code></a> are your friends here. </p></td></tr> </table></div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../callbacks.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="return_errorcode.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/then_there_s____boost_asio__.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Then There&#8217;s Boost.Asio</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="success_error_virtual_methods.html" title="Success/Error Virtual Methods"> <link rel="next" href="../nonblocking.html" title="Integrating Fibers with Nonblocking I/O"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="success_error_virtual_methods.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../nonblocking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.then_there_s____boost_asio__"></a><a name="callbacks_asio"></a><a class="link" href="then_there_s____boost_asio__.html" title="Then There&#8217;s Boost.Asio">Then There&#8217;s <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a></a> </h3></div></div></div> <p> Since the simplest form of Boost.Asio asynchronous operation completion token is a callback function, we could apply the same tactics for Asio as for our hypothetical <code class="computeroutput"><span class="identifier">AsyncAPI</span></code> asynchronous operations. </p> <p> Fortunately we need not. Boost.Asio incorporates a mechanism<sup>[<a name="fiber.callbacks.then_there_s____boost_asio__.f0" href="#ftn.fiber.callbacks.then_there_s____boost_asio__.f0" class="footnote">5</a>]</sup> by which the caller can customize the notification behavior of any async operation. Therefore we can construct a <span class="emphasis"><em>completion token</em></span> which, when passed to a <a href="http://www.boost.org/doc/libs/release/libs/asio/index.html" target="_top">Boost.Asio</a> async operation, requests blocking for the calling fiber. </p> <p> A typical Asio async function might look something like this:<sup>[<a name="fiber.callbacks.then_there_s____boost_asio__.f1" href="#ftn.fiber.callbacks.then_there_s____boost_asio__.f1" class="footnote">6</a>]</sup> </p> <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span> <span class="special">...,</span> <span class="keyword">class</span> <span class="identifier">CompletionToken</span> <span class="special">&gt;</span> <span class="emphasis"><em>deduced_return_type</em></span> <span class="identifier">async_something</span><span class="special">(</span> <span class="special">...</span> <span class="special">,</span> <span class="identifier">CompletionToken</span><span class="special">&amp;&amp;</span> <span class="identifier">token</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// construct handler_type instance from CompletionToken</span> <span class="identifier">handler_type</span><span class="special">&lt;</span><span class="identifier">CompletionToken</span><span class="special">,</span> <span class="special">...&gt;::</span><span class="identifier">type</span> <span class="bold"><strong><code class="computeroutput">handler(token)</code></strong></span><span class="special">;</span> <span class="comment">// construct async_result instance from handler_type</span> <span class="identifier">async_result</span><span class="special">&lt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">handler</span><span class="special">)&gt;</span> <span class="bold"><strong><code class="computeroutput">result(handler)</code></strong></span><span class="special">;</span> <span class="comment">// ... arrange to call handler on completion ...</span> <span class="comment">// ... initiate actual I/O operation ...</span> <span class="keyword">return</span> <span class="bold"><strong><code class="computeroutput">result.get()</code></strong></span><span class="special">;</span> <span class="special">}</span> </pre> <p> We will engage that mechanism, which is based on specializing Asio&#8217;s <code class="computeroutput"><span class="identifier">handler_type</span><span class="special">&lt;&gt;</span></code> template for the <code class="computeroutput"><span class="identifier">CompletionToken</span></code> type and the signature of the specific callback. The remainder of this discussion will refer back to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> as the Asio async function under consideration. </p> <p> The implementation described below uses lower-level facilities than <code class="computeroutput"><span class="identifier">promise</span></code> and <code class="computeroutput"><span class="identifier">future</span></code> because the <code class="computeroutput"><span class="identifier">promise</span></code> mechanism interacts badly with <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service/stop.html" target="_top"><code class="computeroutput"><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">stop</span><span class="special">()</span></code></a>. It produces <code class="computeroutput"><span class="identifier">broken_promise</span></code> exceptions. </p> <p> <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">yield</span></code> is a completion token of this kind. <code class="computeroutput"><span class="identifier">yield</span></code> is an instance of <code class="computeroutput"><span class="identifier">yield_t</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">yield_t</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">yield_t</span><span class="special">()</span> <span class="special">=</span> <span class="keyword">default</span><span class="special">;</span> <span class="comment">/** * @code * static yield_t yield; * boost::system::error_code myec; * func(yield[myec]); * @endcode * @c yield[myec] returns an instance of @c yield_t whose @c ec_ points * to @c myec. The expression @c yield[myec] "binds" @c myec to that * (anonymous) @c yield_t instance, instructing @c func() to store any * @c error_code it might produce into @c myec rather than throwing @c * boost::system::system_error. */</span> <span class="identifier">yield_t</span> <span class="keyword">operator</span><span class="special">[](</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="identifier">yield_t</span> <span class="identifier">tmp</span><span class="special">;</span> <span class="identifier">tmp</span><span class="special">.</span><span class="identifier">ec_</span> <span class="special">=</span> <span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">tmp</span><span class="special">;</span> <span class="special">}</span> <span class="comment">//private:</span> <span class="comment">// ptr to bound error_code instance if any</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="special">*</span> <span class="identifier">ec_</span><span class="special">{</span> <span class="keyword">nullptr</span> <span class="special">};</span> <span class="special">};</span> </pre> <p> </p> <p> <code class="computeroutput"><span class="identifier">yield_t</span></code> is in fact only a placeholder, a way to trigger Boost.Asio customization. It can bind a <a href="http://www.boost.org/doc/libs/release/libs/system/doc/reference.html#Class-error_code" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span></code></a> for use by the actual handler. </p> <p> <code class="computeroutput"><span class="identifier">yield</span></code> is declared as: </p> <p> </p> <pre class="programlisting"><span class="comment">// canonical instance</span> <span class="keyword">thread_local</span> <span class="identifier">yield_t</span> <span class="identifier">yield</span><span class="special">{};</span> </pre> <p> </p> <p> Asio customization is engaged by specializing <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/handler_type.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">handler_type</span><span class="special">&lt;&gt;</span></code></a> for <code class="computeroutput"><span class="identifier">yield_t</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// Handler type specialisation for fibers::asio::yield.</span> <span class="comment">// When 'yield' is passed as a completion handler which accepts only</span> <span class="comment">// error_code, use yield_handler&lt;void&gt;. yield_handler will take care of the</span> <span class="comment">// error_code one way or another.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">ReturnType</span> <span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">handler_type</span><span class="special">&lt;</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">yield_t</span><span class="special">,</span> <span class="identifier">ReturnType</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)</span> <span class="special">&gt;</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="keyword">void</span> <span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> (There are actually four different specializations in <a href="../../../../examples/asio/detail/yield.hpp" target="_top">detail/yield.hpp</a>, one for each of the four Asio async callback signatures we expect.) </p> <p> The above directs Asio to use <code class="computeroutput"><span class="identifier">yield_handler</span></code> as the actual handler for an async operation to which <code class="computeroutput"><span class="identifier">yield</span></code> is passed. There&#8217;s a generic <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> implementation and a <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> specialization. Let&#8217;s start with the <code class="computeroutput"><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> specialization: </p> <p> </p> <pre class="programlisting"><span class="comment">// yield_handler&lt;void&gt; is like yield_handler&lt;T&gt; without value_. In fact it's</span> <span class="comment">// just like yield_handler_base.</span> <span class="keyword">template</span><span class="special">&lt;&gt;</span> <span class="keyword">class</span> <span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="keyword">void</span> <span class="special">&gt;:</span> <span class="keyword">public</span> <span class="identifier">yield_handler_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">explicit</span> <span class="identifier">yield_handler</span><span class="special">(</span> <span class="identifier">yield_t</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">yield_handler_base</span><span class="special">{</span> <span class="identifier">y</span> <span class="special">}</span> <span class="special">{</span> <span class="special">}</span> <span class="comment">// nullary completion callback</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()()</span> <span class="special">{</span> <span class="special">(</span> <span class="special">*</span> <span class="keyword">this</span><span class="special">)(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">()</span> <span class="special">);</span> <span class="special">}</span> <span class="comment">// inherit operator()(error_code) overload from base class</span> <span class="keyword">using</span> <span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">();</span> <span class="special">};</span> </pre> <p> </p> <p> <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>, having consulted the <code class="computeroutput"><span class="identifier">handler_type</span><span class="special">&lt;&gt;</span></code> traits specialization, instantiates a <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> to be passed as the actual callback for the async operation. <code class="computeroutput"><span class="identifier">yield_handler</span></code>&#8217;s constructor accepts the <code class="computeroutput"><span class="identifier">yield_t</span></code> instance (the <code class="computeroutput"><span class="identifier">yield</span></code> object passed to the async function) and passes it along to <code class="computeroutput"><span class="identifier">yield_handler_base</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// This class encapsulates common elements between yield_handler&lt;T&gt; (capturing</span> <span class="comment">// a value to return from asio async function) and yield_handler&lt;void&gt; (no</span> <span class="comment">// such value). See yield_handler&lt;T&gt; and its &lt;void&gt; specialization below. Both</span> <span class="comment">// yield_handler&lt;T&gt; and yield_handler&lt;void&gt; are passed by value through</span> <span class="comment">// various layers of asio functions. In other words, they're potentially</span> <span class="comment">// copied multiple times. So key data such as the yield_completion instance</span> <span class="comment">// must be stored in our async_result&lt;yield_handler&lt;&gt;&gt; specialization, which</span> <span class="comment">// should be instantiated only once.</span> <span class="keyword">class</span> <span class="identifier">yield_handler_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">yield_handler_base</span><span class="special">(</span> <span class="identifier">yield_t</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">:</span> <span class="comment">// capture the context* associated with the running fiber</span> <span class="identifier">ctx_</span><span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">active</span><span class="special">()</span> <span class="special">},</span> <span class="comment">// capture the passed yield_t</span> <span class="identifier">yt_</span><span class="special">(</span> <span class="identifier">y</span> <span class="special">)</span> <span class="special">{</span> <span class="special">}</span> <span class="comment">// completion callback passing only (error_code)</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">BOOST_ASSERT_MSG</span><span class="special">(</span> <span class="identifier">ycomp_</span><span class="special">,</span> <span class="string">"Must inject yield_completion* "</span> <span class="string">"before calling yield_handler_base::operator()()"</span><span class="special">);</span> <span class="identifier">BOOST_ASSERT_MSG</span><span class="special">(</span> <span class="identifier">yt_</span><span class="special">.</span><span class="identifier">ec_</span><span class="special">,</span> <span class="string">"Must inject boost::system::error_code* "</span> <span class="string">"before calling yield_handler_base::operator()()"</span><span class="special">);</span> <span class="comment">// If originating fiber is busy testing state_ flag, wait until it</span> <span class="comment">// has observed (completed != state_).</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">lock_t</span> <span class="identifier">lk</span><span class="special">{</span> <span class="identifier">ycomp_</span><span class="special">-&gt;</span><span class="identifier">mtx_</span> <span class="special">};</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">state_t</span> <span class="identifier">state</span> <span class="special">=</span> <span class="identifier">ycomp_</span><span class="special">-&gt;</span><span class="identifier">state_</span><span class="special">;</span> <span class="comment">// Notify a subsequent yield_completion::wait() call that it need not</span> <span class="comment">// suspend.</span> <span class="identifier">ycomp_</span><span class="special">-&gt;</span><span class="identifier">state_</span> <span class="special">=</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">complete</span><span class="special">;</span> <span class="comment">// set the error_code bound by yield_t</span> <span class="special">*</span> <span class="identifier">yt_</span><span class="special">.</span><span class="identifier">ec_</span> <span class="special">=</span> <span class="identifier">ec</span><span class="special">;</span> <span class="comment">// unlock the lock that protects state_</span> <span class="identifier">lk</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span> <span class="comment">// If ctx_ is still active, e.g. because the async operation</span> <span class="comment">// immediately called its callback (this method!) before the asio</span> <span class="comment">// async function called async_result_base::get(), we must not set it</span> <span class="comment">// ready.</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">waiting</span> <span class="special">==</span> <span class="identifier">state</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// wake the fiber</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">active</span><span class="special">()-&gt;</span><span class="identifier">schedule</span><span class="special">(</span> <span class="identifier">ctx_</span><span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="comment">//private:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span> <span class="special">*</span> <span class="identifier">ctx_</span><span class="special">;</span> <span class="identifier">yield_t</span> <span class="identifier">yt_</span><span class="special">;</span> <span class="comment">// We depend on this pointer to yield_completion, which will be injected</span> <span class="comment">// by async_result.</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">ptr_t</span> <span class="identifier">ycomp_</span><span class="special">{};</span> <span class="special">};</span> </pre> <p> </p> <p> <code class="computeroutput"><span class="identifier">yield_handler_base</span></code> stores a copy of the <code class="computeroutput"><span class="identifier">yield_t</span></code> instance &#8212; which, as shown above, contains only an <code class="computeroutput"><span class="identifier">error_code</span><span class="special">*</span></code>. It also captures the <a class="link" href="../scheduling.html#class_context"><code class="computeroutput">context</code></a>* for the currently-running fiber by calling <a class="link" href="../scheduling.html#context_active"><code class="computeroutput">context::active()</code></a>. </p> <p> You will notice that <code class="computeroutput"><span class="identifier">yield_handler_base</span></code> has one more data member (<code class="computeroutput"><span class="identifier">ycomp_</span></code>) that is initialized to <code class="computeroutput"><span class="keyword">nullptr</span></code> by its constructor &#8212; though its <code class="computeroutput"><span class="keyword">operator</span><span class="special">()()</span></code> method relies on <code class="computeroutput"><span class="identifier">ycomp_</span></code> being non-null. More on this in a moment. </p> <p> Having constructed the <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> instance, <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> goes on to construct an <code class="computeroutput"><span class="identifier">async_result</span></code> specialized for the <code class="computeroutput"><span class="identifier">handler_type</span><span class="special">&lt;&gt;::</span><span class="identifier">type</span></code>: in this case, <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;&gt;</span></code>. It passes the <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> instance to the new <code class="computeroutput"><span class="identifier">async_result</span></code> instance. </p> <p> </p> <pre class="programlisting"><span class="comment">// Without the need to handle a passed value, our yield_handler&lt;void&gt;</span> <span class="comment">// specialization is just like async_result_base.</span> <span class="keyword">template</span><span class="special">&lt;&gt;</span> <span class="keyword">class</span> <span class="identifier">async_result</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="keyword">void</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">async_result_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">type</span><span class="special">;</span> <span class="keyword">explicit</span> <span class="identifier">async_result</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="keyword">void</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">h</span><span class="special">):</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">async_result_base</span><span class="special">{</span> <span class="identifier">h</span> <span class="special">}</span> <span class="special">{</span> <span class="special">}</span> <span class="special">};</span> </pre> <p> </p> <p> Naturally that leads us straight to <code class="computeroutput"><span class="identifier">async_result_base</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// Factor out commonality between async_result&lt;yield_handler&lt;T&gt;&gt; and</span> <span class="comment">// async_result&lt;yield_handler&lt;void&gt;&gt;</span> <span class="keyword">class</span> <span class="identifier">async_result_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">explicit</span> <span class="identifier">async_result_base</span><span class="special">(</span> <span class="identifier">yield_handler_base</span> <span class="special">&amp;</span> <span class="identifier">h</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">ycomp_</span><span class="special">{</span> <span class="keyword">new</span> <span class="identifier">yield_completion</span><span class="special">{}</span> <span class="special">}</span> <span class="special">{</span> <span class="comment">// Inject ptr to our yield_completion instance into this</span> <span class="comment">// yield_handler&lt;&gt;.</span> <span class="identifier">h</span><span class="special">.</span><span class="identifier">ycomp_</span> <span class="special">=</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">ycomp_</span><span class="special">;</span> <span class="comment">// if yield_t didn't bind an error_code, make yield_handler_base's</span> <span class="comment">// error_code* point to an error_code local to this object so</span> <span class="comment">// yield_handler_base::operator() can unconditionally store through</span> <span class="comment">// its error_code*</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">h</span><span class="special">.</span><span class="identifier">yt_</span><span class="special">.</span><span class="identifier">ec_</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">h</span><span class="special">.</span><span class="identifier">yt_</span><span class="special">.</span><span class="identifier">ec_</span> <span class="special">=</span> <span class="special">&amp;</span> <span class="identifier">ec_</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">void</span> <span class="identifier">get</span><span class="special">()</span> <span class="special">{</span> <span class="comment">// Unless yield_handler_base::operator() has already been called,</span> <span class="comment">// suspend the calling fiber until that call.</span> <span class="identifier">ycomp_</span><span class="special">-&gt;</span><span class="identifier">wait</span><span class="special">();</span> <span class="comment">// The only way our own ec_ member could have a non-default value is</span> <span class="comment">// if our yield_handler did not have a bound error_code AND the</span> <span class="comment">// completion callback passed a non-default error_code.</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">ec_</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">throw_exception</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">system_error</span><span class="special">{</span> <span class="identifier">ec_</span> <span class="special">}</span> <span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">private</span><span class="special">:</span> <span class="comment">// If yield_t does not bind an error_code instance, store into here.</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec_</span><span class="special">{};</span> <span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">ptr_t</span> <span class="identifier">ycomp_</span><span class="special">;</span> <span class="special">};</span> </pre> <p> </p> <p> This is how <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="identifier">ycomp_</span></code> becomes non-null: <code class="computeroutput"><span class="identifier">async_result_base</span></code>&#8217;s constructor injects a pointer back to its own <code class="computeroutput"><span class="identifier">yield_completion</span></code> member. </p> <p> Recall that the canonical <code class="computeroutput"><span class="identifier">yield_t</span></code> instance <code class="computeroutput"><span class="identifier">yield</span></code> initializes its <code class="computeroutput"><span class="identifier">error_code</span><span class="special">*</span></code> member <code class="computeroutput"><span class="identifier">ec_</span></code> to <code class="computeroutput"><span class="keyword">nullptr</span></code>. If this instance is passed to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> (<code class="computeroutput"><span class="identifier">ec_</span></code> is still <code class="computeroutput"><span class="keyword">nullptr</span></code>), the copy stored in <code class="computeroutput"><span class="identifier">yield_handler_base</span></code> will likewise have null <code class="computeroutput"><span class="identifier">ec_</span></code>. <code class="computeroutput"><span class="identifier">async_result_base</span></code>&#8217;s constructor sets <code class="computeroutput"><span class="identifier">yield_handler_base</span></code>&#8217;s <code class="computeroutput"><span class="identifier">yield_t</span></code>&#8217;s <code class="computeroutput"><span class="identifier">ec_</span></code> member to point to its own <code class="computeroutput"><span class="identifier">error_code</span></code> member. </p> <p> The stage is now set. <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> initiates the actual async operation, arranging to call its <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> instance on completion. Let&#8217;s say, for the sake of argument, that the actual async operation&#8217;s callback has signature <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">)</span></code>. </p> <p> But since it&#8217;s an async operation, control returns at once to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>. <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> calls <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;&gt;::</span><span class="identifier">get</span><span class="special">()</span></code>, and will return its return value. </p> <p> <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;&gt;::</span><span class="identifier">get</span><span class="special">()</span></code> inherits <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code>. </p> <p> <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code> immediately calls <code class="computeroutput"><span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>. </p> <p> </p> <pre class="programlisting"><span class="comment">// Bundle a completion bool flag with a spinlock to protect it.</span> <span class="keyword">struct</span> <span class="identifier">yield_completion</span> <span class="special">{</span> <span class="keyword">enum</span> <span class="identifier">state_t</span> <span class="special">{</span> <span class="identifier">init</span><span class="special">,</span> <span class="identifier">waiting</span><span class="special">,</span> <span class="identifier">complete</span> <span class="special">};</span> <span class="keyword">typedef</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">spinlock</span> <span class="identifier">mutex_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_lock</span><span class="special">&lt;</span> <span class="identifier">mutex_t</span> <span class="special">&gt;</span> <span class="identifier">lock_t</span><span class="special">;</span> <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive_ptr</span><span class="special">&lt;</span> <span class="identifier">yield_completion</span> <span class="special">&gt;</span> <span class="identifier">ptr_t</span><span class="special">;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="special">&gt;</span> <span class="identifier">use_count_</span><span class="special">{</span> <span class="number">0</span> <span class="special">};</span> <span class="identifier">mutex_t</span> <span class="identifier">mtx_</span><span class="special">{};</span> <span class="identifier">state_t</span> <span class="identifier">state_</span><span class="special">{</span> <span class="identifier">init</span> <span class="special">};</span> <span class="keyword">void</span> <span class="identifier">wait</span><span class="special">()</span> <span class="special">{</span> <span class="comment">// yield_handler_base::operator()() will set state_ `complete` and</span> <span class="comment">// attempt to wake a suspended fiber. It would be Bad if that call</span> <span class="comment">// happened between our detecting (complete != state_) and suspending.</span> <span class="identifier">lock_t</span> <span class="identifier">lk</span><span class="special">{</span> <span class="identifier">mtx_</span> <span class="special">};</span> <span class="comment">// If state_ is already set, we're done here: don't suspend.</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">complete</span> <span class="special">!=</span> <span class="identifier">state_</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">state_</span> <span class="special">=</span> <span class="identifier">waiting</span><span class="special">;</span> <span class="comment">// suspend(unique_lock&lt;spinlock&gt;) unlocks the lock in the act of</span> <span class="comment">// resuming another fiber</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">active</span><span class="special">()-&gt;</span><span class="identifier">suspend</span><span class="special">(</span> <span class="identifier">lk</span><span class="special">);</span> <span class="special">}</span> <span class="special">}</span> <span class="keyword">friend</span> <span class="keyword">void</span> <span class="identifier">intrusive_ptr_add_ref</span><span class="special">(</span> <span class="identifier">yield_completion</span> <span class="special">*</span> <span class="identifier">yc</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="keyword">nullptr</span> <span class="special">!=</span> <span class="identifier">yc</span><span class="special">);</span> <span class="identifier">yc</span><span class="special">-&gt;</span><span class="identifier">use_count_</span><span class="special">.</span><span class="identifier">fetch_add</span><span class="special">(</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">memory_order_relaxed</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">friend</span> <span class="keyword">void</span> <span class="identifier">intrusive_ptr_release</span><span class="special">(</span> <span class="identifier">yield_completion</span> <span class="special">*</span> <span class="identifier">yc</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="identifier">BOOST_ASSERT</span><span class="special">(</span> <span class="keyword">nullptr</span> <span class="special">!=</span> <span class="identifier">yc</span><span class="special">);</span> <span class="keyword">if</span> <span class="special">(</span> <span class="number">1</span> <span class="special">==</span> <span class="identifier">yc</span><span class="special">-&gt;</span><span class="identifier">use_count_</span><span class="special">.</span><span class="identifier">fetch_sub</span><span class="special">(</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">memory_order_release</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic_thread_fence</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">memory_order_acquire</span><span class="special">);</span> <span class="keyword">delete</span> <span class="identifier">yc</span><span class="special">;</span> <span class="special">}</span> <span class="special">}</span> <span class="special">};</span> </pre> <p> </p> <p> Supposing that the pending async operation has not yet completed, <code class="computeroutput"><span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">completed_</span></code> will still be <code class="computeroutput"><span class="keyword">false</span></code>, and <code class="computeroutput"><span class="identifier">wait</span><span class="special">()</span></code> will call <a class="link" href="../scheduling.html#context_suspend"><code class="computeroutput">context::suspend()</code></a> on the currently-running fiber. </p> <p> Other fibers will now have a chance to run. </p> <p> Some time later, the async operation completes. It calls <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;::</span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;)</span></code> with an <code class="computeroutput"><span class="identifier">error_code</span></code> indicating either success or failure. We&#8217;ll consider both cases. </p> <p> <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code> explicitly inherits <code class="computeroutput"><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;)</span></code> from <code class="computeroutput"><span class="identifier">yield_handler_base</span></code>. </p> <p> <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;)</span></code> first sets <code class="computeroutput"><span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">completed_</span></code> <code class="computeroutput"><span class="keyword">true</span></code>. This way, if <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>&#8217;s async operation completes immediately &#8212; if <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> is called even before <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code> &#8212; the calling fiber will <span class="emphasis"><em>not</em></span> suspend. </p> <p> The actual <code class="computeroutput"><span class="identifier">error_code</span></code> produced by the async operation is then stored through the stored <code class="computeroutput"><span class="identifier">yield_t</span><span class="special">::</span><span class="identifier">ec_</span></code> pointer. If <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>&#8217;s caller used (e.g.) <code class="computeroutput"><span class="identifier">yield</span><span class="special">[</span><span class="identifier">my_ec</span><span class="special">]</span></code> to bind a local <code class="computeroutput"><span class="identifier">error_code</span></code> instance, the actual <code class="computeroutput"><span class="identifier">error_code</span></code> value is stored into the caller&#8217;s variable. Otherwise, it is stored into <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">ec_</span></code>. </p> <p> If the stored fiber context <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="identifier">ctx_</span></code> is not already running, it is marked as ready to run by passing it to <a class="link" href="../scheduling.html#context_schedule"><code class="computeroutput">context::schedule()</code></a>. Control then returns from <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code>: the callback is done. </p> <p> In due course, that fiber is resumed. Control returns from <a class="link" href="../scheduling.html#context_suspend"><code class="computeroutput">context::suspend()</code></a> to <code class="computeroutput"><span class="identifier">yield_completion</span><span class="special">::</span><span class="identifier">wait</span><span class="special">()</span></code>, which returns to <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code>. </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem"> If the original caller passed <code class="computeroutput"><span class="identifier">yield</span><span class="special">[</span><span class="identifier">my_ec</span><span class="special">]</span></code> to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> to bind a local <code class="computeroutput"><span class="identifier">error_code</span></code> instance, then <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> stored its <code class="computeroutput"><span class="identifier">error_code</span></code> to the caller&#8217;s <code class="computeroutput"><span class="identifier">my_ec</span></code> instance, leaving <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">ec_</span></code> initialized to success. </li> <li class="listitem"> If the original caller passed <code class="computeroutput"><span class="identifier">yield</span></code> to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> without binding a local <code class="computeroutput"><span class="identifier">error_code</span></code> variable, then <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> stored its <code class="computeroutput"><span class="identifier">error_code</span></code> into <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">ec_</span></code>. If in fact that <code class="computeroutput"><span class="identifier">error_code</span></code> is success, then all is well. </li> <li class="listitem"> Otherwise &#8212; the original caller did not bind a local <code class="computeroutput"><span class="identifier">error_code</span></code> and <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()</span></code> was called with an <code class="computeroutput"><span class="identifier">error_code</span></code> indicating error &#8212; <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code> throws <code class="computeroutput"><span class="identifier">system_error</span></code> with that <code class="computeroutput"><span class="identifier">error_code</span></code>. </li> </ul></div> <p> The case in which <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>&#8217;s completion callback has signature <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code> is similar. <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;::</span><span class="keyword">operator</span><span class="special">()()</span></code> invokes the machinery above with a <span class="quote">&#8220;<span class="quote">success</span>&#8221;</span> <code class="computeroutput"><span class="identifier">error_code</span></code>. </p> <p> A completion callback with signature <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">T</span><span class="special">)</span></code> (that is: in addition to <code class="computeroutput"><span class="identifier">error_code</span></code>, callback receives some data item) is handled somewhat differently. For this kind of signature, <code class="computeroutput"><span class="identifier">handler_type</span><span class="special">&lt;&gt;::</span><span class="identifier">type</span></code> specifies <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> (for <code class="computeroutput"><span class="identifier">T</span></code> other than <code class="computeroutput"><span class="keyword">void</span></code>). </p> <p> A <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> reserves a <code class="computeroutput"><span class="identifier">value_</span></code> pointer to a value of type <code class="computeroutput"><span class="identifier">T</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// asio uses handler_type&lt;completion token type, signature&gt;::type to decide</span> <span class="comment">// what to instantiate as the actual handler. Below, we specialize</span> <span class="comment">// handler_type&lt; yield_t, ... &gt; to indicate yield_handler&lt;&gt;. So when you pass</span> <span class="comment">// an instance of yield_t as an asio completion token, asio selects</span> <span class="comment">// yield_handler&lt;&gt; as the actual handler class.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">yield_handler</span><span class="special">:</span> <span class="keyword">public</span> <span class="identifier">yield_handler_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="comment">// asio passes the completion token to the handler constructor</span> <span class="keyword">explicit</span> <span class="identifier">yield_handler</span><span class="special">(</span> <span class="identifier">yield_t</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">yield_handler_base</span><span class="special">{</span> <span class="identifier">y</span> <span class="special">}</span> <span class="special">{</span> <span class="special">}</span> <span class="comment">// completion callback passing only value (T)</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">T</span> <span class="identifier">t</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// just like callback passing success error_code</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">t</span><span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="comment">// completion callback passing (error_code, T)</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">t</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">BOOST_ASSERT_MSG</span><span class="special">(</span> <span class="identifier">value_</span><span class="special">,</span> <span class="string">"Must inject value ptr "</span> <span class="string">"before caling yield_handler&lt;T&gt;::operator()()"</span><span class="special">);</span> <span class="comment">// move the value to async_result&lt;&gt; instance BEFORE waking up a</span> <span class="comment">// suspended fiber</span> <span class="special">*</span> <span class="identifier">value_</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">t</span><span class="special">);</span> <span class="comment">// forward the call to base-class completion handler</span> <span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()(</span> <span class="identifier">ec</span><span class="special">);</span> <span class="special">}</span> <span class="comment">//private:</span> <span class="comment">// pointer to destination for eventual value</span> <span class="comment">// this must be injected by async_result before operator()() is called</span> <span class="identifier">T</span> <span class="special">*</span> <span class="identifier">value_</span><span class="special">{</span> <span class="keyword">nullptr</span> <span class="special">};</span> <span class="special">};</span> </pre> <p> </p> <p> This pointer is initialized to <code class="computeroutput"><span class="keyword">nullptr</span></code>. </p> <p> When <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> instantiates <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;</span></code>: </p> <p> </p> <pre class="programlisting"><span class="comment">// asio constructs an async_result&lt;&gt; instance from the yield_handler specified</span> <span class="comment">// by handler_type&lt;&gt;::type. A particular asio async method constructs the</span> <span class="comment">// yield_handler, constructs this async_result specialization from it, then</span> <span class="comment">// returns the result of calling its get() method.</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">async_result</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">async_result_base</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="comment">// type returned by get()</span> <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">type</span><span class="special">;</span> <span class="keyword">explicit</span> <span class="identifier">async_result</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">yield_handler</span><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">h</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">async_result_base</span><span class="special">{</span> <span class="identifier">h</span> <span class="special">}</span> <span class="special">{</span> <span class="comment">// Inject ptr to our value_ member into yield_handler&lt;&gt;: result will</span> <span class="comment">// be stored here.</span> <span class="identifier">h</span><span class="special">.</span><span class="identifier">value_</span> <span class="special">=</span> <span class="special">&amp;</span> <span class="identifier">value_</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// asio async method returns result of calling get()</span> <span class="identifier">type</span> <span class="identifier">get</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">();</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">value_</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">private</span><span class="special">:</span> <span class="identifier">type</span> <span class="identifier">value_</span><span class="special">{};</span> <span class="special">};</span> </pre> <p> </p> <p> this <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;&gt;</span></code> specialization reserves a member of type <code class="computeroutput"><span class="identifier">T</span></code> to receive the passed data item, and sets <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value_</span></code> to point to its own data member. </p> <p> <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;</span></code> overrides <code class="computeroutput"><span class="identifier">get</span><span class="special">()</span></code>. The override calls <code class="computeroutput"><span class="identifier">async_result_base</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code>, so the calling fiber suspends as described above. </p> <p> <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">T</span><span class="special">)</span></code> stores its passed <code class="computeroutput"><span class="identifier">T</span></code> value into <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;::</span><span class="identifier">value_</span></code>. </p> <p> Then it passes control to <code class="computeroutput"><span class="identifier">yield_handler_base</span><span class="special">::</span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">error_code</span><span class="special">)</span></code> to deal with waking the original fiber as described above. </p> <p> When <code class="computeroutput"><span class="identifier">async_result</span><span class="special">&lt;</span><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&gt;::</span><span class="identifier">get</span><span class="special">()</span></code> resumes, it returns the stored <code class="computeroutput"><span class="identifier">value_</span></code> to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code> and ultimately to <code class="computeroutput"><span class="identifier">async_something</span><span class="special">()</span></code>&#8217;s caller. </p> <p> The case of a callback signature <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span></code> is handled by having <code class="computeroutput"><span class="identifier">yield_handler</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T</span><span class="special">)</span></code> engage the <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">T</span><span class="special">)</span></code> machinery, passing a <span class="quote">&#8220;<span class="quote">success</span>&#8221;</span> <code class="computeroutput"><span class="identifier">error_code</span></code>. </p> <p> The source code above is found in <a href="../../../../examples/asio/yield.hpp" target="_top">yield.hpp</a> and <a href="../../../../examples/asio/detail/yield.hpp" target="_top">detail/yield.hpp</a>. </p> <div class="footnotes"> <br><hr width="100" align="left"> <div class="footnote"><p><sup>[<a name="ftn.fiber.callbacks.then_there_s____boost_asio__.f0" href="#fiber.callbacks.then_there_s____boost_asio__.f0" class="para">5</a>] </sup> This mechanism has been proposed as a conventional way to allow the caller of an arbitrary async function to specify completion handling: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4045.pdf" target="_top">N4045</a>. </p></div> <div class="footnote"><p><sup>[<a name="ftn.fiber.callbacks.then_there_s____boost_asio__.f1" href="#fiber.callbacks.then_there_s____boost_asio__.f1" class="para">6</a>] </sup> per <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4045.pdf" target="_top">N4045</a> </p></div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="success_error_virtual_methods.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../nonblocking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/data_or_exception.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Data or Exception</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="return_errorcode_or_data.html" title="Return Errorcode or Data"> <link rel="next" href="success_error_virtual_methods.html" title="Success/Error Virtual Methods"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="return_errorcode_or_data.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="success_error_virtual_methods.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.data_or_exception"></a><a name="Data_or_Exception"></a><a class="link" href="data_or_exception.html" title="Data or Exception">Data or Exception</a> </h3></div></div></div> <p> But a more natural API for a function that obtains data is to return only the data on success, throwing an exception on error. </p> <p> As with <code class="computeroutput"><span class="identifier">write</span><span class="special">()</span></code> above, it&#8217;s certainly possible to code a <code class="computeroutput"><span class="identifier">read</span><span class="special">()</span></code> wrapper in terms of <code class="computeroutput"><span class="identifier">read_ec</span><span class="special">()</span></code>. But since a given application is unlikely to need both, let&#8217;s code <code class="computeroutput"><span class="identifier">read</span><span class="special">()</span></code> from scratch, leveraging <a class="link" href="../synchronization/futures/promise.html#promise_set_exception"><code class="computeroutput">promise::set_exception()</code></a>: </p> <p> </p> <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">read</span><span class="special">(</span> <span class="identifier">AsyncAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">promise</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="identifier">future</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">get_future</span><span class="special">()</span> <span class="special">);</span> <span class="comment">// Both 'promise' and 'future' will survive until our lambda has been</span> <span class="comment">// called.</span> <span class="preprocessor">#if</span> <span class="special">!</span> <span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span><span class="special">)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_read</span><span class="special">([&amp;</span><span class="identifier">promise</span><span class="special">](</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">data</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_exception</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_exception_ptr</span><span class="special">(</span> <span class="identifier">make_exception</span><span class="special">(</span><span class="string">"read"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="special">});</span> <span class="preprocessor">#else</span> <span class="comment">// defined(BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES)</span> <span class="identifier">api</span><span class="special">.</span><span class="identifier">init_read</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind</span><span class="special">([](</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">promise</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">promise</span><span class="special">,</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="keyword">mutable</span> <span class="special">{</span> <span class="keyword">if</span> <span class="special">(</span> <span class="special">!</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_value</span><span class="special">(</span> <span class="identifier">data</span><span class="special">);</span> <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span> <span class="identifier">promise</span><span class="special">.</span><span class="identifier">set_exception</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_exception_ptr</span><span class="special">(</span> <span class="identifier">make_exception</span><span class="special">(</span><span class="string">"read"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">)</span> <span class="special">);</span> <span class="special">}</span> <span class="special">},</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span> <span class="identifier">promise</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">_2</span><span class="special">)</span> <span class="special">);</span> <span class="preprocessor">#endif</span> <span class="comment">// BOOST_NO_CXX14_INITIALIZED_LAMBDA_CAPTURES</span> <span class="keyword">return</span> <span class="identifier">future</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span> <span class="special">}</span> </pre> <p> </p> <p> <a class="link" href="../synchronization/futures/future.html#future_get"><code class="computeroutput">future::get()</code></a> will do the right thing, either returning <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> or throwing an exception. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="return_errorcode_or_data.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="success_error_virtual_methods.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/callbacks/success_or_exception.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Success or Exception</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../callbacks.html" title="Integrating Fibers with Asynchronous Callbacks"> <link rel="prev" href="return_errorcode.html" title="Return Errorcode"> <link rel="next" href="return_errorcode_or_data.html" title="Return Errorcode or Data"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="return_errorcode.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="return_errorcode_or_data.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.callbacks.success_or_exception"></a><a class="link" href="success_or_exception.html" title="Success or Exception">Success or Exception</a> </h3></div></div></div> <p> A wrapper more aligned with modern C++ practice would use an exception, rather than an <code class="computeroutput"><span class="identifier">errorcode</span></code>, to communicate failure to its caller. This is straightforward to code in terms of <code class="computeroutput"><span class="identifier">write_ec</span><span class="special">()</span></code>: </p> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">write</span><span class="special">(</span> <span class="identifier">AsyncAPI</span> <span class="special">&amp;</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">AsyncAPI</span><span class="special">::</span><span class="identifier">errorcode</span> <span class="identifier">ec</span> <span class="special">=</span> <span class="identifier">write_ec</span><span class="special">(</span> <span class="identifier">api</span><span class="special">,</span> <span class="identifier">data</span><span class="special">);</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">ec</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">throw</span> <span class="identifier">make_exception</span><span class="special">(</span><span class="string">"write"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> <span class="special">}</span> <span class="special">}</span> </pre> <p> </p> <p> The point is that since each fiber has its own stack, you need not repeat messy boilerplate: normal encapsulation works. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="return_errorcode.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../callbacks.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="return_errorcode_or_data.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/fiber_mgmt/this_fiber.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Namespace this_fiber</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../fiber_mgmt.html" title="Fiber management"> <link rel="prev" href="id.html" title="Class fiber::id"> <link rel="next" href="../scheduling.html" title="Scheduling"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="id.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../scheduling.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.fiber_mgmt.this_fiber"></a><a class="link" href="this_fiber.html" title="Namespace this_fiber">Namespace this_fiber</a> </h3></div></div></div> <p> In general, <code class="computeroutput"><span class="identifier">this_fiber</span></code> operations may be called from the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber &#8212; the fiber on which function <code class="computeroutput"><span class="identifier">main</span><span class="special">()</span></code> is entered &#8212; as well as from an explicitly-launched thread&#8217;s thread-function. That is, in many respects the main fiber on each thread can be treated like an explicitly-launched fiber. </p> <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">this_fiber</span> <span class="special">{</span> <span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">yield</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">();</span> <span class="special">}}</span> </pre> <p> </p> <h5> <a name="this_fiber_get_id_bridgehead"></a> <span><a name="this_fiber_get_id"></a></span> <a class="link" href="this_fiber.html#this_fiber_get_id">Non-member function <code class="computeroutput">this_fiber::get_id()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">operations</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> An instance of <a class="link" href="../fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> that represents the currently executing fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="this_fiber_sleep_until_bridgehead"></a> <span><a name="this_fiber_sleep_until"></a></span> <a class="link" href="this_fiber.html#this_fiber_sleep_until">Non-member function <code class="computeroutput">this_fiber::sleep_until()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">operations</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_until</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">time_point</span><span class="special">&lt;</span> <span class="identifier">Clock</span><span class="special">,</span> <span class="identifier">Duration</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abs_time</span><span class="special">);</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Suspends the current fiber until the time point specified by <code class="computeroutput"><span class="identifier">abs_time</span></code> has been reached. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The current fiber will not resume before <code class="computeroutput"><span class="identifier">abs_time</span></code>, but there are no guarantees about how soon after <code class="computeroutput"><span class="identifier">abs_time</span></code> it might resume. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <span class="quote">&#8220;<span class="quote">timeout-related exceptions</span>&#8221;</span> are as defined in the C++ Standard, section <span class="bold"><strong>30.2.4 Timing specifications [thread.req.timing]</strong></span>: <span class="quote">&#8220;<span class="quote">A function that takes an argument which specifies a timeout will throw if, during its execution, a clock, time point, or time duration throws an exception. Such exceptions are referred to as <span class="emphasis"><em>timeout-related exceptions.</em></span></span>&#8221;</span> </p></dd> </dl> </div> <p> </p> <h5> <a name="this_fiber_sleep_for_bridgehead"></a> <span><a name="this_fiber_sleep_for"></a></span> <a class="link" href="this_fiber.html#this_fiber_sleep_for">Non-member function <code class="computeroutput">this_fiber::sleep_for()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">operations</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">sleep_for</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special">&lt;</span> <span class="identifier">Rep</span><span class="special">,</span> <span class="identifier">Period</span> <span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rel_time</span><span class="special">);</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Suspends the current fiber until the time duration specified by <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> timeout-related exceptions. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The current fiber will not resume before <code class="computeroutput"><span class="identifier">rel_time</span></code> has elapsed, but there are no guarantees about how soon after that it might resume. </p></dd> </dl> </div> <p> </p> <h5> <a name="this_fiber_yield_bridgehead"></a> <span><a name="this_fiber_yield"></a></span> <a class="link" href="this_fiber.html#this_fiber_yield">Non-member function <code class="computeroutput">this_fiber::yield()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">operations</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">yield</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Relinquishes execution control, allowing other fibers to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> A fiber that calls <code class="computeroutput"><span class="identifier">yield</span><span class="special">()</span></code> is not suspended: it is immediately passed to the scheduler as ready to run. </p></dd> </dl> </div> <p> </p> <h5> <a name="this_fiber_properties_bridgehead"></a> <span><a name="this_fiber_properties"></a></span> <a class="link" href="this_fiber.html#this_fiber_properties">Non-member function <code class="computeroutput">this_fiber::properties()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">operations</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">();</span> <span class="special">}}</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <a class="link" href="fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a> has been called from this thread with a subclass of <a class="link" href="../scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> with the same template argument <code class="computeroutput"><span class="identifier">PROPS</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> a reference to the scheduler properties instance for the currently running fiber. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_cast</span></code> if <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code> was called with an <code class="computeroutput"><span class="identifier">algorithm_with_properties</span></code> subclass with some other template parameter than <code class="computeroutput"><span class="identifier">PROPS</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <a class="link" href="../scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> provides a way for a user-coded scheduler to associate extended properties, such as priority, with a fiber instance. This function allows access to those user-provided properties. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The first time this function is called from the main fiber of a thread, it may internally yield, permitting other fibers to run. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="../custom.html#custom">Customization</a> </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="id.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../scheduling.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/fiber_mgmt/fiber.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Class fiber</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../fiber_mgmt.html" title="Fiber management"> <link rel="prev" href="../fiber_mgmt.html" title="Fiber management"> <link rel="next" href="id.html" title="Class fiber::id"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="id.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.fiber_mgmt.fiber"></a><a name="class_fiber"></a><a class="link" href="fiber.html" title="Class fiber">Class <code class="computeroutput"><span class="identifier">fiber</span></code></a> </h3></div></div></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">fiber</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">class</span> <span class="identifier">id</span><span class="special">;</span> <span class="keyword">constexpr</span> <span class="identifier">fiber</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">StackAllocator</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a><span class="special">,</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">StackAllocator</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...);</span> <span class="special">~</span><span class="identifier">fiber</span><span class="special">();</span> <span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="special">=</span> <span class="keyword">delete</span><span class="special">;</span> <span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">fiber</span> <span class="special">&amp;&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">joinable</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">detach</span><span class="special">();</span> <span class="keyword">void</span> <span class="identifier">join</span><span class="special">();</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">();</span> <span class="special">};</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;,</span> <span class="identifier">fiber</span> <span class="special">&amp;)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">SchedAlgo</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">use_scheduling_algorithm</span><span class="special">(</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...)</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="special">}}</span> </pre> <h5> <a name="fiber.fiber_mgmt.fiber.h0"></a> <span><a name="fiber.fiber_mgmt.fiber.default_constructor"></a></span><a class="link" href="fiber.html#fiber.fiber_mgmt.fiber.default_constructor">Default constructor</a> </h5> <pre class="programlisting"><span class="keyword">constexpr</span> <span class="identifier">fiber</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Constructs a <a class="link" href="fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> instance that refers to <span class="emphasis"><em>not-a-fiber</em></span>. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span><span class="special">()</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <a name="fiber_fiber"></a><h5> <a name="fiber.fiber_mgmt.fiber.h1"></a> <span><a name="fiber.fiber_mgmt.fiber.constructor"></a></span><a class="link" href="fiber.html#fiber.fiber_mgmt.fiber.constructor">Constructor</a> </h5> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a> <span class="identifier">policy</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">StackAllocator</span> <span class="special">&amp;&amp;</span> <span class="identifier">salloc</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <a class="link" href="../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="identifier">fiber</span><span class="special">(</span> <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput"><span class="identifier">launch</span></code></a> <span class="identifier">policy</span><span class="special">,</span> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a><span class="special">,</span> <span class="identifier">StackAllocator</span> <span class="special">&amp;&amp;</span> <span class="identifier">salloc</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="special">&amp;&amp;</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">Fn</span></code> must be copyable or movable. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fn</span></code> is copied or moved into internal storage for access by the new fiber. If <a class="link" href="../fiber_mgmt.html#class_launch"><code class="computeroutput">launch</code></a> is specified (or defaulted) to <code class="computeroutput"><span class="identifier">post</span></code>, the new fiber is marked <span class="quote">&#8220;<span class="quote">ready</span>&#8221;</span> and will be entered at the next opportunity. If <code class="computeroutput"><span class="identifier">launch</span></code> is specified as <code class="computeroutput"><span class="identifier">dispatch</span></code>, the calling fiber is suspended and the new fiber is entered immediately. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> refers to the newly created fiber of execution. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> if an error occurs. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <a class="link" href="../stack.html#stack_allocator_concept"><code class="computeroutput"><span class="identifier">StackAllocator</span></code></a> is required to allocate a stack for the internal __econtext__. If <code class="computeroutput"><span class="identifier">StackAllocator</span></code> is not explicitly passed, the default stack allocator depends on <code class="computeroutput"><span class="identifier">BOOST_USE_SEGMENTED_STACKS</span></code>: if defined, you will get a <a class="link" href="../stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a>, else a <a class="link" href="../stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a>. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a href="http://en.cppreference.com/w/cpp/memory/allocator_arg_t" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_arg_t</span></code></a>, <a class="link" href="../stack.html#stack">Stack allocation</a> </p></dd> </dl> </div> <h5> <a name="fiber.fiber_mgmt.fiber.h2"></a> <span><a name="fiber.fiber_mgmt.fiber.move_constructor"></a></span><a class="link" href="fiber.html#fiber.fiber_mgmt.fiber.move_constructor">Move constructor</a> </h5> <pre class="programlisting"><span class="identifier">fiber</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Transfers ownership of the fiber managed by <code class="computeroutput"><span class="identifier">other</span></code> to the newly constructed <a class="link" href="fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> instance. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">get_id</span><span class="special">()</span></code> returns the value of <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> prior to the construction </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <h5> <a name="fiber.fiber_mgmt.fiber.h3"></a> <span><a name="fiber.fiber_mgmt.fiber.move_assignment_operator"></a></span><a class="link" href="fiber.html#fiber.fiber_mgmt.fiber.move_assignment_operator">Move assignment operator</a> </h5> <pre class="programlisting"><span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">fiber</span> <span class="special">&amp;&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Transfers ownership of the fiber managed by <code class="computeroutput"><span class="identifier">other</span></code> (if any) to <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span><span class="special">-&gt;</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">get_id</span><span class="special">()</span></code> returns the value of <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> prior to the assignment. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <h5> <a name="fiber.fiber_mgmt.fiber.h4"></a> <span><a name="fiber.fiber_mgmt.fiber.destructor"></a></span><a class="link" href="fiber.html#fiber.fiber_mgmt.fiber.destructor">Destructor</a> </h5> <pre class="programlisting"><span class="special">~</span><span class="identifier">fiber</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> If the fiber is <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>, calls std::terminate. Destroys <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> The programmer must ensure that the destructor is never executed while the fiber is still <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>. Even if you know that the fiber has completed, you must still call either <a class="link" href="fiber.html#fiber_join"><code class="computeroutput">fiber::join()</code></a> or <a class="link" href="fiber.html#fiber_detach"><code class="computeroutput">fiber::detach()</code></a> before destroying the <code class="computeroutput"><span class="identifier">fiber</span></code> object. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_joinable_bridgehead"></a> <span><a name="fiber_joinable"></a></span> <a class="link" href="fiber.html#fiber_joinable">Member function <code class="computeroutput">joinable</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">joinable</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> refers to a fiber of execution, which may or may not have completed; otherwise <code class="computeroutput"><span class="keyword">false</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_join_bridgehead"></a> <span><a name="fiber_join"></a></span> <a class="link" href="fiber.html#fiber_join">Member function <code class="computeroutput">join</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">join</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> the fiber is <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> Waits for the referenced fiber of execution to complete. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> The fiber of execution referenced on entry has completed. <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> no longer refers to any fiber of execution. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>resource_deadlock_would_occur</strong></span>: if <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">this_fiber</span><span class="special">::</span><span class="identifier">get_id</span><span class="special">()</span></code>. <span class="bold"><strong>invalid_argument</strong></span>: if the fiber is not <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_detach_bridgehead"></a> <span><a name="fiber_detach"></a></span> <a class="link" href="fiber.html#fiber_detach">Member function <code class="computeroutput">detach</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">detach</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> the fiber is <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>. </p></dd> <dt><span class="term">Effects:</span></dt> <dd><p> The fiber of execution becomes detached, and no longer has an associated <a class="link" href="fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> object. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> no longer refers to any fiber of execution. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">fiber_error</span></code> </p></dd> <dt><span class="term">Error Conditions:</span></dt> <dd><p> <span class="bold"><strong>invalid_argument</strong></span>: if the fiber is not <a class="link" href="fiber.html#fiber_joinable"><code class="computeroutput">fiber::joinable()</code></a>. </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_get_id_bridgehead"></a> <span><a name="fiber_get_id"></a></span> <a class="link" href="fiber.html#fiber_get_id">Member function <code class="computeroutput">get_id</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span> <span class="identifier">get_id</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> If <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> refers to a fiber of execution, an instance of <a class="link" href="../fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a> that represents that fiber. Otherwise returns a default-constructed <a class="link" href="../fiber_mgmt.html#class_fiber_id"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code></a>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="this_fiber.html#this_fiber_get_id"><code class="computeroutput">this_fiber::get_id()</code></a> </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_properties_bridgehead"></a> <span><a name="fiber_properties"></a></span> <a class="link" href="fiber.html#fiber_properties">Templated member function <code class="computeroutput">properties</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">PROPS</span> <span class="special">&gt;</span> <span class="identifier">PROPS</span> <span class="special">&amp;</span> <span class="identifier">properties</span><span class="special">();</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Preconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> refers to a fiber of execution. <a class="link" href="fiber.html#use_scheduling_algorithm"><code class="computeroutput">use_scheduling_algorithm()</code></a> has been called from this thread with a subclass of <a class="link" href="../scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> with the same template argument <code class="computeroutput"><span class="identifier">PROPS</span></code>. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> a reference to the scheduler properties instance for <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_cast</span></code> if <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code> was called with a <code class="computeroutput"><span class="identifier">algorithm_with_properties</span></code> subclass with some other template parameter than <code class="computeroutput"><span class="identifier">PROPS</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> <a class="link" href="../scheduling.html#class_algorithm_with_properties"><code class="computeroutput">algorithm_with_properties&lt;&gt;</code></a> provides a way for a user-coded scheduler to associate extended properties, such as priority, with a fiber instance. This method allows access to those user-provided properties. </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="../custom.html#custom">Customization</a> </p></dd> </dl> </div> <p> </p> <h5> <a name="fiber_swap_bridgehead"></a> <span><a name="fiber_swap"></a></span> <a class="link" href="fiber.html#fiber_swap">Member function <code class="computeroutput">swap</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Exchanges the fiber of execution associated with <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> and <code class="computeroutput"><span class="identifier">other</span></code>, so <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> becomes associated with the fiber formerly associated with <code class="computeroutput"><span class="identifier">other</span></code>, and vice-versa. </p></dd> <dt><span class="term">Postconditions:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_id</span><span class="special">()</span></code> returns the same value as <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> prior to the call. <code class="computeroutput"><span class="identifier">other</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> returns the same value as <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get_id</span><span class="special">()</span></code> prior to the call. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="swap_for_fiber_bridgehead"></a> <span><a name="swap_for_fiber"></a></span> <a class="link" href="fiber.html#swap_for_fiber">Non-member function <code class="computeroutput">swap()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">fiber</span> <span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Same as <code class="computeroutput"><span class="identifier">l</span><span class="special">.</span><span class="identifier">swap</span><span class="special">(</span> <span class="identifier">r</span><span class="special">)</span></code>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> </dl> </div> <p> </p> <h5> <a name="operator&lt;_bridgehead"></a> <span><a name="operator&lt;"></a></span> <a class="link" href="fiber.html#operator&lt;">Non-member function <code class="computeroutput">operator&lt;()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">fiber</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="identifier">l</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">get_id</span><span class="special">()</span></code> is <code class="computeroutput"><span class="keyword">true</span></code>, false otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="use_scheduling_algorithm_bridgehead"></a> <span><a name="use_scheduling_algorithm"></a></span> <a class="link" href="fiber.html#use_scheduling_algorithm">Non-member function <code class="computeroutput">use_scheduling_algorithm()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">SchedAlgo</span><span class="special">,</span> <span class="keyword">typename</span> <span class="special">...</span> <span class="identifier">Args</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">use_scheduling_algorithm</span><span class="special">(</span> <span class="identifier">Args</span> <span class="special">&amp;&amp;</span> <span class="special">...</span> <span class="identifier">args</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Directs <span class="bold"><strong>Boost.Fiber</strong></span> to use <code class="computeroutput"><span class="identifier">SchedAlgo</span></code>, which must be a concrete subclass of <a class="link" href="../scheduling.html#class_algorithm"><code class="computeroutput">algorithm</code></a>, as the scheduling algorithm for all fibers in the current thread. Pass any required <code class="computeroutput"><span class="identifier">SchedAlgo</span></code> constructor arguments as <code class="computeroutput"><span class="identifier">args</span></code>. </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> If you want a given thread to use a non-default scheduling algorithm, make that thread call <code class="computeroutput"><span class="identifier">use_scheduling_algorithm</span><span class="special">()</span></code> before any other <span class="bold"><strong>Boost.Fiber</strong></span> entry point. If no scheduler has been set for the current thread by the time <span class="bold"><strong>Boost.Fiber</strong></span> needs to use it, the library will create a default <a class="link" href="../scheduling.html#class_round_robin"><code class="computeroutput">round_robin</code></a> instance for this thread. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">See also:</span></dt> <dd><p> <a class="link" href="../scheduling.html#scheduling">Scheduling</a>, <a class="link" href="../custom.html#custom">Customization</a> </p></dd> </dl> </div> <p> </p> <h5> <a name="has_ready_fibers_bridgehead"></a> <span><a name="has_ready_fibers"></a></span> <a class="link" href="fiber.html#has_ready_fibers">Non-member function <code class="computeroutput">has_ready_fibers()</code></a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">has_ready_fibers</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if scheduler has fibers ready to run. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing </p></dd> <dt><span class="term">Note:</span></dt> <dd><p> Can be used for work-stealing to find an idle scheduler. </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="id.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos/fiber/doc/html/fiber
repos/fiber/doc/html/fiber/fiber_mgmt/id.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Class fiber::id</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Fiber"> <link rel="up" href="../fiber_mgmt.html" title="Fiber management"> <link rel="prev" href="fiber.html" title="Class fiber"> <link rel="next" href="this_fiber.html" title="Namespace this_fiber"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fiber.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="this_fiber.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="fiber.fiber_mgmt.id"></a><a name="class_id"></a><a class="link" href="id.html" title="Class fiber::id">Class fiber::id</a> </h3></div></div></div> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span> <span class="keyword">class</span> <span class="identifier">id</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">constexpr</span> <span class="identifier">id</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_ostream</span><span class="special">&lt;</span> <span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_ostream</span><span class="special">&lt;</span> <span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="special">&amp;,</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;);</span> <span class="special">};</span> <span class="special">}}</span> </pre> <h5> <a name="fiber.fiber_mgmt.id.h0"></a> <span><a name="fiber.fiber_mgmt.id.constructor"></a></span><a class="link" href="id.html#fiber.fiber_mgmt.id.constructor">Constructor</a> </h5> <pre class="programlisting"><span class="keyword">constexpr</span> <span class="identifier">id</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Effects:</span></dt> <dd><p> Represents an instance of <span class="emphasis"><em>not-a-fiber</em></span>. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_equal_bridgehead"></a> <span><a name="id_operator_equal"></a></span> <a class="link" href="id.html#id_operator_equal">Member function <code class="computeroutput">operator==</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> and <code class="computeroutput"><span class="identifier">other</span></code> represent the same fiber, or both represent <span class="emphasis"><em>not-a-fiber</em></span>, <code class="computeroutput"><span class="keyword">false</span></code> otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_not_equal_bridgehead"></a> <span><a name="id_operator_not_equal"></a></span> <a class="link" href="id.html#id_operator_not_equal">Member function <code class="computeroutput">operator!=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput">! (other == * this)</code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_less_bridgehead"></a> <span><a name="id_operator_less"></a></span> <a class="link" href="id.html#id_operator_less">Member function <code class="computeroutput">operator&lt;</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="keyword">true</span></code> if <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span> <span class="special">!=</span> <span class="identifier">other</span></code> is true and the implementation-defined total order of <code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">id</span></code> values places <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> before <code class="computeroutput"><span class="identifier">other</span></code>, false otherwise. </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_greater_bridgehead"></a> <span><a name="id_operator_greater"></a></span> <a class="link" href="id.html#id_operator_greater">Member function <code class="computeroutput">operator&gt;</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">other</span> <span class="special">&lt;</span> <span class="special">*</span> <span class="keyword">this</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_less_equal_bridgehead"></a> <span><a name="id_operator_less_equal"></a></span> <a class="link" href="id.html#id_operator_less_equal">Member function <code class="computeroutput">operator&lt;=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="special">!</span> <span class="special">(</span><span class="identifier">other</span> <span class="special">&lt;</span> <span class="special">*</span> <span class="keyword">this</span><span class="special">)</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <p> </p> <h5> <a name="id_operator_greater_equal_bridgehead"></a> <span><a name="id_operator_greater_equal"></a></span> <a class="link" href="id.html#id_operator_greater_equal">Member function <code class="computeroutput">operator&gt;=</code>()</a> </h5> <p> </p> <pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;=(</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="special">!</span> <span class="special">(*</span> <span class="keyword">this</span> <span class="special">&lt;</span> <span class="identifier">other</span><span class="special">)</span></code> </p></dd> <dt><span class="term">Throws:</span></dt> <dd><p> Nothing. </p></dd> </dl> </div> <h5> <a name="fiber.fiber_mgmt.id.h1"></a> <span><a name="fiber.fiber_mgmt.id.operator_lt__lt_"></a></span><a class="link" href="id.html#fiber.fiber_mgmt.id.operator_lt__lt_">operator&lt;&lt;</a> </h5> <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_ostream</span><span class="special">&lt;</span> <span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_ostream</span><span class="special">&lt;</span> <span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traitsT</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="identifier">id</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">);</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl> <dt><span class="term">Efects:</span></dt> <dd><p> Writes the representation of <code class="computeroutput"><span class="identifier">other</span></code> to stream <code class="computeroutput"><span class="identifier">os</span></code>. The representation is unspecified. </p></dd> <dt><span class="term">Returns:</span></dt> <dd><p> <code class="computeroutput"><span class="identifier">os</span></code> </p></dd> </dl> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="fiber.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fiber_mgmt.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="this_fiber.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>
0
repos
repos/libredo/COPYING.md
# GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. ## Preamble The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. The precise terms and conditions for copying, distribution and modification follow. ## TERMS AND CONDITIONS ### 0. Definitions. "This License" refers to version 3 of the GNU Affero General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. ### 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. ### 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. ### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. ### 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. ### 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: - a) The work must carry prominent notices stating that you modified it, and giving a relevant date. - b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". - c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. - d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. ### 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: - a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. - b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. - c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. - d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. - e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. ### 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: - a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or - b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or - c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or - d) Limiting the use for publicity purposes of names of licensors or authors of the material; or - e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or - f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. ### 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. ### 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. ### 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. ### 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. ### 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. ### 13. Remote Network Interaction; Use with the GNU General Public License. Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License. ### 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. ### 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ### 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. ### 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS ## How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. <one line to give the program's name and a brief idea of what it does.> Copyright (C) <year> <name of author> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. Also add information on how to contact you by electronic and paper mail. If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements. You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>.
0
repos
repos/libredo/build.zig
const std = @import("std"); var mod: *std.Build.Module = undefined; fn link(exe: *std.Build.CompileStep, add_coz: bool) void { exe.addModule("signals", mod); if (add_coz) { exe.linkLibC(); exe.addLibraryPath(.{ .path = "/usr/lib/coz-profiler" }); exe.linkSystemLibrary("coz"); exe.addCSourceFiles(&.{"src/coz.c"}, &.{}); } } pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const opt_test_filter = b.option([]const u8, "test-filter", "test filter"); const opt_use_coz = b.option(bool, "coz", "Enable coz profiler") orelse false; mod = b.addModule("signals", .{ .source_file = .{ .path = "src/main.zig" }, }); const bench = b.addExecutable(.{ .name = "bench", .root_source_file = .{ .path = "src/tests.zig" }, }); link(bench, opt_use_coz); b.installArtifact(bench); const bench_step = b.step("bench", "Run benchmarks"); bench_step.dependOn(&b.addRunArtifact(bench).step); const tests = b.addTest(.{ .root_source_file = .{ .path = "src/tests.zig" }, .target = target, .optimize = optimize, }); if (opt_test_filter) |x| tests.filter = x; link(tests, false); b.installArtifact(tests); const run_tests = b.addRunArtifact(tests); const test_step = b.step("test", "Run library tests"); test_step.dependOn(&run_tests.step); }
0
repos
repos/libredo/readme.md
Reactive signal/Dependency tracking library in Zig. Data management not included. Possible application: writing UI or build system logic. Dependency graph data structure inspired by [redo](https://github.com/apenwarr/redo). Dependency tracker algorithom inspired by [trkl](https://github.com/jbreckmckye/trkl). Benchmark code adapted from [maverick-js/signals](https://github.com/maverick-js/signals/pull/19/files#diff-ed2047e0fe1c26b6afee97d3b120cc35ee4bc0203bc06be33687736a16ac4a8e). ## Documentation Read [src/main.zig](src/main.zig#L1). ## Use as library This is a Zig-only library. The module name is called `signals`. This git repo has no submodule, so the tarball can be used in .zon directly. If you don't know how to use a Zig library, use the search engine to look it up. ## Todo - add test suite more more tests - add solid.js-like interface ## optimization ideas The current `BijectMap` is fast enough already. - [ ] cache lookup, so `BijectMap.add` can be used later faster - [x] use u16 instead of u64 as id: ~4x faster - [x] coz: tested. not useful. - [x] ReleaseFast: same speed as ReleaseSafe. - [x] hashmap of hashset: see branch `algo-hashmap`. too slow - [x] splay tree: see branch `algo-splaytree`. way too slow
0
repos/libredo/research
repos/libredo/research/redo/readme.md
Example "project" to test the time complexity of redo. Run `redo-ifchange 100` to test cache. Run `touch 0` to invalidate cache. ## Benchmark trace Verdict: it's O(n). ``` ❯ for n in 10 100 500 1000 2000 ./bench $n end ________________________________________________________ Executed in 397.20 millis fish external usr time 273.70 millis 147.00 micros 273.55 millis sys time 71.70 millis 53.00 micros 71.65 millis ________________________________________________________ Executed in 3.59 secs fish external usr time 2.85 secs 158.00 micros 2.85 secs sys time 0.72 secs 43.00 micros 0.72 secs ________________________________________________________ Executed in 18.05 secs fish external usr time 14.30 secs 179.00 micros 14.30 secs sys time 3.82 secs 25.00 micros 3.82 secs ________________________________________________________ Executed in 36.43 secs fish external usr time 28.70 secs 176.00 micros 28.70 secs sys time 7.69 secs 52.00 micros 7.69 secs ________________________________________________________ Executed in 77.38 secs fish external usr time 59.48 secs 150.00 micros 59.48 secs sys time 16.69 secs 50.00 micros 16.69 secs ```
0
repos/libredo
repos/libredo/src/coz.c
#include <coz.h> void coz_begin(const char* name) { COZ_BEGIN(name); } void coz_end(const char* name) { COZ_END(name); }
0
repos/libredo
repos/libredo/src/main.zig
//! Dependency tracking library supporting cyclic dependencies test "Table Of Contents" { _ = .{ // Dependency graph as trie, for direct dependency tracking // If you are building a build system, this is what you need BijectMap(u64, u64), // redo/Solid.JS-like automatic dependency tracker. // Data not included (you need to manage data yourself). dependency_module(u64).Tracker, }; } const std = @import("std"); const FieldType = std.meta.FieldType; const assert = std.debug.assert; const asBytes = std.mem.asBytes; /// Special two-way map for dependency tracking /// Please use as BijectMap(dependent, dependency) /// /// The data structure is adapted from redo-python and sqlite /// Important functions: /// - replaceValues /// - [`iteratorByValue`] pub fn BijectMap(comptime K: type, comptime V: type) type { return struct { const _important_api = .{ replaceValues, }; a: std.mem.Allocator, arr: std.ArrayList(Entry), pub const Entry = std.meta.Tuple(&.{ K, V }); pub const Iterator = struct { value: V, slice: []const Entry, i: usize, pub fn next(this: *@This()) ?K { while (this.i < this.slice.len) { const entry = this.slice[this.i]; this.i += 1; if (entry[1] == this.value) return entry[0]; } return null; } }; pub fn init(a: std.mem.Allocator) @This() { return .{ .a = a, .arr = FieldType(@This(), .arr).init(a) }; } pub fn deinit(this: @This()) void { this.arr.deinit(); } /// Update dependencies of `key` /// /// `values` must be a list of `.{key, <dependency>}` pub fn replaceValues(this: *@This(), key: K, entries: []const Entry) void { const start = binarySearchNotGreater(Entry, Entry{ key, 0 }, this.arr.items, void{}, _compareFn); var i = start; // var i: usize = 0; var len: usize = 0; while (i < this.arr.items.len) : (i += 1) { const item = this.arr.items[i]; if (item[0] == key) { len += 1; } else { break; } } for (entries) |entry| { assert(entry[0] == key); } if (len > 0 or entries.len > 0) this.arr.replaceRange(start, len, entries) catch unreachable; } /// Query dependents of `value`. Returns `Iterator`. pub fn iteratorByValue(this: @This(), value: V) ?Iterator { return Iterator{ .value = value, .slice = this.arr.items, .i = 0, }; } pub fn _eq(lhs: Entry, rhs: Entry) bool { return std.meta.eql(lhs, rhs); } pub fn _compareFn(_: void, lhs: Entry, rhs: Entry) std.math.Order { return std.mem.order(u8, asBytes(&lhs), asBytes(&rhs)); } pub fn _lessThanFn(_: void, lhs: Entry, rhs: Entry) bool { return std.mem.lessThan(u8, asBytes(&lhs), asBytes(&rhs)); } /// print debug info pub fn _dumpLog(this: @This()) void { for (this.arr.items) |x| { std.log.warn("{} -> {}", x); } } }; } // helper pub fn binarySearchNotGreater( comptime T: type, key: anytype, items: []const T, context: anytype, comptime compareFn: fn (context: @TypeOf(context), key: @TypeOf(key), mid_item: T) std.math.Order, ) usize { var left: usize = 0; var right: usize = items.len; while (left < right) { // Avoid overflowing in the midpoint calculation const mid = left + (right - left) / 2; // Compare the key with the midpoint element switch (compareFn(context, key, items[mid])) { .eq => return mid, .gt => left = mid + 1, .lt => right = mid, } } assert(left == right); return left; } /// Construct a dependency-tracking module /// `id_type`: signal/task id type. Should be integer like u64. Smaller is better (saves memory). /// /// Returns a struct filled with types. pub fn dependency_module(comptime id_type: type) type { return struct { pub const TaskId = id_type; pub const Map = BijectMap(TaskId, TaskId); pub const Entry = Map.Entry; pub const Collector = struct { dependent: TaskId, dependencies: std.ArrayList(Entry), pub fn init(a: std.mem.Allocator, dependent: TaskId) @This() { return .{ .dependent = dependent, .dependencies = FieldType(@This(), .dependencies).init(a), }; } pub fn deinit(this: @This()) void { this.dependencies.deinit(); } pub fn add(this: *@This(), dependency: TaskId) !void { try this.dependencies.append(.{ this.dependent, dependency }); } pub fn getSortedList(this: @This()) []const Entry { std.mem.sort(Entry, this.dependencies.items, void{}, Map._lessThanFn); return this.dependencies.items; } }; /// Auto Depnedency Tracker like Solid.JS /// /// For usage, check below. /// /// For example, please refer to function `run` in tests.zig for usage. pub const Tracker = struct { test "Usage" { _ = .{ // Register tasks register, unregister, // // Query and set task status isDirty, setDirty, invalidate, // // Tracking dependencies begin, end, used, }; } a: std.mem.Allocator, tracked: std.ArrayList(Collector), pairs: Map, dirty_set: std.AutoHashMap(TaskId, void), pub fn init(a: std.mem.Allocator) !@This() { return .{ .a = a, .tracked = FieldType(@This(), .tracked).init(a), .pairs = FieldType(@This(), .pairs).init(a), .dirty_set = FieldType(@This(), .dirty_set).init(a), }; } pub fn deinit(this: @This()) void { this.tracked.deinit(); this.pairs.deinit(); var dict = this.dirty_set; dict.deinit(); } /// register non-source task /// /// source tasks (ones that do not depend on anything) do not need to be registered pub fn register(this: *@This(), task_id: TaskId) !void { const res = try this.setDirty(task_id, true); if (res) std.debug.panic("task_id already registered: {}", .{task_id}); } /// unregister non-source task pub fn unregister(this: *@This(), task_id: TaskId) void { this.pairs.replaceValues(task_id, &.{}); } /// check if the task need to be re-run pub fn isDirty(this: @This(), dependency: TaskId) bool { return this.dirty_set.contains(dependency); } /// set task dirty state, returns previous state pub fn setDirty(this: *@This(), dependency: TaskId, value: bool) !bool { if (value) { const res = try this.dirty_set.getOrPut(dependency); return res.found_existing; } else { return this.dirty_set.remove(dependency); } } /// mark that the task has changed pub fn invalidate(this: *@This(), dependency: TaskId) !void { var it = this.pairs.iteratorByValue(dependency) orelse return; while (it.next()) |dependent| { if (!try this.setDirty(dependent, true)) { try this.invalidate(dependent); } } } /// start tracking dependencies pub fn begin(this: *@This(), dependent: TaskId) !void { try this.tracked.append(Collector.init(this.a, dependent)); } /// stop tracking dependencies pub fn end(this: *@This()) void { const collector: Collector = this.tracked.pop(); defer collector.deinit(); const sorted_list = collector.getSortedList(); this.pairs.replaceValues(collector.dependent, sorted_list); // std.log.warn("replace({}, {any})", .{ collector.dependent, sorted_list }); } /// mark that`dependency` is used pub fn used(this: *@This(), dependency: TaskId) !void { if (this.tracked.items.len == 0) return; const collector = &this.tracked.items[this.tracked.items.len - 1]; try collector.add(dependency); } /// print debug info pub fn _dumpLog(this: @This()) void { std.log.warn("#dep={} #dirty={}", .{ this.pairs.arr.items.len, this.dirty_set.count() }); this.pairs._dumpLog(); var it = this.dirty_set.iterator(); while (it.next()) |kv| { std.log.warn("dirty: {}", .{kv.key_ptr.*}); } } }; }; }
0
repos/libredo
repos/libredo/src/tests.zig
const std = @import("std"); const signals = @import("signals"); extern fn coz_begin(name: [*:0]const u8) void; extern fn coz_end(name: [*:0]const u8) void; test "type check" { std.testing.refAllDeclsRecursive(signals); } const mod = signals.dependency_module(u16); const Scope = mod.Tracker; /// Benchmarking code adapted from https://github.com/maverick-js/signals/blob/b2084a54968101018c21aa247da127276a0389df/bench/layers.js#L229-L267 fn get_layers(cx: *Scope, _id: anytype) !void { const id: mod.TaskId = @intCast(_id); // std.log.warn("get({})", .{id}); try cx.used(id); if (id >= 4 and (try cx.setDirty(id, false))) { try cx.begin(id); defer cx.end(); switch (id % 4) { 0 => { try get_layers(cx, id - 3); }, 1 => { try get_layers(cx, id - 3); try get_layers(cx, id - 5); }, 2 => { try get_layers(cx, id - 3); try get_layers(cx, id - 5); }, 3 => { try get_layers(cx, id - 5); }, else => unreachable, } } } /// returns ns elapsed fn run(a: std.mem.Allocator, layer_count: usize, comptime check: bool, comptime coz: bool) !u64 { // var opts = Scope.InitOptions{}; // opts.dependency_pairs_capacity *= @max(1, layer_count / 100); // opts.dependent_stack_capacity *= @max(1, layer_count / 100); // opts.dirty_set_capacity *= @max(1, @as(u32, @intCast(layer_count / 100))); var cx = try Scope.init(a); defer cx.deinit(); const base_id = (layer_count - 1) * 4; const checkpoint_name = try std.fmt.allocPrintZ(a, "n={}", .{layer_count}); defer a.free(checkpoint_name); if (coz) coz_begin(checkpoint_name); defer if (coz) coz_end(checkpoint_name); var timer = try std.time.Timer.start(); // register memos (by default is dirty) for (4..layer_count * 4) |i| { try cx.register(@intCast(i)); } // defer { // for (0..layer_count * 4) |i| { // cx.unregister(i); // } // } if (check) for (4..layer_count * 4) |i| { // cx.pairs.dumpLog(); try std.testing.expect(cx.isDirty(i)); }; const ns_prepare = timer.lap(); try get_layers(&cx, base_id + 0); try get_layers(&cx, base_id + 1); try get_layers(&cx, base_id + 2); try get_layers(&cx, base_id + 3); if (check) for (4..layer_count * 4) |i| { // cx.pairs.dumpLog(); try std.testing.expect(!cx.isDirty(i)); }; const ns0 = timer.lap(); // try get_layers(&cx, base_id + 0); // try get_layers(&cx, base_id + 1); // try get_layers(&cx, base_id + 2); // try get_layers(&cx, base_id + 3); // if (check) for (4..layer_count * 4) |i| { // // cx.pairs.dumpLog(); // try std.testing.expect(!cx.isDirty(i)); // }; // const ns1 = timer.lap(); // _ = ns1; // try cx.invalidate(0); // try cx.invalidate(1); // try cx.invalidate(2); // try cx.invalidate(3); // if (check) { // // cx.pairs.dumpLog(); // // { // // var it = cx.dirty_set.iterator(); // // while (it.next()) |kv| { // // std.log.warn("dirty: {}", .{kv.key_ptr.*}); // // } // // } // // { // // for (cx.pairs.items) |kv| { // // std.log.warn("dep: {} -> {}", .{ kv[0], kv[1] }); // // } // // } // for (4..layer_count * 4) |i| { // try std.testing.expect(cx.isDirty(i)); // } // } // const ns2 = timer.lap(); // _ = ns2; // try get_layers(&cx, base_id + 0); // // std.log.warn("after base_id+0", .{}); // // cx.pairs.dumpLog(); // try get_layers(&cx, base_id + 1); // // std.log.warn("after base_id+1", .{}); // // cx.pairs.dumpLog(); // try get_layers(&cx, base_id + 2); // // cx.pairs.dumpLog(); // // unreachable; // try get_layers(&cx, base_id + 3); // if (check) for (4..layer_count * 4) |i| { // try std.testing.expect(!cx.isDirty(i)); // }; // const ns3 = timer.lap(); // _ = ns3; // try get_layers(&cx, base_id + 0); // try get_layers(&cx, base_id + 1); // try get_layers(&cx, base_id + 2); // try get_layers(&cx, base_id + 3); // if (check) for (4..layer_count * 4) |i| { // try std.testing.expect(!cx.isDirty(i)); // }; // const ns4 = timer.lap(); // _ = ns4; // std.log.warn("time used: {any}", .{[_]u64{ ns_prepare, ns0, ns1, ns2, ns3, ns4 }}); // return ns_prepare + ns0 + ns1 + ns2 + ns3 + ns4; return ns_prepare + ns0; } test "sanity check" { _ = try run(std.testing.allocator, 2, true, false); } test "verify dependency" { var cx = try Scope.init(std.testing.allocator); defer cx.deinit(); try std.testing.expectEqualDeep(@as([]const mod.Entry, &.{}), cx.pairs.arr.items); for (0..16) |i| try cx.register(@intCast(i)); try std.testing.expectEqualDeep(@as([]const mod.Entry, &.{}), cx.pairs.arr.items); for (12..16) |i| try get_layers(&cx, i); const expected = [_]mod.Entry{ .{ 4, 1 }, .{ 5, 0 }, .{ 5, 2 }, .{ 6, 1 }, .{ 6, 3 }, .{ 7, 2 }, .{ 8, 5 }, .{ 9, 4 }, .{ 9, 6 }, .{ 10, 5 }, .{ 10, 7 }, .{ 11, 6 }, .{ 12, 9 }, .{ 13, 8 }, .{ 13, 10 }, .{ 14, 9 }, .{ 14, 11 }, .{ 15, 10 }, }; try std.testing.expectEqualDeep(@as([]const mod.Entry, &expected), cx.pairs.arr.items); } fn get2(cx: *Scope, id: u64) !void { try cx.used(id); if (try cx.setDirty(id, false)) { try cx.begin(id); defer cx.end(); switch (id % 3) { 0 => { try get2(cx, 1); }, 1 => { try get2(cx, 2); }, 2 => { try get2(cx, 3); }, else => unreachable, } } } test "cyclic dependency graph" { var cx = try Scope.init(std.testing.allocator); defer cx.deinit(); try std.testing.expectEqual(@as(u32, 0), cx.dirty_set.count()); try std.testing.expectEqualDeep(@as([]const mod.Entry, &.{}), cx.pairs.arr.items); for (1..4) |i| try cx.register(@intCast(i)); try std.testing.expectEqual(@as(u32, 3), cx.dirty_set.count()); try std.testing.expectEqualDeep(@as([]const mod.Entry, &.{}), cx.pairs.arr.items); for (1..4) |i| { try get2(&cx, 3); try get2(&cx, 2); try get2(&cx, 1); try std.testing.expectEqualDeep(@as([]const mod.Entry, &.{ .{ 1, 2 }, .{ 2, 3 }, .{ 3, 1 } }), cx.pairs.arr.items); try std.testing.expectEqual(@as(u32, 0), cx.dirty_set.count()); try cx.invalidate(i); try std.testing.expectEqual(@as(u32, 3), cx.dirty_set.count()); } } /// benchmark pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const RUNS_PER_TIER = 150; const LAYER_TIERS = [_]usize{ 10, 100, 500, 1000, 2000, }; // const SOLUTIONS = { // 10: [2, 4, -2, -3], // 100: [-2, -4, 2, 3], // 500: [-2, 1, -4, -4], // 1000: [-2, -4, 2, 3], // 2000: [-2, 1, -4, -4], // // 2500: [-2, -4, 2, 3], // }; const stderr = std.io.getStdErr().writer(); for (LAYER_TIERS) |n_layers| { var sum: u64 = 0; for (0..RUNS_PER_TIER) |_| { sum += try run(gpa.allocator(), n_layers, false, false); } const ns: f64 = @floatFromInt(sum / RUNS_PER_TIER); const ms = ns / std.time.ns_per_ms; try stderr.print("n_layers={} avg {d}ms\n", .{ n_layers, ms }); } }
0
repos
repos/simulations/README.md
# Markets Visually simulate markets of basic consumers and producers. Built on [zig-gamedev](https://github.com/michal-z/zig-gamedev/). Download from the latest release or build from source. ## Random Resource Simulation [demo.webm](https://user-images.githubusercontent.com/95145274/202062756-61222967-26ee-41e1-ba2b-fb9d7d2d41a1.webm) - Simulate market dynamics of consumers and producers. - Consumers move to Producers, get resources, travel home and consume the resources. - Red means consumer is empty and looking for resources. - Green means consumer has enough resources to consume. - Parameters: - Number of Consumers - Number of Producers - Consumers moving rate - Consumers demand rate - Consumers size - Producers production rate - Producers maximum inventory - Data Gathered: - Transactions per second - Empty Consumers - Total Producer Inventory - To remove a line from the graph, click it's title in the legend. ## Resource Editor [editor.webm](https://github.com/ckrowland/simulations/assets/95145274/2c21762f-0dd2-4a00-8d2e-0aad38e83c78) - Manually place position of consumers and producers. - Each producer and consumer grouping has individual parameters. ## Variable Parameters [variable.webm](https://github.com/ckrowland/simulations/assets/95145274/b7e97f85-6828-42fe-827d-af6ee2bdb049) - Very similiar to the random simulation. - Have input parameters controlled via a wave timeline. ## Build From Source ### Download - [Git](https://git-scm.com/) - [Git LFS](https://git-lfs.github.com/) - Zig **0.13.0-dev.351+64ef45eb0**. [zigup](https://github.com/marler8997/zigup) is recommended for managing compiler versions. Alternatively, you can download and install manually using the links below: | OS/Arch | Download link | | --------------- | --------------------------- | | Windows x86_64 | [zig-windows-x86_64-0.13.0-dev.351+64ef45eb0.zip](https://ziglang.org/builds/zig-windows-x86_64-0.13.0-dev.351+64ef45eb0.zip) | | Linux x86_64 | [zig-linux-x86_64-0.13.0-dev.351+64ef45eb0.tar.xz](https://ziglang.org/builds/zig-linux-x86_64-0.13.0-dev.351+64ef45eb0.tar.xz) | | macOS x86_64 | [zig-macos-x86_64-0.13.0-dev.351+64ef45eb0.tar.xz](https://ziglang.org/builds/zig-macos-x86_64-0.13.0-dev.351+64ef45eb0.tar.xz) | | macOS aarch64 | [zig-macos-aarch64-0.13.0-dev.351+64ef45eb0.tar.xz](https://ziglang.org/builds/zig-macos-aarch64-0.13.0-dev.351+64ef45eb0.tar.xz) | ### Run ``` git clone https://github.com/ckrowland/simulations.git cd simulations zig build run ```
0
repos
repos/simulations/build.zig.zon
.{ .name = "Visual Simulations", .version = "0.1.0", .paths = .{""}, .dependencies = .{ .system_sdk = .{ .path = "libs/system-sdk" }, .zglfw = .{ .path = "libs/zglfw" }, .zgui = .{ .path = "libs/zgui" }, .zmath = .{ .path = "libs/zmath" }, .zstbi = .{ .path = "libs/zstbi" }, .ztracy = .{ .path = "libs/ztracy" }, .zemscripten = .{ .path = "libs/zemscripten" }, .emsdk = .{ .url = "https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.52.tar.gz", .hash = "12202192726bf983ec243c7eea956d6107baf6f49d50b62f6a91f5d7471bc6daf53b", }, .zgpu = .{ .path = "libs/zgpu" }, .zpool = .{ .path = "libs/zpool" }, .dawn_x86_64_windows_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-windows-gnu/archive/d3a68014e6b6b53fd330a0ccba99e4dcfffddae5.tar.gz", .hash = "1220f9448cde02ef3cd51bde2e0850d4489daa0541571d748154e89c6eb46c76a267", }, .dawn_x86_64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-linux-gnu/archive/7d70db023bf254546024629cbec5ee6113e12a42.tar.gz", .hash = "12204a3519efd49ea2d7cf63b544492a3a771d37eda320f86380813376801e4cfa73", }, .dawn_aarch64_linux_gnu = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-linux-gnu/archive/c1f55e740a62f6942ff046e709ecd509a005dbeb.tar.gz", .hash = "12205cd13f6849f94ef7688ee88c6b74c7918a5dfb514f8a403fcc2929a0aa342627", }, .dawn_aarch64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-aarch64-macos/archive/d2360cdfff0cf4a780cb77aa47c57aca03cc6dfe.tar.gz", .hash = "12201fe677e9c7cfb8984a36446b329d5af23d03dc1e4f79a853399529e523a007fa", }, .dawn_x86_64_macos = .{ .url = "https://github.com/michal-z/webgpu_dawn-x86_64-macos/archive/901716b10b31ce3e0d3fe479326b41e91d59c661.tar.gz", .hash = "1220b1f02f2f7edd98a078c64e3100907d90311d94880a3cc5927e1ac009d002667a", }, }, }
0
repos
repos/simulations/build.zig
const builtin = @import("builtin"); const std = @import("std"); pub const Options = struct { optimize: std.builtin.Mode, target: std.Build.ResolvedTarget, }; pub fn build(b: *std.Build) !void { const options = Options{ .optimize = b.standardOptimizeOption(.{}), .target = b.standardTargetOptions(.{}), }; const exe = createExe(b, options); //const native = std.zig.system.NativeTargetInfo.detect(options.target) catch unreachable; //std.debug.print("{s}-{s}-{s}\n", .{ @tagName(native.target.cpu.arch), @tagName(native.target.os.tag), @tagName(native.target.abi) }); const install_exe = b.addInstallArtifact(exe, .{}); b.getInstallStep().dependOn(&install_exe.step); b.step("demo", "Build demo").dependOn(&install_exe.step); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(&install_exe.step); b.step("run", "Run demo").dependOn(&run_cmd.step); var release_step = b.step("release", "create executables for all apps"); var build_step = b.step("build", "build executables for all apps"); inline for (.{ .{ .os = .windows, .arch = .x86_64, .output = "apps/Windows/windows-x86_64" }, .{ .os = .macos, .arch = .x86_64, .output = "apps/Mac/x86_64/Simulations.app/Contents/MacOS" }, .{ .os = .macos, .arch = .aarch64, .output = "apps/Mac/aarch64/Simulations.app/Contents/MacOS" }, .{ .os = .linux, .arch = .x86_64, .output = "apps/Linux/linux-x86_64-gnu" }, //.{ .os = .linux, .arch = .aarch64, .output = "apps/Linux/aarch64/linux-aarch64" }, }) |release| { const target = b.resolveTargetQuery(.{ .cpu_arch = release.arch, .os_tag = release.os, .abi = if (release.os == .linux) .gnu else null, }); const release_exe = createExe(b, .{ .optimize = .ReleaseFast, .target = target, }); if (release.os == .macos) { release_exe.headerpad_size = 0x10000; } const install_release = b.addInstallArtifact(release_exe, .{ .dest_dir = .{ .override = .{ .custom = "../" ++ release.output }, }, }); build_step.dependOn(&install_release.step); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "../" ++ release.output }, .install_subdir = "content", }); build_step.dependOn(&install_content_step.step); } const zip_windows = b.addSystemCommand(&.{ "tar", "-cavf" }); zip_windows.setCwd(b.path("apps/Windows")); zip_windows.addArg("windows-x86_64.tar.xz"); zip_windows.addArg("Windows-x86_64"); zip_windows.step.dependOn(build_step); const zip_linux = b.addSystemCommand(&.{ "tar", "-cavf" }); zip_linux.setCwd(b.path("apps/Linux")); zip_linux.addArg("linux-x86_64-gnu.tar.xz"); zip_linux.addArg("linux-x86_64-gnu"); zip_linux.step.dependOn(build_step); const notarize_apps = b.option( bool, "notarize_apps", "Create an apple signed dmg to distribute", ) orelse false; if (notarize_apps) { var notarize_step = b.step("notarize", "notarize macos apps"); const dev_id = b.option( []const u8, "developer_id", "Name of certificate used for codesigning macos applications", ); const apple_id = b.option( []const u8, "apple_id", "Apple Id used with your Apple Developer account.", ); const apple_password = b.option( []const u8, "apple_password", "The Apple app specific password used to notarize applications.", ); const apple_team_id = b.option( []const u8, "apple_team_id", "The Apple team ID given on you Apple Developer account.", ); const x86_64 = notarizeMacApp( b, b.path("apps/Mac/x86_64"), dev_id, apple_id, apple_password, apple_team_id, ); const aarch64 = notarizeMacApp( b, b.path("apps/Mac/aarch64"), dev_id, apple_id, apple_password, apple_team_id, ); notarize_step.dependOn(x86_64); notarize_step.dependOn(aarch64); } release_step.dependOn(build_step); release_step.dependOn(&zip_windows.step); release_step.dependOn(&zip_linux.step); } fn createExe(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = "Simulations", .root_source_file = b.path("src/main.zig"), .target = options.target, .optimize = options.optimize, }); @import("system_sdk").addLibraryPathsTo(exe); const zglfw = b.dependency("zglfw", .{ .target = options.target, }); exe.root_module.addImport("zglfw", zglfw.module("root")); exe.linkLibrary(zglfw.artifact("glfw")); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{ .target = options.target, }); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); const zmath = b.dependency("zmath", .{ .target = options.target, }); exe.root_module.addImport("zmath", zmath.module("root")); const zgui = b.dependency("zgui", .{ .target = options.target, .backend = .glfw_wgpu, }); exe.root_module.addImport("zgui", zgui.module("root")); exe.linkLibrary(zgui.artifact("imgui")); const zpool = b.dependency("zpool", .{ .target = options.target, }); exe.root_module.addImport("zpool", zpool.module("root")); const zstbi = b.dependency("zstbi", .{ .target = options.target, }); exe.root_module.addImport("zstbi", zstbi.module("root")); exe.linkLibrary(zstbi.artifact("zstbi")); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "" }, .install_subdir = "bin/content", }); exe.step.dependOn(&install_content_step.step); // TODO: Problems with LTO on Windows. if (exe.rootModuleTarget().os.tag == .windows) { exe.want_lto = false; } if (exe.root_module.optimize == .ReleaseFast) { exe.root_module.strip = true; } return exe; } fn notarizeMacApp( b: *std.Build, path: std.Build.LazyPath, dev_id: ?[]const u8, apple_id: ?[]const u8, apple_password: ?[]const u8, apple_team_id: ?[]const u8, ) *std.Build.Step { const codesign_app = b.addSystemCommand(&.{ "codesign", "-f", "-v", "--deep", "--options=runtime", "--timestamp", "-s", dev_id.?, "Simulations.app", }); codesign_app.setCwd(path); const create_dmg = b.addSystemCommand(&.{ "hdiutil", "create", "-volname", "Simulations", "-srcfolder", "Simulations.app", "-ov", "Simulations.dmg", }); create_dmg.setCwd(path); create_dmg.step.dependOn(&codesign_app.step); const codesign_dmg = b.addSystemCommand(&.{ "codesign", "--timestamp", "-s", dev_id.?, "Simulations.dmg", }); codesign_dmg.setCwd(path); codesign_dmg.step.dependOn(&create_dmg.step); const notarize_dmg = b.addSystemCommand(&.{ "xcrun", "notarytool", "submit", "Simulations.dmg", "--apple-id", apple_id.?, "--password", apple_password.?, "--team-id", apple_team_id.?, "--wait", }); notarize_dmg.setCwd(path); notarize_dmg.step.dependOn(&codesign_dmg.step); const staple_app = b.addSystemCommand(&.{ "xcrun", "stapler", "staple", "Simulations.app", }); staple_app.setCwd(path); staple_app.step.dependOn(&notarize_dmg.step); const staple_dmg = b.addSystemCommand(&.{ "xcrun", "stapler", "staple", "Simulations.dmg", }); staple_dmg.setCwd(path); staple_dmg.step.dependOn(&staple_app.step); const validate_app = b.addSystemCommand(&.{ "xcrun", "stapler", "validate", "Simulations.app", }); validate_app.setCwd(path); validate_app.step.dependOn(&staple_dmg.step); const validate_dmg = b.addSystemCommand(&.{ "xcrun", "stapler", "validate", "Simulations.dmg", }); validate_dmg.setCwd(path); validate_dmg.step.dependOn(&validate_app.step); return &validate_dmg.step; }
0
repos/simulations
repos/simulations/src/main.zig
const std = @import("std"); const zglfw = @import("zglfw"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const zgui = @import("zgui"); const zstbi = @import("zstbi"); const Random = @import("random/main.zig"); const Editor = @import("editor/main.zig"); const Variable = @import("variable/main.zig"); const Selection = enum { Random, Editor, Variable, }; pub var selection = Selection.Variable; pub fn selectionGui() void { zgui.text("Pick a demo", .{}); _ = zgui.comboFromEnum("##tab_bar", &selection); zgui.dummy(.{ .w = 1, .h = 10 }); } pub var quit = false; pub fn main() !void { try zglfw.init(); defer zglfw.terminate(); // Change current working directory to where the executable is located. var buffer: [1024]u8 = undefined; const path = std.fs.selfExeDirPath(buffer[0..]) catch "."; std.posix.chdir(path) catch {}; zglfw.windowHintTyped(.client_api, .no_api); const window = try zglfw.Window.create(1600, 900, "Simulations", null); defer window.destroy(); window.setSizeLimits(400, 400, -1, -1); window.setPos(50, 50); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); zstbi.init(allocator); defer zstbi.deinit(); const gctx = try zgpu.GraphicsContext.create( allocator, .{ .window = window, .fn_getTime = @ptrCast(&zglfw.getTime), .fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize), .fn_getWin32Window = @ptrCast(&zglfw.getWin32Window), .fn_getX11Display = @ptrCast(&zglfw.getX11Display), .fn_getX11Window = @ptrCast(&zglfw.getX11Window), .fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay), .fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow), .fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow), }, .{}, ); defer gctx.destroy(allocator); zgui.init(allocator); defer zgui.deinit(); zgui.plot.init(); defer zgui.plot.deinit(); const cs = window.getContentScale(); const content_scale = @max(cs[0], cs[1]); zgui.io.setIniFilename(null); _ = zgui.io.addFontFromFile( "content/fonts/Roboto-Medium.ttf", 22.0 * content_scale, ); zgui.backend.init( window, gctx.device, @intFromEnum(zgpu.GraphicsContext.swapchain_format), @intFromEnum(wgpu.TextureFormat.undef), ); defer zgui.backend.deinit(); while (!quit) { inline for (.{ .{ .file = @import("random/main.zig"), .selection = Selection.Random, }, .{ .file = @import("editor/main.zig"), .selection = Selection.Editor, }, .{ .file = @import("variable/main.zig"), .selection = Selection.Variable, }, }) |demo| { var state = try demo.file.init(gctx, allocator, window); defer demo.file.deinit(&state); quit = window.shouldClose(); while (!quit) { quit = window.shouldClose(); switch (selection) { demo.selection => { zglfw.pollEvents(); const sd = state.gctx.swapchain_descriptor; zgui.backend.newFrame(sd.width, sd.height); demo.file.update(&state, &selectionGui); demo.file.draw(&state); state.window.swapBuffers(); }, else => break, } } } } }
0
repos/simulations/src
repos/simulations/src/variable/camera.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zmath = @import("zmath"); // Camera Settings pub const POS: [3]f32 = .{ 0.0, 0.0, -3000.0 }; pub const FOCUS: [3]f32 = .{ 0.0, 0.0, 0.0 }; pub const UP: [4]f32 = .{ 0.0, 1.0, 0.0, 0.0 }; pub const FOV_Y: f32 = 0.22 * std.math.pi; pub const NEAR_PLANE: f32 = 0.01; pub const FAR_PLANE: f32 = 3100.0; // Grid limits for absolute positions (without aspect ratio) pub const MAX_X: i32 = 1000; pub const MIN_X: i32 = -1000; pub const MAX_Y: i32 = 1000; pub const MIN_Y: i32 = -1000; pub const TOTAL_X: i32 = 2000; pub const TOTAL_Y: i32 = 2000; // Viewport size relative to total window size pub const VP_X_SIZE: f32 = 0.75; pub const VP_Y_SIZE: f32 = 0.75; pub fn getViewportPixelSize(gctx: *zgpu.GraphicsContext) [2]f32 { return .{ @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)) * VP_X_SIZE, @as(f32, @floatFromInt(gctx.swapchain_descriptor.height)) * VP_Y_SIZE, }; } pub fn getAspectRatio(gctx: *zgpu.GraphicsContext) f32 { const sd = gctx.swapchain_descriptor; return @as(f32, @floatFromInt(sd.width)) / @as(f32, @floatFromInt(sd.height)); } // Given a world position (grid position with aspect), return grid position pub fn getGridFromWorld(gctx: *zgpu.GraphicsContext, world_pos: [2]f32) [2]i32 { const aspect = getAspectRatio(gctx); return .{ @as(i32, @intFromFloat(world_pos[0] / aspect)), @as(i32, @intFromFloat(world_pos[1])), // world_pos[2], // world_pos[3], }; } // Given a grid position, return a world position pub fn getWorldPosition(gctx: *zgpu.GraphicsContext, grid_pos: [4]i32) [4]f32 { const aspect = getAspectRatio(gctx); return .{ @as(f32, @floatFromInt(grid_pos[0])) * aspect, @as(f32, @floatFromInt(grid_pos[1])), @as(f32, @floatFromInt(grid_pos[2])), @as(f32, @floatFromInt(grid_pos[3])), }; } // Given a grid position, return a pixel position pub fn getPixelPosition(gctx: *zgpu.GraphicsContext, g_pos: [2]i32) [2]f32 { const grid_pos = .{ g_pos[0], g_pos[1], 1, 1 }; const world_pos = zmath.loadArr4(getWorldPosition(gctx, grid_pos)); const camera_pos = zmath.mul(world_pos, getObjectToClipMat(gctx)); const rel_pos = [4]f32{ camera_pos[0] / -POS[2], camera_pos[1] / -POS[2], 0, 1 }; const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const cursor_in_vp_x = ((rel_pos[0] + 1) * viewport_size[0]) / 2; const cursor_in_vp_y = ((-rel_pos[1] + 1) * viewport_size[1]) / 2; return .{ cursor_in_vp_x + xOffset, cursor_in_vp_y }; } // Given a pixel position, return a grid position pub fn getGridPosition(gctx: *zgpu.GraphicsContext, p_pos: [2]f32) [2]i32 { const viewport_size = getViewportPixelSize(gctx); const width = @as(f32, @floatFromInt(gctx.swapchain_descriptor.width)); const xOffset = width - viewport_size[0]; const rel_pos_x = (((p_pos[0] - xOffset) * 2) / viewport_size[0]) - 1; const rel_pos_y = ((p_pos[1] * 2) / viewport_size[1]) - 1; const camera_pos = zmath.loadArr4(.{ rel_pos_x * -POS[2], rel_pos_y * POS[2], 1, 1, }); const inverse_mat = zmath.inverse(getObjectToClipMat(gctx)); const world_pos = zmath.mul(camera_pos, inverse_mat); return getGridFromWorld(gctx, .{ world_pos[0], world_pos[1] }); } pub fn getObjectToClipMat(gctx: *zgpu.GraphicsContext) zmath.Mat { const camWorldToView = zmath.lookAtLh( zmath.loadArr3(POS), zmath.loadArr3(FOCUS), zmath.loadArr4(UP), ); const camViewToClip = zmath.perspectiveFovLh( FOV_Y, getAspectRatio(gctx), NEAR_PLANE, FAR_PLANE, ); const camWorldToClip = zmath.mul(camWorldToView, camViewToClip); // return zmath.transpose(camWorldToClip); return camWorldToClip; }
0
repos/simulations/src
repos/simulations/src/variable/distribution.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), avg_consumer_balance: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), .avg_consumer_balance = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); self.avg_consumer_balance.deinit(); } pub fn generateAndFillRandomColor(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 4 * @sizeOf(u32), f32, &.{ random.float(f32), random.float(f32), random.float(f32) }, ); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); self.avg_consumer_balance.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const setArgs = struct { stat_obj: Wgpu.ObjectBuffer(u32), num: u32, param: enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }, }; pub fn setNum(gctx: *zgpu.GraphicsContext, args: setArgs) void { gctx.queue.writeBuffer( gctx.lookupResource(args.stat_obj.buf).?, @intFromEnum(args.param) * @sizeOf(u32), u32, &.{args.num}, ); }
0
repos/simulations/src
repos/simulations/src/variable/main.zig
const std = @import("std"); const math = std.math; const zglfw = @import("zglfw"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const zgui = @import("zgui"); const zm = @import("zmath"); const zstbi = @import("zstbi"); const Statistics = @import("statistics.zig"); const Gui = @import("gui.zig"); const Wgpu = @import("wgpu.zig"); const Config = @import("config.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Square = @import("square.zig"); const Circle = @import("circle.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_PRODUCERS = 100; pub const MAX_NUM_CONSUMERS = 40000; pub const NUM_CONSUMER_SIDES = 40; pub const PRODUCER_WIDTH = 40; pub const CONSUMER_RADIUS: f32 = 1.0; pub const DemoState = struct { gctx: *zgpu.GraphicsContext, window: *zglfw.Window, aspect: f32, allocator: std.mem.Allocator, running: bool = false, push_coord_update: bool = false, push_restart: bool = false, content_scale: f32, timeline_visible: bool, comptime sliders: Gui.Variables = .{}, params: struct { sample_idx: usize, radian: f64, max_num_stats: u32, num_consumer_sides: u32, plot_hovered: bool, production_rate: Gui.Variable(u32), max_inventory: Gui.Variable(u32), consumer_size: Gui.Variable(f32), price: Gui.Variable(u32), num_consumers: Gui.Variable(u32), num_producers: Gui.Variable(u32), max_demand_rate: Gui.Variable(u32), moving_rate: Gui.Variable(f32), income: Gui.Variable(u32), }, stats: Statistics, render_pipelines: struct { circle: zgpu.RenderPipelineHandle, square: zgpu.RenderPipelineHandle, }, compute_pipelines: struct { consumer: zgpu.ComputePipelineHandle, producer: zgpu.ComputePipelineHandle, }, bind_groups: struct { render: zgpu.BindGroupHandle, compute: zgpu.BindGroupHandle, }, buffers: struct { data: struct { //ObjectBuffer field name must be ObjectBuffer Type + "s". consumers: Wgpu.ObjectBuffer(Consumer), consumer_params: zgpu.BufferHandle, producers: Wgpu.ObjectBuffer(Producer), stats: Wgpu.ObjectBuffer(u32), }, index: struct { circle: zgpu.BufferHandle, }, vertex: struct { circle: zgpu.BufferHandle, square: zgpu.BufferHandle, }, }, depth_texture: zgpu.TextureHandle, depth_texture_view: zgpu.TextureViewHandle, }; pub fn init( gctx: *zgpu.GraphicsContext, allocator: std.mem.Allocator, window: *zglfw.Window, ) !DemoState { const consumer_object = Wgpu.createObjectBuffer( gctx, Consumer, MAX_NUM_CONSUMERS, 0, ); const consumer_params_buf = Wgpu.createBuffer(gctx, u32, 3); const producer_object = Wgpu.createObjectBuffer( gctx, Producer, MAX_NUM_PRODUCERS, 0, ); const stats_object = Wgpu.createObjectBuffer( gctx, u32, Statistics.NUM_STATS, Statistics.NUM_STATS, ); const compute_bind_group = Wgpu.createComputeBindGroup(gctx, .{ .consumer = consumer_object.buf, .consumer_params = consumer_params_buf, .producer = producer_object.buf, .stats = stats_object.buf, }); const depth = Wgpu.createDepthTexture(gctx); var demo = DemoState{ .gctx = gctx, .window = window, .timeline_visible = true, .aspect = Camera.getAspectRatio(gctx), .content_scale = getContentScale(window), .render_pipelines = .{ .circle = Wgpu.createRenderPipeline(gctx, Config.cpi), .square = Wgpu.createRenderPipeline(gctx, Config.ppi), }, .compute_pipelines = .{ .producer = Wgpu.createComputePipeline(gctx, Config.pcpi), .consumer = Wgpu.createComputePipeline(gctx, Config.ccpi), }, .bind_groups = .{ .render = Wgpu.createUniformBindGroup(gctx), .compute = compute_bind_group, }, .buffers = .{ .data = .{ .consumers = consumer_object, .consumer_params = consumer_params_buf, .producers = producer_object, .stats = stats_object, }, .index = .{ .circle = Circle.createIndexBuffer(gctx, NUM_CONSUMER_SIDES), }, .vertex = .{ .circle = Circle.createVertexBuffer( gctx, NUM_CONSUMER_SIDES, CONSUMER_RADIUS, ), .square = Square.createVertexBuffer(gctx, PRODUCER_WIDTH), }, }, .depth_texture = depth.texture, .depth_texture_view = depth.view, .allocator = allocator, .params = .{ .sample_idx = 0, .radian = 0, .max_num_stats = 3, .plot_hovered = false, .num_consumer_sides = 20, .production_rate = Gui.Variable(u32).init(allocator, 0, 300, 1000, 100, 1, false), .max_inventory = Gui.Variable(u32).init(allocator, 100, 7000, 10000, 100, 1, false), .consumer_size = Gui.Variable(f32).init(allocator, 0, 1, 3, 100, 0.5, false), .price = Gui.Variable(u32).init(allocator, 0, 10, 1000, 100, 0.9, false), .num_consumers = Gui.Variable(u32).init(allocator, 0, 5000, 10000, 100, 1, true), .num_producers = Gui.Variable(u32).init(allocator, 1, 20, 100, 100, 0.7, true), .max_demand_rate = Gui.Variable(u32).init(allocator, 1, 100, 300, 100, 0.9, false), .moving_rate = Gui.Variable(f32).init(allocator, 1, 5, 20, 100, 1, false), .income = Gui.Variable(u32).init(allocator, 1, 100, 500, 100, 1.3, true), }, .stats = Statistics.init(allocator), }; const num_consumers = demo.params.num_consumers.slider.val; const num_producers = demo.params.num_producers.slider.val; Statistics.setNum(&demo, num_consumers, .consumers); Statistics.setNum(&demo, num_producers, .producers); Consumer.generateBulk(&demo, num_consumers); Consumer.setParamsBuf( &demo, demo.params.moving_rate.slider.val, demo.params.max_demand_rate.slider.val, demo.params.income.slider.val, ); Producer.generateBulk(&demo, num_producers); setImguiContentScale(demo.content_scale); return demo; } pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { if (demo.push_restart) restartSimulation(demo); if (demo.push_coord_update) updateAspectRatio(demo); Wgpu.runCallbackIfReady(u32, &demo.buffers.data.stats.mapping); Wgpu.runCallbackIfReady(Producer, &demo.buffers.data.producers.mapping); Wgpu.runCallbackIfReady(Consumer, &demo.buffers.data.consumers.mapping); //zgui.showDemoWindow(null); Gui.update(demo, selection_gui); if (demo.running) { Gui.updateWaves(demo); Statistics.generateAndFillRandomColor(demo); const current_time = @as(f32, @floatCast(demo.gctx.stats.time)); const seconds_passed = current_time - demo.stats.second; if (seconds_passed >= 1) { demo.stats.second = current_time; Wgpu.getAllAsync(demo, u32, Callbacks.numTransactions); Wgpu.getAllAsync(demo, Consumer, Callbacks.consumerStats); Wgpu.getAllAsync(demo, Producer, Callbacks.totalInventory); } } } pub fn draw(demo: *DemoState) void { const gctx = demo.gctx; const cam_world_to_clip = Camera.getObjectToClipMat(gctx); const back_buffer_view = gctx.swapchain.getCurrentTextureView(); defer back_buffer_view.release(); const commands = commands: { const encoder = gctx.device.createCommandEncoder(null); defer encoder.release(); const data = demo.buffers.data; const num_consumers = data.consumers.mapping.num_structs; const num_producers = data.producers.mapping.num_structs; // Compute shaders if (demo.running) { pass: { const pcp = gctx.lookupResource(demo.compute_pipelines.producer) orelse break :pass; const ccp = gctx.lookupResource(demo.compute_pipelines.consumer) orelse break :pass; const bg = gctx.lookupResource(demo.bind_groups.compute) orelse break :pass; const pass = encoder.beginComputePass(null); defer { pass.end(); pass.release(); } pass.setBindGroup(0, bg, &.{}); pass.setPipeline(pcp); pass.dispatchWorkgroups(@divFloor(num_producers, 64) + 1, 1, 1); pass.setPipeline(ccp); pass.dispatchWorkgroups(@divFloor(num_consumers, 64) + 1, 1, 1); } } // Copy data to mapped buffers so we can retrieve it on demand pass: { if (!demo.buffers.data.stats.mapping.waiting) { const s = gctx.lookupResource(data.stats.buf) orelse break :pass; const s_info = gctx.lookupResourceInfo(data.stats.buf) orelse break :pass; const sm = gctx.lookupResource(data.stats.mapping.buf) orelse break :pass; const s_size = @as(usize, @intCast(s_info.size)); encoder.copyBufferToBuffer(s, 0, sm, 0, s_size); } if (!demo.buffers.data.producers.mapping.waiting) { const p = gctx.lookupResource(data.producers.buf) orelse break :pass; const p_info = gctx.lookupResourceInfo(data.producers.buf) orelse break :pass; const pm = gctx.lookupResource(data.producers.mapping.buf) orelse break :pass; const p_size = @as(usize, @intCast(p_info.size)); encoder.copyBufferToBuffer(p, 0, pm, 0, p_size); } if (!demo.buffers.data.consumers.mapping.waiting) { const c = gctx.lookupResource(data.consumers.buf) orelse break :pass; const c_info = gctx.lookupResourceInfo(data.consumers.buf) orelse break :pass; const cm = gctx.lookupResource(data.consumers.mapping.buf) orelse break :pass; const c_size = @as(usize, @intCast(c_info.size)); encoder.copyBufferToBuffer(c, 0, cm, 0, c_size); } } // Draw the circles and squares in our defined viewport pass: { const svb_info = gctx.lookupResourceInfo(demo.buffers.vertex.square) orelse break :pass; const pb_info = gctx.lookupResourceInfo(demo.buffers.data.producers.buf) orelse break :pass; const cvb_info = gctx.lookupResourceInfo(demo.buffers.vertex.circle) orelse break :pass; const cb_info = gctx.lookupResourceInfo(demo.buffers.data.consumers.buf) orelse break :pass; const cib_info = gctx.lookupResourceInfo(demo.buffers.index.circle) orelse break :pass; const square_rp = gctx.lookupResource(demo.render_pipelines.square) orelse break :pass; const circle_rp = gctx.lookupResource(demo.render_pipelines.circle) orelse break :pass; const render_bind_group = gctx.lookupResource(demo.bind_groups.render) orelse break :pass; const depth_view = gctx.lookupResource(demo.depth_texture_view) orelse break :pass; const color_attachments = [_]wgpu.RenderPassColorAttachment{.{ .view = back_buffer_view, .load_op = .clear, .store_op = .store, }}; const depth_attachment = wgpu.RenderPassDepthStencilAttachment{ .view = depth_view, .depth_load_op = .clear, .depth_store_op = .store, .depth_clear_value = 1.0, }; const render_pass_info = wgpu.RenderPassDescriptor{ .color_attachment_count = color_attachments.len, .color_attachments = &color_attachments, .depth_stencil_attachment = &depth_attachment, }; const pass = encoder.beginRenderPass(render_pass_info); defer { pass.end(); pass.release(); } const sd = gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const xOffset = width / 4; const height = @as(f32, @floatFromInt(sd.height)); const yOffset = height / 4; pass.setViewport(xOffset, 0, width - xOffset, height - yOffset, 0, 1); var mem = gctx.uniformsAllocate(zm.Mat, 1); mem.slice[0] = cam_world_to_clip; pass.setBindGroup(0, render_bind_group, &.{mem.offset}); const num_indices_circle = @as( u32, @intCast(cib_info.size / @sizeOf(f32)), ); pass.setPipeline(circle_rp); pass.setVertexBuffer(0, cvb_info.gpuobj.?, 0, cvb_info.size); pass.setVertexBuffer(1, cb_info.gpuobj.?, 0, cb_info.size); pass.setIndexBuffer(cib_info.gpuobj.?, .uint32, 0, cib_info.size); pass.drawIndexed(num_indices_circle, num_consumers, 0, 0, 0); pass.setPipeline(square_rp); pass.setVertexBuffer(0, svb_info.gpuobj.?, 0, svb_info.size); pass.setVertexBuffer(1, pb_info.gpuobj.?, 0, pb_info.size); pass.draw(6, num_producers, 0, 0); } // Draw ImGui { const pass = zgpu.beginRenderPassSimple( encoder, .load, back_buffer_view, null, null, null, ); defer zgpu.endReleasePass(pass); zgui.backend.draw(pass); } break :commands encoder.finish(null); }; defer commands.release(); gctx.submit(&.{commands}); if (demo.gctx.present() == .swap_chain_resized) { demo.content_scale = getContentScale(demo.window); setImguiContentScale(demo.content_scale); updateAspectRatio(demo); } } pub fn restartSimulation(demo: *DemoState) void { const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; const stats_waiting = demo.buffers.data.stats.mapping.waiting; if (consumer_waiting or producer_waiting or stats_waiting) { demo.push_restart = true; return; } Wgpu.clearObjBuffer(demo.gctx, Consumer, &demo.buffers.data.consumers); Wgpu.clearObjBuffer(demo.gctx, Producer, &demo.buffers.data.producers); Wgpu.clearObjBuffer(demo.gctx, u32, &demo.buffers.data.stats); demo.buffers.data.stats.mapping.num_structs = Statistics.NUM_STATS; const num_consumers = demo.params.num_consumers.slider.val; const num_producers = demo.params.num_producers.slider.val; Statistics.setNum(demo, num_consumers, .consumers); Statistics.setNum(demo, num_producers, .producers); Consumer.generateBulk(demo, num_consumers); Producer.generateBulk(demo, num_producers); demo.stats.clear(); demo.push_restart = false; } pub fn updateDepthTexture(demo: *DemoState) void { // Release old depth texture. demo.gctx.releaseResource(demo.depth_texture_view); demo.gctx.destroyResource(demo.depth_texture); // Create a new depth texture to match the new window size. const depth = Wgpu.createDepthTexture(demo.gctx); demo.depth_texture = depth.texture; demo.depth_texture_view = depth.view; } pub fn updateAspectRatio(demo: *DemoState) void { updateDepthTexture(demo); const consumer_waiting = demo.buffers.data.consumers.mapping.waiting; const producer_waiting = demo.buffers.data.producers.mapping.waiting; if (consumer_waiting or producer_waiting) { demo.push_coord_update = true; return; } Wgpu.getAllAsync(demo, Consumer, Callbacks.updateConsumerCoords); Wgpu.getAllAsync(demo, Producer, Callbacks.updateProducerCoords); demo.push_coord_update = false; demo.aspect = Camera.getAspectRatio(demo.gctx); } fn getContentScale(window: *zglfw.Window) f32 { const content_scale = window.getContentScale(); return @max(content_scale[0], content_scale[1]); } fn setImguiContentScale(scale: f32) void { zgui.getStyle().* = zgui.Style.init(); zgui.getStyle().scaleAllSizes(scale); zgui.plot.getStyle().plot_padding = .{ 20 * scale, 20 * scale }; zgui.plot.getStyle().line_weight = 2 * scale; } pub fn deinit(demo: *DemoState) void { demo.params.num_consumers.wave.deinit(); demo.params.num_producers.wave.deinit(); demo.params.max_demand_rate.wave.deinit(); demo.params.moving_rate.wave.deinit(); demo.params.income.wave.deinit(); demo.params.consumer_size.wave.deinit(); demo.params.price.wave.deinit(); demo.params.production_rate.wave.deinit(); demo.params.max_inventory.wave.deinit(); demo.stats.deinit(); //demo.gctx.destroy(demo.allocator); }
0
repos/simulations/src
repos/simulations/src/variable/statistics.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const zgpu = @import("zgpu"); const DemoState = @import("main.zig").DemoState; const Wgpu = @import("wgpu.zig"); const Self = @This(); num_transactions: array(u32), second: f32 = 0, num_empty_consumers: array(u32), num_total_producer_inventory: array(u32), avg_consumer_balance: array(u32), pub const NUM_STATS = 8; pub const zero = [NUM_STATS]u32{ 0, 0, 0, 0, 0, 0, 0, 0 }; pub fn init(allocator: std.mem.Allocator) Self { return Self{ .num_transactions = array(u32).init(allocator), .num_empty_consumers = array(u32).init(allocator), .num_total_producer_inventory = array(u32).init(allocator), .avg_consumer_balance = array(u32).init(allocator), }; } pub fn deinit(self: *Self) void { self.num_transactions.deinit(); self.num_empty_consumers.deinit(); self.num_total_producer_inventory.deinit(); self.avg_consumer_balance.deinit(); } pub fn generateAndFillRandomColor(demo: *DemoState) void { const handle = demo.buffers.data.stats.buf; const resource = demo.gctx.lookupResource(handle).?; const color = [3]f32{ random.float(f32), random.float(f32), random.float(f32) }; demo.gctx.queue.writeBuffer(resource, 3 * @sizeOf(u32), [3]f32, &.{color}); } pub fn clear(self: *Self) void { self.num_transactions.clearAndFree(); self.num_empty_consumers.clearAndFree(); self.num_total_producer_inventory.clearAndFree(); self.avg_consumer_balance.clearAndFree(); } pub fn clearNumTransactions(gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, u32, &.{0}); } pub const Param = enum(u32) { num_transactions = 0, consumers = 1, producers = 2, consumer_hovers = 3, }; pub fn setNum(demo: *DemoState, num: u32, param: Param) void { const resource = demo.gctx.lookupResource(demo.buffers.data.stats.buf).?; const offset = @intFromEnum(param) * @sizeOf(u32); demo.gctx.queue.writeBuffer(resource, offset, u32, &.{num}); }
0
repos/simulations/src
repos/simulations/src/variable/config.zig
const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); pub const cpi = .{ .vs = @embedFile("shaders/vertex/consumer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Consumer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "position", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, }, }; pub const ppi = .{ .vs = @embedFile("shaders/vertex/producer.wgsl"), .fs = @embedFile("shaders/fragment/fragment.wgsl"), .inst_type = Producer, .inst_attrs = &[_]Wgpu.RenderPipelineInfo.Attribute{ .{ .name = "home", .type = [4]f32, }, .{ .name = "color", .type = [4]f32, }, .{ .name = "inventory", .type = u32, }, .{ .name = "max_inventory", .type = u32, }, }, }; const common = @embedFile("shaders/compute/common.wgsl"); pub const ccpi = .{ .cs = common ++ @embedFile("shaders/compute/consumer.wgsl"), .entry_point = "main", }; pub const pcpi = .{ .cs = common ++ @embedFile("shaders/compute/producer.wgsl"), .entry_point = "main", };
0
repos/simulations/src
repos/simulations/src/variable/producer.zig
const std = @import("std"); const array = std.ArrayList; const random = std.crypto.random; const Allocator = std.mem.Allocator; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Wgpu = @import("wgpu.zig"); const Main = @import("main.zig"); const DemoState = Main.DemoState; const Parameters = Main.Parameters; const Camera = @import("camera.zig"); pub const Self = @This(); absolute_home: [4]i32 = .{ 0, 0, 0, 0 }, home: [4]f32 = .{ 0, 0, 0, 0 }, color: [4]f32 = .{ 0, 0, 0, 0 }, production_rate: u32 = 0, inventory: i32 = 0, max_inventory: u32 = 0, price: u32 = 1, pub const z_pos = 0; pub const Parameter = enum { production_rate, supply_shock, max_inventory, }; pub const DEFAULT_PRODUCTION_RATE: u32 = 300; pub const DEFAULT_MAX_INVENTORY: u32 = 10000; pub const Args = struct { absolute_home: [2]i32, home: [2]f32, color: [4]f32 = .{ 1, 1, 1, 0 }, production_rate: u32 = DEFAULT_PRODUCTION_RATE, inventory: i32 = 0, max_inventory: u32 = DEFAULT_MAX_INVENTORY, price: u32 = 1, }; pub fn generateBulk(demo: *DemoState, num: u32) void { var i: usize = 0; while (i < num) { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); createAndAppend(demo.gctx, .{ .obj_buf = &demo.buffers.data.producers, .producer = .{ .absolute_home = .{ x, y }, .home = [2]f32{ @as(f32, @floatFromInt(x)) * demo.aspect, @as(f32, @floatFromInt(y)), }, .production_rate = demo.params.production_rate.slider.val, .inventory = @as(i32, @intCast(demo.params.max_inventory.slider.val)), .max_inventory = demo.params.max_inventory.slider.val, .price = demo.params.price.slider.val, }, }); i += 1; } } pub const AppendArgs = struct { producer: Args, obj_buf: *Wgpu.ObjectBuffer(Self), }; pub fn createAndAppend(gctx: *zgpu.GraphicsContext, args: AppendArgs) void { const abs_home = args.producer.absolute_home; const home = args.producer.home; var producers: [1]Self = .{ .{ .absolute_home = .{ abs_home[0], abs_home[1], z_pos, 1 }, .home = .{ home[0], home[1], z_pos, 1 }, .color = args.producer.color, .production_rate = args.producer.production_rate, .inventory = args.producer.inventory, .max_inventory = args.producer.max_inventory, .price = args.producer.price, }, }; Wgpu.appendBuffer(gctx, Self, .{ .num_old_structs = args.obj_buf.mapping.num_structs, .buf = args.obj_buf.buf, .structs = producers[0..], }); args.obj_buf.mapping.num_structs += 1; }
0
repos/simulations/src
repos/simulations/src/variable/circle.zig
const std = @import("std"); const math = std.math; const zgpu = @import("zgpu"); const Self = @This(); position: [4]f32, color: [4]f32, radius: f32, pub fn createIndexBuffer(gctx: *zgpu.GraphicsContext, comptime num_vertices: u32) zgpu.BufferHandle { const num_triangles = num_vertices - 1; const consumer_index_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .index = true }, .size = num_triangles * 3 * @sizeOf(u32), }); const num_indices = num_triangles * 3; var indices: [num_indices]u32 = undefined; var i: usize = 0; while (i < num_triangles) { indices[i * 3] = 0; indices[i * 3 + 1] = @as(u32, @intCast(i)) + 1; indices[i * 3 + 2] = @as(u32, @intCast(i)) + 2; i += 1; } indices[num_indices - 1] = 1; gctx.queue.writeBuffer(gctx.lookupResource(consumer_index_buffer).?, 0, u32, indices[0..]); return consumer_index_buffer; } pub fn createVertexBuffer( gctx: *zgpu.GraphicsContext, comptime num_vertices: u32, radius: f32, ) zgpu.BufferHandle { const consumer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = num_vertices * @sizeOf(f32) * 3, }); var consumer_vertex_data: [num_vertices][3]f32 = undefined; const num_sides = @as(f32, num_vertices - 1); const angle = 2 * math.pi / num_sides; consumer_vertex_data[0] = [3]f32{ 0, 0, 0 }; var i: u32 = 1; while (i < num_vertices) { const current_angle = angle * @as(f32, @floatFromInt(i)); const x = @cos(current_angle) * radius; const y = @sin(current_angle) * radius; consumer_vertex_data[i] = [3]f32{ x, y, 0 }; i += 1; } gctx.queue.writeBuffer(gctx.lookupResource(consumer_vertex_buffer).?, 0, [3]f32, consumer_vertex_data[0..]); return consumer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/variable/consumer.zig
const std = @import("std"); const math = std.math; const array = std.ArrayList; const Allocator = std.mem.Allocator; const random = std.crypto.random; const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Main = @import("main.zig"); const DemoState = Main.DemoState; const Parameters = Main.Parameters; const Wgpu = @import("wgpu.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Self = @This(); absolute_home: [4]i32 = .{ 0, 0, 0, 0 }, position: [4]f32 = .{ 0, 0, 0, 0 }, home: [4]f32 = .{ 0, 0, 0, 0 }, destination: [4]f32 = .{ 0, 0, 0, 0 }, color: [4]f32 = .{ 1, 0, 0, 0 }, step_size: [2]f32 = .{ 0, 0 }, radius: f32 = 20.0, inventory: u32 = 0, balance: u32 = 0, max_balance: u32 = 100000, producer_id: i32 = -1, grouping_id: u32 = 0, pub const Params = struct { moving_rate: f32 = 0, max_demand_rate: u32 = 0, income: u32 = 0, }; pub const z_pos = 0; pub fn generateBulk(demo: *DemoState, num: u32) void { for (0..num) |_| { const c = createNewConsumer(demo); appendConsumer(demo, c); } } pub fn createNewConsumer(demo: *DemoState) Self { const x = random.intRangeAtMost(i32, Camera.MIN_X, Camera.MAX_X); const y = random.intRangeAtMost(i32, Camera.MIN_Y, Camera.MAX_Y); const f_x = @as(f32, @floatFromInt(x)) * demo.aspect; const f_y = @as(f32, @floatFromInt(y)); const home = [4]f32{ f_x, f_y, z_pos, 1 }; return .{ .absolute_home = .{ x, y, z_pos, 1 }, .position = home, .home = home, .destination = home, }; } pub fn appendConsumer(demo: *DemoState, c: Self) void { const obj_buf = &demo.buffers.data.consumers; var consumers: [1]Self = .{c}; Wgpu.appendBuffer(demo.gctx, Self, .{ .num_old_structs = obj_buf.mapping.num_structs, .buf = obj_buf.buf, .structs = consumers[0..], }); obj_buf.mapping.num_structs += 1; } pub fn setParamsBuf(demo: *DemoState, mr: f32, mdr: u32, income: u32) void { const r = demo.gctx.lookupResource(demo.buffers.data.consumer_params).?; demo.gctx.queue.writeBuffer(r, 0, f32, &.{mr}); demo.gctx.queue.writeBuffer(r, 4, u32, &.{ mdr, income }); }
0
repos/simulations/src
repos/simulations/src/variable/build.zig
const std = @import("std"); const Options = @import("../../../build.zig").Options; pub fn build(b: *std.Build, options: Options) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = "Simulations", .root_source_file = b.path("src/resources/variable/main.zig"), .target = options.target, .optimize = options.optimize, }); @import("system_sdk").addLibraryPathsTo(exe); const zglfw = b.dependency("zglfw", .{ .target = options.target, }); exe.root_module.addImport("zglfw", zglfw.module("root")); exe.linkLibrary(zglfw.artifact("glfw")); @import("zgpu").addLibraryPathsTo(exe); const zgpu = b.dependency("zgpu", .{ .target = options.target, }); exe.root_module.addImport("zgpu", zgpu.module("root")); exe.linkLibrary(zgpu.artifact("zdawn")); const zmath = b.dependency("zmath", .{ .target = options.target, }); exe.root_module.addImport("zmath", zmath.module("root")); const zgui = b.dependency("zgui", .{ .target = options.target, .backend = .glfw_wgpu, }); exe.root_module.addImport("zgui", zgui.module("root")); exe.linkLibrary(zgui.artifact("imgui")); const zpool = b.dependency("zpool", .{ .target = options.target, }); exe.root_module.addImport("zpool", zpool.module("root")); const zstbi = b.dependency("zstbi", .{ .target = options.target, }); exe.root_module.addImport("zstbi", zstbi.module("root")); exe.linkLibrary(zstbi.artifact("zstbi")); const install_content_step = b.addInstallDirectory(.{ .source_dir = b.path("content"), .install_dir = .{ .custom = "" }, .install_subdir = "bin/content", }); exe.step.dependOn(&install_content_step.step); return exe; } inline fn thisDir() []const u8 { return comptime std.fs.path.dirname(@src().file) orelse "."; }
0
repos/simulations/src
repos/simulations/src/variable/gui.zig
const std = @import("std"); const random = std.crypto.random; const zgpu = @import("zgpu"); const zgui = @import("zgui"); const wgpu = zgpu.wgpu; const Main = @import("main.zig"); const DemoState = Main.DemoState; const Statistics = @import("statistics.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Wgpu = @import("wgpu.zig"); const Circle = @import("circle.zig"); const Callbacks = @import("callbacks.zig"); pub const Pos = struct { x: f32, y: f32, margin: struct { percent: f32 = 0.02, top: bool = true, bottom: bool = true, left: bool = true, right: bool = true, } = .{}, }; pub fn Slider(comptime T: type) type { return struct { min: T, val: T, prev: T, max: T, pub fn init(min: T, val: T, max: T) Slider(T) { return .{ .min = min, .val = val, .prev = val, .max = max }; } }; } pub fn Variable(comptime T: type) type { return struct { slider: Slider(T), wave: Wave, pub fn init( alloc: std.mem.Allocator, min: f64, mid: f64, max: f64, margin: f64, ratio: f64, variable: bool, ) Variable(T) { const dist_to_min = mid - min; const dist_to_max = max - mid; const wave_amplitude = @min(dist_to_min, dist_to_max); const scale = max / 1000; const wave_margin = margin * scale; var wave_max = mid + wave_amplitude - wave_margin; if (wave_max <= 0) wave_max += wave_margin; var s_min: T = undefined; var s_mid: T = undefined; var s_max: T = undefined; switch (T) { f32, f64 => { s_min = @floatCast(min); s_mid = @floatCast(mid); s_max = @floatCast(max); }, else => { s_min = @intFromFloat(min); s_mid = @intFromFloat(mid); s_max = @intFromFloat(max); }, } return .{ .slider = Slider(T).init(s_min, s_mid, s_max), .wave = Wave.init(alloc, mid, wave_max, ratio, scale, variable), }; } }; } pub const Wave = struct { active: bool, scale: f64, mid: f64, max: f64, scaled_max: f64, scaled_diff: f64, scaled_mid: f64, radian_ratio: f64, x_max: f64, xv: std.ArrayList(f64), yv: std.ArrayList(f64), pub fn init( alloc: std.mem.Allocator, mid: f64, max: f64, ratio: f64, scale: f64, active: bool, ) Wave { var xv = std.ArrayList(f64).init(alloc); var yv = std.ArrayList(f64).init(alloc); createXValues(&xv); createYValues(&yv, mid, max, ratio, scale); const scaled_max = max / scale; const scaled_mid = mid / scale; const scaled_diff = scaled_max - scaled_mid; return .{ .active = active, .mid = mid, .max = max, .scaled_max = scaled_max, .scaled_mid = scaled_mid, .scaled_diff = scaled_diff, .scale = scale, .radian_ratio = ratio, .x_max = (std.math.pi / 2.0) / ratio, .xv = xv, .yv = yv, }; } pub fn deinit(self: *Wave) void { self.xv.deinit(); self.yv.deinit(); } }; const GuiFn = *const fn (demo: *DemoState) void; pub const Agent = enum { consumer, producer, custom }; pub const SliderInfo = struct { title: [:0]const u8, field: [:0]const u8, scale: [:0]const u8 = "1", help: ?[:0]const u8 = null, agent: union(Agent) { consumer: bool, producer: bool, custom: GuiFn, }, }; pub const Variables = struct { num_consumers: SliderInfo = .{ .title = "Number of Consumers", .scale = "10", .field = "num_consumers", .agent = .{ .custom = numConsumerUpdate }, }, num_producers: SliderInfo = .{ .title = "Number of Producers", .scale = "0.1", .field = "num_producers", .agent = .{ .custom = numProducersUpdate }, }, max_demand_rate: SliderInfo = .{ .title = "Max Demand Rate", .help = "The maximum amount consumers will buy from producers " ++ "if they have enough money.", .field = "max_demand_rate", .scale = "0.3", .agent = .{ .consumer = true }, }, consumer_income: SliderInfo = .{ .title = "Consumer Income", .help = "How much consumers earn each frame", .field = "income", .scale = "0.5", .agent = .{ .consumer = true }, }, consumer_size: SliderInfo = .{ .title = "Consumer Size", .field = "consumer_size", .scale = "0.003", .agent = .{ .custom = consumerSize }, }, price: SliderInfo = .{ .title = "Resource Price", .help = "The cost a consumer must pay to buy 1 resource item.", .field = "price", .scale = "1", .agent = .{ .producer = true }, }, production_rate: SliderInfo = .{ .title = "Production Rate", .help = "How many resources a producer creates each cycle.", .field = "production_rate", .scale = "1", .agent = .{ .producer = true }, }, max_producer_inventory: SliderInfo = .{ .title = "Max Producer Inventory", .help = "The maximum amount of resources a producer can hold", .field = "max_inventory", .scale = "10", .agent = .{ .producer = true }, }, moving_rate: SliderInfo = .{ .title = "Moving Rate", .help = "How fast consumers move to and from producers", .field = "moving_rate", .scale = "0.02", .agent = .{ .consumer = true }, }, }; pub fn update(demo: *DemoState, selection_gui: *const fn () void) void { setupWindowPos(demo, .{ .x = 0, .y = 0 }); setupWindowSize(demo, .{ .x = 0.25, .y = 0.75 }); const flags = zgui.WindowFlags.no_decoration; if (zgui.begin("0", .{ .flags = flags })) { zgui.pushIntId(0); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); parameters(demo, selection_gui); zgui.popId(); } zgui.end(); var pos: Pos = .{ .x = 0, .y = 0.75, .margin = .{ .top = false } }; var size: Pos = .{ .x = 1, .y = 0.25, .margin = .{ .top = false } }; setupWindowPos(demo, pos); setupWindowSize(demo, size); if (zgui.begin("1", .{ .flags = flags })) { zgui.pushIntId(1); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); plots(demo); zgui.popId(); } zgui.end(); pos = .{ .x = 0.25, .y = 0, .margin = .{ .left = false } }; size = .{ .x = 0.75, .y = 0.75, .margin = .{ .left = false } }; if (demo.timeline_visible) { setupWindowPos(demo, pos); setupWindowSize(demo, size); if (zgui.begin("2", .{ .flags = flags })) { zgui.pushIntId(2); zgui.pushItemWidth(zgui.getContentRegionAvail()[0]); timeline(demo); zgui.popId(); } zgui.end(); } } pub fn updateWaves(demo: *DemoState) void { const sample_idx = demo.params.sample_idx; inline for (@typeInfo(Variables).Struct.fields) |f| { const info = @field(demo.sliders, f.name); const param = &@field(demo.params, info.field); if (param.wave.active) { const num = param.wave.yv.items[sample_idx]; param.slider.prev = param.slider.val; if (@TypeOf(param.slider.val) == f32) { param.slider.val = @floatCast(num * param.wave.scale); } else { param.slider.val = @intFromFloat(num * param.wave.scale); } } } const rad = demo.params.num_consumers.wave.xv.items[sample_idx]; demo.params.radian = rad; demo.params.sample_idx = @mod(sample_idx + 1, SAMPLE_SIZE); } pub fn setupWindowPos(demo: *DemoState, pos: Pos) void { const sd = demo.gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const pos_margin_pixels = getMarginPixels(sd, pos.margin.percent); var x = width * pos.x; if (pos.margin.left) { x += pos_margin_pixels; } var y = height * pos.y; if (pos.margin.top) { y += pos_margin_pixels; } zgui.setNextWindowPos(.{ .x = x, .y = y }); } pub fn setupWindowSize(demo: *DemoState, size: Pos) void { const sd = demo.gctx.swapchain_descriptor; const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const size_margin_pixels = getMarginPixels(sd, size.margin.percent); var w = width * size.x; if (size.margin.left) { w -= size_margin_pixels; } if (size.margin.right) { w -= size_margin_pixels; } var h = height * size.y; if (size.margin.top) { h -= size_margin_pixels; } if (size.margin.bottom) { h -= size_margin_pixels; } zgui.setNextWindowSize(.{ .w = w, .h = h }); } fn getMarginPixels(sd: wgpu.SwapChainDescriptor, margin_percent: f32) f32 { const width = @as(f32, @floatFromInt(sd.width)); const height = @as(f32, @floatFromInt(sd.height)); const margin_x = width * margin_percent; const margin_y = height * margin_percent; return @min(margin_x, margin_y); } pub fn parameters(demo: *DemoState, selection_gui: *const fn () void) void { inline for (@typeInfo(Variables).Struct.fields) |f| { const info = @field(demo.sliders, f.name); const param = &@field(demo.params, info.field); if (param.wave.active) { const slider_type = @TypeOf(param.slider.val); checkIfSliderUpdated(demo, slider_type, info); } } selection_gui(); if (zgui.beginTabBar("##tab_bar", .{})) { defer zgui.endTabBar(); if (zgui.beginTabItem("Variables", .{})) { defer zgui.endTabItem(); showTimeline(demo); inline for (@typeInfo(Variables).Struct.fields) |f| { const info = @field(demo.sliders, f.name); const param = &@field(demo.params, info.field); if (param.wave.active) { infoTitle(demo, info); const slider_type = @TypeOf(param.slider.val); displaySlider(demo, slider_type, &param.slider, info); } } } if (zgui.beginTabItem("Constants", .{})) { defer zgui.endTabItem(); showTimeline(demo); inline for (@typeInfo(Variables).Struct.fields) |f| { const info = @field(demo.sliders, f.name); const param = &@field(demo.params, info.field); if (!param.wave.active) { infoTitle(demo, info); const slider_type = @TypeOf(param.slider.val); displaySlider(demo, slider_type, &param.slider, info); } } } } buttons(demo); } fn showTimeline(demo: *DemoState) void { if (zgui.button("Show Timeline", .{})) { demo.timeline_visible = !demo.timeline_visible; } } pub fn timeline(demo: *DemoState) void { const size = zgui.getWindowSize(); const margin = 15; const plot_size = .{ .w = size[0] - margin, .h = size[1] - margin }; if (zgui.plot.beginPlot("", plot_size)) { defer zgui.plot.endPlot(); const flags = .{ .label = "", .flags = .{ .auto_fit = true } }; zgui.plot.setupAxis(.x1, flags); zgui.plot.setupAxis(.y1, flags); const location_flags = .{ .north = true, .east = true }; const legend_flags = .{ .no_buttons = true }; zgui.plot.setupLegend(location_flags, legend_flags); inline for (@typeInfo(Variables).Struct.fields, 0..) |f, i| { const info = @field(demo.sliders, f.name); const param = &@field(demo.params, info.field); if (param.wave.active) { plotWave(&param.wave, info); dragTopPoint(demo, info, i); dragMidPoint(demo, info, i); } } plotRadianLine(demo.params.radian); } } fn plotWave(wave: *Wave, comptime info: SliderInfo) void { const values = .{ .xv = wave.xv.items, .yv = wave.yv.items }; zgui.plot.plotLine(info.title ++ " x " ++ info.scale, f64, values); } fn plotRadianLine(radian: f64) void { const x1x2 = .{ radian, radian }; const y1y2 = .{ 0, 1000 }; //_reserved0 hides line from legend const line = .{ .xv = &x1x2, .yv = &y1y2, .flags = .{ ._reserved0 = true } }; zgui.plot.plotLine("Vertical Line", f64, line); } fn lightenColor(color: [4]f32, amount: f32) [4]f32 { return .{ color[0] + amount, color[1] + amount, color[2] + amount, color[3], }; } fn dragTopPoint(demo: *DemoState, comptime info: SliderInfo, id: i32) void { const param = &@field(demo.params, info.field); var color = zgui.plot.getLastItemColor(); color = lightenColor(color, 0.3); const wave = &param.wave; const flags = .{ .x = &wave.x_max, .y = &wave.scaled_max, .col = color, .size = 4 * demo.content_scale, }; if (zgui.plot.dragPoint(id, flags)) { const scaled_diff = wave.scaled_max - wave.scaled_mid; const scaled_min = wave.scaled_mid - scaled_diff; const min_outside = scaled_min < 0 or scaled_min > 1000; const max_outside = wave.scaled_max < 0 or wave.scaled_max > 1000; if (min_outside or max_outside) { wave.scaled_max = wave.max / wave.scale; wave.x_max = (std.math.pi / 2.0) / wave.radian_ratio; return; } wave.max = wave.scaled_max * wave.scale; const ratio = (std.math.pi / 2.0) / wave.x_max; wave.radian_ratio = ratio; wave.scaled_diff = scaled_diff; createYValues( &wave.yv, wave.mid, wave.max, wave.radian_ratio, wave.scale, ); } } fn dragMidPoint(demo: *DemoState, comptime info: SliderInfo, id: i32) void { const param = &@field(demo.params, info.field); var color = zgui.plot.getLastItemColor(); color = lightenColor(color, 0.3); var zero: f64 = 0; const wave = &param.wave; const flags = .{ .x = &zero, .y = &wave.scaled_mid, .col = color, .size = 4 * demo.content_scale, }; if (zgui.plot.dragPoint(id + 1000, flags)) { const scaled_max = wave.scaled_mid + wave.scaled_diff; const scaled_min = wave.scaled_mid - wave.scaled_diff; const min_outside = scaled_min < 0 or scaled_min > 1000; const max_outside = scaled_max < 0 or scaled_max > 1000; if (min_outside or max_outside) { wave.scaled_mid = wave.scaled_max - wave.scaled_diff; return; } wave.mid = wave.scaled_mid * wave.scale; wave.max = scaled_max * wave.scale; wave.scaled_max = scaled_max; createYValues( &wave.yv, wave.mid, wave.max, wave.radian_ratio, wave.scale, ); } } fn checkIfSliderUpdated(demo: *DemoState, comptime T: type, comptime info: SliderInfo) void { const param = &@field(demo.params, info.field); const value_changed = param.slider.prev != param.slider.val; if (value_changed) { switch (info.agent) { .consumer => { const offset: u32 = @intCast(std.meta.fieldIndex(Consumer.Params, info.field).?); demo.gctx.queue.writeBuffer( demo.gctx.lookupResource(demo.buffers.data.consumer_params).?, offset * @sizeOf(u32), T, &.{param.slider.val}, ); }, .producer => Wgpu.setObjBufField( demo.gctx, Producer, info.field, param.slider.val, demo.buffers.data.producers, ), .custom => |func| func(demo), } param.slider.prev = param.slider.val; } } fn displaySlider( demo: *DemoState, comptime T: type, slider: *Slider(T), comptime info: SliderInfo, ) void { const flags = .{ .v = &slider.val, .min = slider.min, .max = slider.max }; const slider_changed = zgui.sliderScalar("##" ++ info.title, T, flags); if (slider_changed) { switch (info.agent) { .consumer => { const offset: u32 = @intCast(std.meta.fieldIndex(Consumer.Params, info.field).?); demo.gctx.queue.writeBuffer( demo.gctx.lookupResource(demo.buffers.data.consumer_params).?, offset * @sizeOf(u32), T, &.{slider.val}, ); }, .producer => { Wgpu.setObjBufField( demo.gctx, Producer, info.field, slider.val, demo.buffers.data.producers, ); }, .custom => |func| func(demo), } slider.prev = slider.val; } } fn consumerSize(demo: *DemoState) void { demo.buffers.vertex.circle = Circle.createVertexBuffer( demo.gctx, 40, demo.params.consumer_size.slider.val, ); } fn numConsumers(demo: *DemoState) void { zgui.text("Number Of Consumers", .{}); const param = &demo.params.num_consumers; const new: *u32 = @ptrCast(&param.val); const flags = .{ .v = new, .min = param.min, .max = param.max }; _ = zgui.sliderScalar("##nc", u32, flags); numConsumerUpdate(demo); } pub fn numConsumerUpdate(demo: *DemoState) void { const new = demo.params.num_consumers.slider.val; Statistics.setNum(demo, new, .consumers); const old: u32 = demo.buffers.data.consumers.mapping.num_structs; if (old >= new) { const buf = demo.buffers.data.consumers.buf; Wgpu.shrinkBuffer(demo.gctx, buf, Consumer, new); demo.buffers.data.consumers.mapping.num_structs = new; } else { Consumer.generateBulk(demo, new - old); } } fn numProducersUpdate(demo: *DemoState) void { const new = demo.params.num_producers.slider.val; Statistics.setNum(demo, new, .producers); const old: u32 = demo.buffers.data.producers.mapping.num_structs; if (old >= new) { const buf = demo.buffers.data.producers.buf; Wgpu.shrinkBuffer(demo.gctx, buf, Producer, new); demo.buffers.data.producers.mapping.num_structs = new; } else { Producer.generateBulk(demo, new - old); } } fn infoTitle(demo: *DemoState, comptime slide: SliderInfo) void { zgui.text(slide.title, .{}); if (slide.help) |help| { zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted(help); zgui.endTooltip(); } } zgui.sameLine(.{}); if (zgui.button("Switch" ++ "##" ++ slide.field, .{})) { const param = &@field(demo.params, slide.field); param.wave.active = !param.wave.active; } } fn buttons(demo: *DemoState) void { if (zgui.button("Start", .{})) { demo.running = true; } zgui.sameLine(.{}); if (zgui.button("Stop", .{})) { demo.running = false; } zgui.sameLine(.{}); if (zgui.button("Restart", .{})) { demo.running = true; Main.restartSimulation(demo); } if (zgui.button("Supply Shock", .{})) { Wgpu.setObjBufField( demo.gctx, Producer, "inventory", 0, demo.buffers.data.producers, ); } zgui.sameLine(.{}); zgui.textDisabled("(?)", .{}); if (zgui.isItemHovered(.{})) { _ = zgui.beginTooltip(); zgui.textUnformatted("Set all producer inventory to 0."); zgui.endTooltip(); } } pub fn plots(demo: *DemoState) void { const size = zgui.getWindowSize(); const margin = 15; const plot_size = .{ .w = size[0] - margin, .h = size[1] - margin }; if (zgui.plot.beginPlot("", plot_size)) { defer zgui.plot.endPlot(); var y_flags: zgui.plot.AxisFlags = .{ .auto_fit = true }; if (demo.params.plot_hovered) { y_flags = .{ .lock_min = true }; } const x_axis = .{ .label = "", .flags = .{ .auto_fit = true } }; const y_axis = .{ .label = "", .flags = y_flags }; zgui.plot.setupAxis(.x1, x_axis); zgui.plot.setupAxis(.y1, y_axis); zgui.plot.setupLegend(.{ .north = true, .west = true }, .{}); demo.params.plot_hovered = zgui.plot.isPlotHovered(); const stats = demo.stats; const nt = .{ .v = stats.num_transactions.items[0..] }; const ec = .{ .v = stats.num_empty_consumers.items[0..] }; const tpi = .{ .v = stats.num_total_producer_inventory.items[0..] }; const acb = .{ .v = stats.avg_consumer_balance.items[0..] }; zgui.plot.plotLineValues("Transactions", u32, nt); zgui.plot.plotLineValues("Empty Consumers", u32, ec); zgui.plot.plotLineValues("Total Producer Inventory", u32, tpi); zgui.plot.plotLineValues("Average Consumer Balance", u32, acb); } } pub const RADIAN_END: f64 = 8 * std.math.pi; pub const SAMPLE_SIZE: u32 = 2000; pub const RADIAN_INCREMENT: f64 = RADIAN_END / @as(f64, @floatFromInt(SAMPLE_SIZE)); pub fn createXValues(xv: *std.ArrayList(f64)) void { xv.clearAndFree(); var radian: f64 = 0.0; for (0..SAMPLE_SIZE) |_| { xv.append(radian) catch unreachable; radian += RADIAN_INCREMENT; } } pub fn createYValues(yv: *std.ArrayList(f64), mid: f64, max: f64, ratio: f64, scale: f64) void { yv.clearAndFree(); var radian: f64 = 0.0; const diff = (max - mid); for (0..SAMPLE_SIZE) |_| { const y = mid + (diff * @sin(radian * ratio)); yv.append(y / scale) catch unreachable; radian += RADIAN_INCREMENT; } }
0
repos/simulations/src
repos/simulations/src/variable/wgpu.zig
const std = @import("std"); const zgpu = @import("zgpu"); const zm = @import("zmath"); const zems = @import("zems"); const Gctx = zgpu.GraphicsContext; const wgpu = zgpu.wgpu; const DemoState = @import("main.zig").DemoState; const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Camera = @import("camera.zig"); const Statistics = @import("statistics.zig"); const Callbacks = @import("callbacks.zig"); pub const MAX_NUM_STRUCTS = 10000; // A mishmash of Wgpu initialization functions and buffer helpers for an array of generic structs // Data Types pub fn ObjectBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, mapping: MappingBuffer(T), }; } const callback_queue_len: usize = 10; fn MappingBuffer(comptime T: type) type { return struct { buf: zgpu.BufferHandle, insert_idx: usize = 0, remove_idx: usize = 0, requests: [callback_queue_len]struct { func: Callback(T), args: Callbacks.Args(T), } = undefined, staging: StagingBuffer(T), waiting: bool = false, num_structs: u32, }; } fn StagingBuffer(comptime T: type) type { return struct { slice: ?[]const T = null, buffer: wgpu.Buffer = undefined, num_structs: u32, ready: bool = false, }; } fn Callback(comptime T: type) type { return ?*const fn (args: Callbacks.Args(T)) void; } pub const RenderPipelineInfo = struct { pub const Attribute = struct { name: []const u8, type: type, }; vs: [:0]const u8, fs: [:0]const u8, inst_type: type, inst_attrs: []const Attribute, primitive_topology: wgpu.PrimitiveTopology = .triangle_list, }; pub const ComputePipelineInfo = struct { cs: [:0]const u8, entry_point: [:0]const u8, }; pub fn setObjBufField( gctx: *zgpu.GraphicsContext, comptime Object: type, comptime tag: []const u8, value: anytype, obj_buf: ObjectBuffer(Object), ) void { const resource = gctx.lookupResource(obj_buf.buf).?; const fieldI = std.meta.fieldIndex(Object, tag); const fieldType = @typeInfo(Object).Struct.fields[fieldI.?].type; const field_offset = @offsetOf(Object, tag); for (0..obj_buf.mapping.num_structs) |i| { const offset = i * @sizeOf(Object) + field_offset; gctx.queue.writeBuffer(resource, offset, fieldType, &.{value}); } } pub fn GenCallback(comptime T: type) wgpu.BufferMapCallback { return struct { fn callback( status: wgpu.BufferMapAsyncStatus, userdata: ?*anyopaque, ) callconv(.C) void { const usb = @as(*StagingBuffer(T), @ptrCast(@alignCast(userdata))); std.debug.assert(usb.slice == null); if (status == .success) { const data = usb.buffer.getConstMappedRange( T, 0, usb.num_structs, ); if (data) |d| { usb.slice = d; } usb.ready = true; } else { std.log.err("[zgpu] Failed to map buffer (code: {any})\n", .{status}); } } }.callback; } pub fn getAllAsync( demo: *DemoState, comptime T: type, callback: Callback(T), ) void { const buf = switch (T) { u32 => &demo.buffers.data.stats, Consumer => &demo.buffers.data.consumers, Producer => &demo.buffers.data.producers, else => unreachable, }; const map_ptr = &buf.mapping; if (map_ptr.num_structs < 0) return; map_ptr.staging.num_structs = map_ptr.num_structs; map_ptr.requests[map_ptr.insert_idx].func = callback; map_ptr.requests[map_ptr.insert_idx].args = .{ .gctx = demo.gctx, .buf = buf, .stats = &demo.stats }; map_ptr.insert_idx = (map_ptr.insert_idx + 1) % callback_queue_len; runMapIfReady(T, map_ptr); } pub fn runMapIfReady(comptime T: type, buf: *MappingBuffer(T)) void { if (!buf.waiting and buf.staging.slice == null and buf.insert_idx != buf.remove_idx) { const gctx = buf.requests[buf.remove_idx].args.gctx; buf.staging.buffer = gctx.lookupResource(buf.buf).?; buf.staging.buffer.mapAsync( .{ .read = true }, 0, @sizeOf(T) * buf.staging.num_structs, GenCallback(T), @as(*anyopaque, @ptrCast(&buf.staging)), ); buf.waiting = true; } } pub fn runCallbackIfReady(comptime T: type, buf: *MappingBuffer(T)) void { const request = buf.requests[buf.remove_idx]; if (buf.waiting and buf.staging.ready) { buf.remove_idx = (buf.remove_idx + 1) % callback_queue_len; buf.staging.buffer.unmap(); request.func.?(request.args); buf.staging.slice = null; buf.waiting = false; buf.staging.ready = false; } } pub fn waitForCallback(comptime T: type, buf: *MappingBuffer(T)) void { while (buf.waiting) { runCallbackIfReady(T, buf); } } pub fn getMappedData(comptime T: type, buf: *MappingBuffer(T)) []T { return @constCast(buf.staging.slice.?[0..buf.staging.num_structs]); } pub fn agentParameters(comptime T: type) type { switch (T) { Consumer => return union(enum) { moving_rate: f32, demand_rate: u32, }, Producer => return union(enum) { production_rate: u32, inventory: i32, max_inventory: u32, }, u32 => return u32, else => unreachable, } } pub fn setArgs(comptime T: type) type { return struct { agents: ObjectBuffer, parameter: agentParameters(T), }; } pub fn writeBuffer( gctx: *zgpu.GraphicsContext, buf: zgpu.BufferHandle, comptime T: type, structs: []T, ) void { gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, T, structs); } pub fn shrinkBuffer( gctx: *Gctx, buf: zgpu.BufferHandle, comptime T: type, new_size: u32, ) void { const all_zero = [_]u8{0} ** 10000000; const buff = gctx.lookupResource(buf).?; const buf_info = gctx.lookupResourceInfo(buf).?; const size_to_keep = @sizeOf(T) * new_size; const size_to_clear = buf_info.size - size_to_keep; const usize_to_clear = @as(usize, @intCast(size_to_clear)); gctx.queue.writeBuffer( buff, size_to_keep, u8, all_zero[0..usize_to_clear], ); } pub fn appendArgs(comptime T: type) type { return struct { num_old_structs: u32, buf: zgpu.BufferHandle, structs: []T, }; } pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void { gctx.queue.writeBuffer( gctx.lookupResource(args.buf).?, args.num_old_structs * @sizeOf(T), T, args.structs, ); } pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(buf).?, 0, u8, all_zero[0..b_size], ); } pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void { const all_zero = [_]u8{0} ** 10000000; const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?; const b_size = @as(usize, @intCast(buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.buf).?, 0, u8, all_zero[0..b_size], ); const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?; const m_size = @as(usize, @intCast(map_buf_info.size)); gctx.queue.writeBuffer( gctx.lookupResource(obj_buf.mapping.buf).?, 0, u8, all_zero[0..m_size], ); obj_buf.mapping.insert_idx = 0; obj_buf.mapping.remove_idx = 0; obj_buf.mapping.waiting = false; obj_buf.mapping.staging.ready = false; obj_buf.mapping.staging.slice = null; obj_buf.mapping.num_structs = 0; obj_buf.mapping.staging.num_structs = 0; } // Blank Buffers pub fn createBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .copy_src = true, .vertex = true, .storage = true }, .size = num * @sizeOf(T), }); } pub fn createMappedBuffer( gctx: *Gctx, comptime T: type, num: u32, ) zgpu.BufferHandle { return gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .map_read = true }, .size = num * @sizeOf(T), }); } pub fn createObjectBuffer( gctx: *Gctx, comptime T: type, len: u32, num_structs: u32, ) ObjectBuffer(T) { return .{ .buf = createBuffer(gctx, T, len), .mapping = .{ .buf = createMappedBuffer(gctx, T, len), .num_structs = num_structs, .staging = .{ .num_structs = num_structs, }, }, }; } // Depth Texture pub const Depth = struct { texture: zgpu.TextureHandle, view: zgpu.TextureViewHandle, }; pub fn createDepthTexture(gctx: *zgpu.GraphicsContext) Depth { const texture = gctx.createTexture(.{ .usage = .{ .render_attachment = true }, .dimension = .tdim_2d, .size = .{ .width = gctx.swapchain_descriptor.width, .height = gctx.swapchain_descriptor.height, .depth_or_array_layers = 1, }, .format = .depth32_float, .mip_level_count = 1, .sample_count = 1, }); const view = gctx.createTextureView(texture, .{}); return .{ .texture = texture, .view = view }; } // Bind Group Layouts pub fn createUniformBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .vertex = true }, .uniform, true, 0), }); } pub fn createComputeBindGroupLayout(gctx: *Gctx) zgpu.BindGroupLayoutHandle { return gctx.createBindGroupLayout(&.{ zgpu.bufferEntry(0, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(1, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(2, .{ .compute = true }, .storage, false, 0), zgpu.bufferEntry(3, .{ .compute = true }, .storage, false, 0), }); } // Bind Groups pub fn createUniformBindGroup(gctx: *Gctx) zgpu.BindGroupHandle { const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); return gctx.createBindGroup(bind_group_layout, &.{ .{ .binding = 0, .buffer_handle = gctx.uniforms.buffer, .offset = 0, .size = @sizeOf(zm.Mat) }, }); } pub const computeBindGroup = struct { consumer: zgpu.BufferHandle, consumer_params: zgpu.BufferHandle, producer: zgpu.BufferHandle, stats: zgpu.BufferHandle, }; pub fn createComputeBindGroup(gctx: *Gctx, args: computeBindGroup) zgpu.BindGroupHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const c_info = gctx.lookupResourceInfo(args.consumer) orelse unreachable; const cp_info = gctx.lookupResourceInfo(args.consumer_params) orelse unreachable; const p_info = gctx.lookupResourceInfo(args.producer) orelse unreachable; const s_info = gctx.lookupResourceInfo(args.stats) orelse unreachable; return gctx.createBindGroup(compute_bgl, &[_]zgpu.BindGroupEntryInfo{ .{ .binding = 0, .buffer_handle = args.consumer, .offset = 0, .size = c_info.size, }, .{ .binding = 1, .buffer_handle = args.consumer_params, .offset = 0, .size = cp_info.size, }, .{ .binding = 2, .buffer_handle = args.producer, .offset = 0, .size = p_info.size, }, .{ .binding = 3, .buffer_handle = args.stats, .offset = 0, .size = s_info.size, }, }); } fn getWgpuType(comptime T: type) !wgpu.VertexFormat { return switch (T) { u32 => .uint32, f32 => .float32, [2]f32 => .float32x2, [3]f32 => .float32x3, [4]f32 => .float32x4, else => error.NoValidWgpuType, }; } pub fn createRenderPipeline( gctx: *zgpu.GraphicsContext, comptime args: RenderPipelineInfo, ) zgpu.RenderPipelineHandle { const vs_module = zgpu.createWgslShaderModule(gctx.device, args.vs, "vs"); defer vs_module.release(); const fs_module = zgpu.createWgslShaderModule(gctx.device, args.fs, "fs"); defer fs_module.release(); const color_targets = [_]wgpu.ColorTargetState{.{ .format = zgpu.GraphicsContext.swapchain_format, .blend = &.{ .color = .{}, .alpha = .{} }, }}; const vertex_attributes = [_]wgpu.VertexAttribute{ .{ .format = .float32x3, .offset = 0, .shader_location = 0 }, }; const instance_attributes = init: { var arr: [args.inst_attrs.len]wgpu.VertexAttribute = undefined; inline for (args.inst_attrs, 0..) |attr, i| { arr[i] = .{ .format = getWgpuType(attr.type) catch unreachable, .offset = @offsetOf(args.inst_type, attr.name), .shader_location = i + 1, }; } break :init arr; }; const vertex_buffers = [_]wgpu.VertexBufferLayout{ .{ .array_stride = @sizeOf(f32) * 3, .attribute_count = vertex_attributes.len, .attributes = &vertex_attributes, .step_mode = .vertex, }, .{ .array_stride = @sizeOf(args.inst_type), .attribute_count = instance_attributes.len, .attributes = &instance_attributes, .step_mode = .instance, }, }; const pipeline_descriptor = wgpu.RenderPipelineDescriptor{ .vertex = wgpu.VertexState{ .module = vs_module, .entry_point = "main", .buffer_count = vertex_buffers.len, .buffers = &vertex_buffers, }, .primitive = wgpu.PrimitiveState{ .front_face = .ccw, .cull_mode = .none, .topology = args.primitive_topology, }, .depth_stencil = &wgpu.DepthStencilState{ .format = .depth32_float, .depth_write_enabled = true, .depth_compare = .less_equal, }, .fragment = &wgpu.FragmentState{ .module = fs_module, .entry_point = "main", .target_count = color_targets.len, .targets = &color_targets, }, }; const bind_group_layout = createUniformBindGroupLayout(gctx); defer gctx.releaseResource(bind_group_layout); const pipeline_layout = gctx.createPipelineLayout(&.{bind_group_layout}); return gctx.createRenderPipeline(pipeline_layout, pipeline_descriptor); } pub fn createComputePipeline(gctx: *zgpu.GraphicsContext, cpi: ComputePipelineInfo) zgpu.ComputePipelineHandle { const compute_bgl = createComputeBindGroupLayout(gctx); defer gctx.releaseResource(compute_bgl); const compute_pl = gctx.createPipelineLayout(&.{compute_bgl}); defer gctx.releaseResource(compute_pl); const cs_module = zgpu.createWgslShaderModule(gctx.device, cpi.cs, "cs"); defer cs_module.release(); const pipeline_descriptor = wgpu.ComputePipelineDescriptor{ .compute = wgpu.ProgrammableStageDescriptor{ .module = cs_module, .entry_point = cpi.entry_point, }, }; return gctx.createComputePipeline(compute_pl, pipeline_descriptor); }
0
repos/simulations/src
repos/simulations/src/variable/square.zig
const zgpu = @import("zgpu"); pub fn createVertexBuffer(gctx: *zgpu.GraphicsContext, width: f32) zgpu.BufferHandle { const producer_vertex_buffer = gctx.createBuffer(.{ .usage = .{ .copy_dst = true, .vertex = true }, .size = 6 * @sizeOf(f32) * 3, }); const upper_left = [3]f32{ -width, width, 0.0 }; const lower_left = [3]f32{ -width, -width, 0.0 }; const upper_right = [3]f32{ width, width, 0.0 }; const lower_right = [3]f32{ width, -width, 0.0 }; const vertex_array = [6][3]f32{ upper_left, lower_left, lower_right, lower_right, upper_right, upper_left, }; gctx.queue.writeBuffer(gctx.lookupResource(producer_vertex_buffer).?, 0, [3]f32, vertex_array[0..]); return producer_vertex_buffer; }
0
repos/simulations/src
repos/simulations/src/variable/callbacks.zig
const std = @import("std"); const zgpu = @import("zgpu"); const wgpu = zgpu.wgpu; const Camera = @import("camera.zig"); const Consumer = @import("consumer.zig"); const Producer = @import("producer.zig"); const Statistics = @import("statistics.zig"); const Wgpu = @import("wgpu.zig"); pub fn Args(comptime T: type) type { return struct { gctx: *zgpu.GraphicsContext, buf: *Wgpu.ObjectBuffer(T), stats: *Statistics = undefined, }; } pub fn numTransactions(args: Args(u32)) void { const slice = args.buf.mapping.staging.slice; var num: u32 = 0; if (slice) |stats| { num = stats[0]; } args.stats.num_transactions.append(num) catch unreachable; Statistics.clearNumTransactions(args.gctx, args.buf.buf); } pub fn totalInventory(args: Args(Producer)) void { const slice = args.buf.mapping.staging.slice; var total_inventory: u32 = 0; if (slice) |producers| { for (producers) |p| { total_inventory += @as(u32, @intCast(p.inventory)); } } args.stats.num_total_producer_inventory.append(total_inventory) catch unreachable; } pub fn consumerStats(args: Args(Consumer)) void { const slice = args.buf.mapping.staging.slice; var empty_consumers: u32 = 0; if (slice) |consumers| { for (consumers) |c| { if (c.inventory == 0) { empty_consumers += 1; } } } args.stats.num_empty_consumers.append(empty_consumers) catch unreachable; var total_balance: u32 = 0; if (slice) |consumers| { for (consumers) |c| { total_balance += c.balance; } } const len: u32 = @intCast(args.buf.mapping.num_structs + 1); const avg_balance = total_balance / len; args.stats.avg_consumer_balance.append(avg_balance) catch unreachable; } pub fn updateProducerCoords(args: Args(Producer)) void { const slice = args.buf.mapping.staging.slice; if (slice) |producers| { for (producers, 0..) |p, i| { const new_coord = Camera.getWorldPosition(args.gctx, p.absolute_home); const buf = args.gctx.lookupResource(args.buf.buf).?; const offset = i * @sizeOf(Producer) + @offsetOf(Producer, "home"); args.gctx.queue.writeBuffer(buf, offset, [4]f32, &.{new_coord}); } } } pub fn updateConsumerCoords(args: Args(Consumer)) void { const slice = args.buf.mapping.staging.slice; if (slice) |consumers| { for (consumers, 0..) |c, i| { const new_coord = Camera.getWorldPosition(args.gctx, c.absolute_home); const buf = args.gctx.lookupResource(args.buf.buf).?; var offset = i * @sizeOf(Consumer) + @offsetOf(Consumer, "home"); args.gctx.queue.writeBuffer(buf, offset, [4]f32, &.{new_coord}); offset = i * @sizeOf(Consumer) + @offsetOf(Consumer, "position"); args.gctx.queue.writeBuffer(buf, offset, [4]f32, &.{new_coord}); offset = i * @sizeOf(Consumer) + @offsetOf(Consumer, "destination"); args.gctx.queue.writeBuffer(buf, offset, [4]f32, &.{new_coord}); } } }
0
repos/simulations/src/variable
repos/simulations/src/variable/shaders/shaders.zig
// zig fmt: off pub const vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) demand_rate: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(demand_rate); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (vertex_position[0] * scale); \\ var y = position[1] + (vertex_position[1] * scale); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const producer_vs = \\ @group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; \\ struct VertexOut { \\ @builtin(position) position_clip: vec4<f32>, \\ @location(0) color: vec3<f32>, \\ } \\ @vertex fn main( \\ @location(0) vertex_position: vec3<f32>, \\ @location(1) position: vec4<f32>, \\ @location(2) color: vec4<f32>, \\ @location(3) inventory: u32, \\ @location(4) max_inventory: u32, \\ ) -> VertexOut { \\ var output: VertexOut; \\ let num = f32(inventory) / f32(max_inventory); \\ let scale = min(max(num, 0.4), 1.0); \\ var x = position[0] + (scale * vertex_position[0]); \\ var y = position[1] + (scale * vertex_position[1]); \\ output.position_clip = vec4(x, y, 0.0, 1.0) * object_to_clip; \\ output.color = color.xyz; \\ return output; \\ } ; pub const fs = \\ @stage(fragment) fn main( \\ @location(0) color: vec3<f32>, \\ ) -> @location(0) vec4<f32> { \\ return vec4(color, 1.0); \\ } ; pub const cs = \\ struct Consumer { \\ position: vec4<f32>, \\ home: vec4<f32>, \\ absolute_home: vec4<f32>, \\ destination: vec4<f32>, \\ step_size: vec4<f32>, \\ color: vec4<f32>, \\ moving_rate: f32, \\ demand_rate: u32, \\ inventory: u32, \\ radius: f32, \\ producer_id: i32, \\ } \\ struct Producer { \\ position: vec4<f32>, \\ absolute_pos: vec4<f32>, \\ color: vec4<f32>, \\ production_rate: u32, \\ inventory: atomic<u32>, \\ max_inventory: u32, \\ len: atomic<u32>, \\ queue: array<u32, 480>, \\ } \\ struct Stats { \\ transactions: u32, \\ } \\ \\ @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; \\ @group(0) @binding(1) var<storage, read_write> producers: array<Producer>; \\ @group(0) @binding(2) var<storage, read_write> stats: Stats; \\ @compute @workgroup_size(64) \\ fn consumer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let nc = arrayLength(&consumers); \\ if(GlobalInvocationID.x >= nc) { \\ return; \\ } \\ let c = consumers[index]; \\ consumers[index].position += c.step_size; \\ let dist = abs(c.position - c.destination); \\ let at_destination = all(dist.xy <= vec2<f32>(0.1)); \\ \\ if (at_destination) { \\ var new_destination = vec4<f32>(0); \\ let at_home = all(c.destination == c.home); \\ if (at_home) { \\ consumers[index].position = c.home; \\ let consumption_rate = 1u; \\ if (c.inventory >= consumption_rate) { \\ consumers[index].inventory -= consumption_rate; \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ return; \\ } \\ consumers[index].color = vec4(1.0, 0.0, 0.0, 0.0); \\ var closest_producer = vec4(10000.0, 10000.0, 0.0, 0.0); \\ var shortest_distance = 100000.0; \\ var array_len = i32(arrayLength(&producers)); \\ for(var i = 0; i < array_len; i++){ \\ let dist = distance(c.home, producers[i].position); \\ let inventory = atomicLoad(&producers[i].inventory); \\ if (dist < shortest_distance && inventory > c.demand_rate) { \\ shortest_distance = dist; \\ consumers[index].destination = producers[i].position; \\ consumers[index].step_size = step_sizes(c.position, producers[i].position, c.moving_rate); \\ consumers[index].producer_id = i; \\ } \\ } \\ if (shortest_distance == 100000.0) { \\ consumers[index].destination = c.home; \\ consumers[index].step_size = vec4<f32>(0); \\ } \\ } else { \\ let position = c.destination; \\ let pid = c.producer_id; \\ consumers[index].position = position; \\ consumers[index].step_size = vec4<f32>(0); \\ let idx = atomicAdd(&producers[pid].len, 1); \\ producers[pid].queue[idx] = index + 1; \\ } \\ } \\ } \\ fn step_sizes(pos: vec4<f32>, dest: vec4<f32>, mr: f32) -> vec4<f32>{ \\ let x_num_steps = num_steps(pos.x, dest.x, mr); \\ let y_num_steps = num_steps(pos.y, dest.y, mr); \\ let num_steps = max(x_num_steps, y_num_steps); \\ let distance = dest - pos; \\ return distance / num_steps; \\ } \\ fn num_steps(x: f32, y: f32, rate: f32) -> f32 { \\ let distance = abs(x - y); \\ if (rate > distance) { return 1.0; } \\ return ceil(distance / rate); \\ } \\ @compute @workgroup_size(64) \\ fn producer_main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { \\ let index : u32 = GlobalInvocationID.x; \\ let np = arrayLength(&producers); \\ if(GlobalInvocationID.x >= np) { \\ return; \\ } \\ let max_inventory = producers[index].max_inventory; \\ let inventory = atomicLoad(&producers[index].inventory); \\ var production_rate = producers[index].production_rate; \\ if (max_inventory > inventory) { \\ let diff = max_inventory - inventory; \\ production_rate = min(diff, production_rate); \\ let old_val = atomicAdd(&producers[index].inventory, production_rate); \\ } else if (inventory < max_inventory) { \\ atomicStore(&producers[index].inventory, max_inventory); \\ } \\ \\ let idx = atomicLoad(&producers[index].len); \\ for (var i = 0u; i < idx; i++) { \\ let cid = producers[index].queue[i] - 1; \\ let c = consumers[cid]; \\ let inventory = atomicLoad(&producers[index].inventory); \\ if (inventory >= c.demand_rate) { \\ consumers[cid].destination = c.home; \\ consumers[cid].step_size = step_sizes(c.position, c.home, c.moving_rate); \\ consumers[cid].inventory += c.demand_rate; \\ let old_inv = atomicSub(&producers[index].inventory, c.demand_rate); \\ stats.transactions += 1; \\ consumers[cid].color = vec4(0.0, 1.0, 0.0, 0.0); \\ } \\ } \\ atomicStore(&producers[index].len, 0); \\} ; // zig fmt: on
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/fragment/fragment.wgsl
@fragment fn main( @location(0) color: vec3<f32>, ) -> @location(0) vec4<f32> { return vec4(color, 1.0); }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/vertex/hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/vertex/producer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, @location(4) max_inventory: u32, ) -> VertexOut { var output: VertexOut; let num = f32(inventory) / f32(max_inventory); let scale = min(max(num, 0.4), 1.0); var x = position[0] + (scale * vertex_position[0]); var y = position[1] + (scale * vertex_position[1]); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/vertex/consumer.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) color: vec4<f32>, @location(3) inventory: u32, ) -> VertexOut { var output: VertexOut; let scale = max(f32(inventory) / 5, 5.0); var x = position[0] + (vertex_position[0] * scale); var y = position[1] + (vertex_position[1] * scale); output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = color.xyz; return output; }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/vertex/consumer_hover.wgsl
@group(0) @binding(0) var<uniform> object_to_clip: mat4x4<f32>; struct VertexOut { @builtin(position) position_clip: vec4<f32>, @location(0) color: vec3<f32>, } @vertex fn main( @location(0) vertex_position: vec3<f32>, @location(1) position: vec4<f32>, @location(2) hover_color: vec4<f32>, ) -> VertexOut { var output: VertexOut; var x = position[0] + vertex_position[0]; var y = position[1] + vertex_position[1]; output.position_clip = object_to_clip * vec4(x, y, position[2], 1.0); output.color = hover_color.xyz; return output; }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/compute/common.wgsl
struct Consumer { absolute_home: vec4<i32>, position: vec4<f32>, home: vec4<f32>, destination: vec4<f32>, color: vec4<f32>, step_size: vec2<f32>, radius: f32, inventory: u32, balance: u32, max_balance: u32, producer_id: i32, grouping_id: u32, } //Might need padding if something is wonky struct ConsumerParams{ moving_rate: f32, max_demand_rate: u32, income: u32, } struct Producer { absolute_home: vec4<i32>, home: vec4<f32>, color: vec4<f32>, production_rate: u32, inventory: atomic<i32>, max_inventory: u32, price: u32, } struct Stats { transactions: u32, num_consumers: u32, num_producers: u32, num_consumer_hovers: u32, random_color: vec4<f32>, } @group(0) @binding(0) var<storage, read_write> consumers: array<Consumer>; @group(0) @binding(1) var<storage, read_write> consumer_params: ConsumerParams; @group(0) @binding(2) var<storage, read_write> producers: array<Producer>; @group(0) @binding(3) var<storage, read_write> stats: Stats; fn step_sizes(pos: vec2<f32>, dest: vec2<f32>, mr: f32) -> vec2<f32>{ let x_num_steps = num_steps(pos.x, dest.x, mr); let y_num_steps = num_steps(pos.y, dest.y, mr); let num_steps = max(x_num_steps, y_num_steps); let distance = dest - pos; return distance / num_steps; } fn num_steps(x: f32, y: f32, rate: f32) -> f32 { let distance = abs(x - y); if (rate > distance) { return 1.0; } return ceil(distance / rate); }
0
repos/simulations/src/variable/shaders
repos/simulations/src/variable/shaders/compute/producer.wgsl
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { let index : u32 = GlobalInvocationID.x; if(GlobalInvocationID.x >= stats.num_producers) { return; } let max_inventory = i32(producers[index].max_inventory); var production_rate = i32(producers[index].production_rate); let old_inventory = atomicAdd(&producers[index].inventory, production_rate); if (old_inventory + production_rate > max_inventory) { atomicStore(&producers[index].inventory, max_inventory); } }