1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
|
// Copyright Nat Goodspeed 2015.
// 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 <algorithm>
#include <cassert>
#include <chrono>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <boost/fiber/all.hpp>
#include <boost/variant/variant.hpp>
#include <boost/variant/get.hpp>
// These are wait_something() functions rather than when_something()
// functions. A big part of the point of the Fiber library is to model
// sequencing using the processor's instruction pointer rather than chains of
// callbacks. The future-oriented when_all() / when_any() functions are still
// based on chains of callbacks. With Fiber, we can do better.
/*****************************************************************************
* Verbose
*****************************************************************************/
class Verbose {
public:
Verbose( std::string const& d):
desc( d) {
std::cout << desc << " start" << std::endl;
}
~Verbose() {
std::cout << desc << " stop" << std::endl;
}
Verbose( Verbose const&) = delete;
Verbose & operator=( Verbose const&) = delete;
private:
const std::string desc;
};
/*****************************************************************************
* Runner and Example
*****************************************************************************/
// collect and ultimately run every Example
class Runner {
typedef std::vector< std::pair< std::string, std::function< void() > > > function_list;
public:
void add( std::string const& desc, std::function< void() > const& func) {
functions_.push_back( function_list::value_type( desc, func) );
}
void run() {
for ( function_list::value_type const& pair : functions_) {
Verbose v( pair.first);
pair.second();
}
}
private:
function_list functions_;
};
Runner runner;
// Example allows us to embed Runner::add() calls at module scope
struct Example {
Example( Runner & runner, std::string const& desc, std::function< void() > const& func) {
runner.add( desc, func);
}
};
/*****************************************************************************
* example task functions
*****************************************************************************/
//[wait_sleeper
template< typename T >
T sleeper_impl( T item, int ms, bool thrw = false) {
std::ostringstream descb, funcb;
descb << item;
std::string desc( descb.str() );
funcb << " sleeper(" << item << ")";
Verbose v( funcb.str() );
boost::this_fiber::sleep_for( std::chrono::milliseconds( ms) );
if ( thrw) {
throw std::runtime_error( desc);
}
return item;
}
//]
inline
std::string sleeper( std::string const& item, int ms, bool thrw = false) {
return sleeper_impl( item, ms, thrw);
}
inline
double sleeper( double item, int ms, bool thrw = false) {
return sleeper_impl( item, ms, thrw);
}
inline
int sleeper(int item, int ms, bool thrw = false) {
return sleeper_impl( item, ms, thrw);
}
/*****************************************************************************
* Done
*****************************************************************************/
//[wait_done
// Wrap canonical pattern for condition_variable + bool flag
struct Done {
private:
boost::fibers::condition_variable cond;
boost::fibers::mutex mutex;
bool ready = false;
public:
typedef std::shared_ptr< Done > ptr;
void wait() {
std::unique_lock< boost::fibers::mutex > lock( mutex);
cond.wait( lock, [this](){ return ready; });
}
void notify() {
{
std::unique_lock< boost::fibers::mutex > lock( mutex);
ready = true;
} // release mutex
cond.notify_one();
}
};
//]
/*****************************************************************************
* when_any, simple completion
*****************************************************************************/
//[wait_first_simple_impl
// Degenerate case: when there are no functions to wait for, return
// immediately.
void wait_first_simple_impl( Done::ptr) {
}
// When there's at least one function to wait for, launch it and recur to
// process the rest.
template< typename Fn, typename ... Fns >
void wait_first_simple_impl( Done::ptr done, Fn && function, Fns && ... functions) {
boost::fibers::fiber( [done, function](){
function();
done->notify();
}).detach();
wait_first_simple_impl( done, std::forward< Fns >( functions) ... );
}
//]
// interface function: instantiate Done, launch tasks, wait for Done
//[wait_first_simple
template< typename ... Fns >
void wait_first_simple( Fns && ... functions) {
// Use shared_ptr because each function's fiber will bind it separately,
// and we're going to return before the last of them completes.
auto done( std::make_shared< Done >() );
wait_first_simple_impl( done, std::forward< Fns >( functions) ... );
done->wait();
}
//]
// example usage
Example wfs( runner, "wait_first_simple()", [](){
//[wait_first_simple_ex
wait_first_simple(
[](){ sleeper("wfs_long", 150); },
[](){ sleeper("wfs_medium", 100); },
[](){ sleeper("wfs_short", 50); });
//]
});
/*****************************************************************************
* when_any, return value
*****************************************************************************/
// When there's only one function, call this overload
//[wait_first_value_impl
template< typename T, typename Fn >
void wait_first_value_impl( std::shared_ptr< boost::fibers::buffered_channel< T > > chan,
Fn && function) {
boost::fibers::fiber( [chan, function](){
// Ignore channel_op_status returned by push():
// might be closed; we simply don't care.
chan->push( function() );
}).detach();
}
//]
// When there are two or more functions, call this overload
template< typename T, typename Fn0, typename Fn1, typename ... Fns >
void wait_first_value_impl( std::shared_ptr< boost::fibers::buffered_channel< T > > chan,
Fn0 && function0,
Fn1 && function1,
Fns && ... functions) {
// process the first function using the single-function overload
wait_first_value_impl< T >( chan,
std::forward< Fn0 >( function0) );
// then recur to process the rest
wait_first_value_impl< T >( chan,
std::forward< Fn1 >( function1),
std::forward< Fns >( functions) ... );
}
//[wait_first_value
// Assume that all passed functions have the same return type. The return type
// of wait_first_value() is the return type of the first passed function. It is
// simply invalid to pass NO functions.
template< typename Fn, typename ... Fns >
typename std::result_of< Fn() >::type
wait_first_value( Fn && function, Fns && ... functions) {
typedef typename std::result_of< Fn() >::type return_t;
typedef boost::fibers::buffered_channel< return_t > channel_t;
auto chanp( std::make_shared< channel_t >( 64) );
// launch all the relevant fibers
wait_first_value_impl< return_t >( chanp,
std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// retrieve the first value
return_t value( chanp->value_pop() );
// close the channel: no subsequent push() has to succeed
chanp->close();
return value;
}
//]
// example usage
Example wfv( runner, "wait_first_value()", [](){
//[wait_first_value_ex
std::string result = wait_first_value(
[](){ return sleeper("wfv_third", 150); },
[](){ return sleeper("wfv_second", 100); },
[](){ return sleeper("wfv_first", 50); });
std::cout << "wait_first_value() => " << result << std::endl;
assert(result == "wfv_first");
//]
});
/*****************************************************************************
* when_any, produce first outcome, whether result or exception
*****************************************************************************/
// When there's only one function, call this overload.
//[wait_first_outcome_impl
template< typename T, typename CHANP, typename Fn >
void wait_first_outcome_impl( CHANP chan, Fn && function) {
boost::fibers::fiber(
// Use std::bind() here for C++11 compatibility. C++11 lambda capture
// can't move a move-only Fn type, but bind() can. Let bind() move the
// channel pointer and the function into the bound object, passing
// references into the lambda.
std::bind(
[]( CHANP & chan,
typename std::decay< Fn >::type & function) {
// Instantiate a packaged_task to capture any exception thrown by
// function.
boost::fibers::packaged_task< T() > task( function);
// Immediately run this packaged_task on same fiber. We want
// function() to have completed BEFORE we push the future.
task();
// Pass the corresponding future to consumer. Ignore
// channel_op_status returned by push(): might be closed; we
// simply don't care.
chan->push( task.get_future() );
},
chan,
std::forward< Fn >( function)
)).detach();
}
//]
// When there are two or more functions, call this overload
template< typename T, typename CHANP, typename Fn0, typename Fn1, typename ... Fns >
void wait_first_outcome_impl( CHANP chan,
Fn0 && function0,
Fn1 && function1,
Fns && ... functions) {
// process the first function using the single-function overload
wait_first_outcome_impl< T >( chan,
std::forward< Fn0 >( function0) );
// then recur to process the rest
wait_first_outcome_impl< T >( chan,
std::forward< Fn1 >( function1),
std::forward< Fns >( functions) ... );
}
// Assume that all passed functions have the same return type. The return type
// of wait_first_outcome() is the return type of the first passed function. It is
// simply invalid to pass NO functions.
//[wait_first_outcome
template< typename Fn, typename ... Fns >
typename std::result_of< Fn() >::type
wait_first_outcome( Fn && function, Fns && ... functions) {
// In this case, the value we pass through the channel is actually a
// future -- which is already ready. future can carry either a value or an
// exception.
typedef typename std::result_of< Fn() >::type return_t;
typedef boost::fibers::future< return_t > future_t;
typedef boost::fibers::buffered_channel< future_t > channel_t;
auto chanp(std::make_shared< channel_t >( 64) );
// launch all the relevant fibers
wait_first_outcome_impl< return_t >( chanp,
std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// retrieve the first future
future_t future( chanp->value_pop() );
// close the channel: no subsequent push() has to succeed
chanp->close();
// either return value or throw exception
return future.get();
}
//]
// example usage
Example wfo( runner, "wait_first_outcome()", [](){
//[wait_first_outcome_ex
std::string result = wait_first_outcome(
[](){ return sleeper("wfos_first", 50); },
[](){ return sleeper("wfos_second", 100); },
[](){ return sleeper("wfos_third", 150); });
std::cout << "wait_first_outcome(success) => " << result << std::endl;
assert(result == "wfos_first");
std::string thrown;
try {
result = wait_first_outcome(
[](){ return sleeper("wfof_first", 50, true); },
[](){ return sleeper("wfof_second", 100); },
[](){ return sleeper("wfof_third", 150); });
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_first_outcome(fail) threw '" << thrown
<< "'" << std::endl;
assert(thrown == "wfof_first");
//]
});
/*****************************************************************************
* when_any, collect exceptions until success; throw exception_list if no
* success
*****************************************************************************/
// define an exception to aggregate exception_ptrs; prefer
// std::exception_list (N4407 et al.) once that becomes available
//[exception_list
class exception_list : public std::runtime_error {
public:
exception_list( std::string const& what) :
std::runtime_error( what) {
}
typedef std::vector< std::exception_ptr > bundle_t;
// N4407 proposed std::exception_list API
typedef bundle_t::const_iterator iterator;
std::size_t size() const noexcept {
return bundle_.size();
}
iterator begin() const noexcept {
return bundle_.begin();
}
iterator end() const noexcept {
return bundle_.end();
}
// extension to populate
void add( std::exception_ptr ep) {
bundle_.push_back( ep);
}
private:
bundle_t bundle_;
};
//]
// Assume that all passed functions have the same return type. The return type
// of wait_first_success() is the return type of the first passed function. It is
// simply invalid to pass NO functions.
//[wait_first_success
template< typename Fn, typename ... Fns >
typename std::result_of< Fn() >::type
wait_first_success( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
// In this case, the value we pass through the channel is actually a
// future -- which is already ready. future can carry either a value or an
// exception.
typedef typename std::result_of< typename std::decay< Fn >::type() >::type return_t;
typedef boost::fibers::future< return_t > future_t;
typedef boost::fibers::buffered_channel< future_t > channel_t;
auto chanp( std::make_shared< channel_t >( 64) );
// launch all the relevant fibers
wait_first_outcome_impl< return_t >( chanp,
std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// instantiate exception_list, just in case
exception_list exceptions("wait_first_success() produced only errors");
// retrieve up to 'count' results -- but stop there!
for ( std::size_t i = 0; i < count; ++i) {
// retrieve the next future
future_t future( chanp->value_pop() );
// retrieve exception_ptr if any
std::exception_ptr error( future.get_exception_ptr() );
// if no error, then yay, return value
if ( ! error) {
// close the channel: no subsequent push() has to succeed
chanp->close();
// show caller the value we got
return future.get();
}
// error is non-null: collect
exceptions.add( error);
}
// We only arrive here when every passed function threw an exception.
// Throw our collection to inform caller.
throw exceptions;
}
//]
// example usage
Example wfss( runner, "wait_first_success()", [](){
//[wait_first_success_ex
std::string result = wait_first_success(
[](){ return sleeper("wfss_first", 50, true); },
[](){ return sleeper("wfss_second", 100); },
[](){ return sleeper("wfss_third", 150); });
std::cout << "wait_first_success(success) => " << result << std::endl;
assert(result == "wfss_second");
//]
std::string thrown;
std::size_t count = 0;
try {
result = wait_first_success(
[](){ return sleeper("wfsf_first", 50, true); },
[](){ return sleeper("wfsf_second", 100, true); },
[](){ return sleeper("wfsf_third", 150, true); });
} catch ( exception_list const& e) {
thrown = e.what();
count = e.size();
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_first_success(fail) threw '" << thrown << "': "
<< count << " errors" << std::endl;
assert(thrown == "wait_first_success() produced only errors");
assert(count == 3);
});
/*****************************************************************************
* when_any, heterogeneous
*****************************************************************************/
//[wait_first_value_het
// No need to break out the first Fn for interface function: let the compiler
// complain if empty.
// Our functions have different return types, and we might have to return any
// of them. Use a variant, expanding std::result_of<Fn()>::type for each Fn in
// parameter pack.
template< typename ... Fns >
boost::variant< typename std::result_of< Fns() >::type ... >
wait_first_value_het( Fns && ... functions) {
// Use buffered_channel<boost::variant<T1, T2, ...>>; see remarks above.
typedef boost::variant< typename std::result_of< Fns() >::type ... > return_t;
typedef boost::fibers::buffered_channel< return_t > channel_t;
auto chanp( std::make_shared< channel_t >( 64) );
// launch all the relevant fibers
wait_first_value_impl< return_t >( chanp,
std::forward< Fns >( functions) ... );
// retrieve the first value
return_t value( chanp->value_pop() );
// close the channel: no subsequent push() has to succeed
chanp->close();
return value;
}
//]
// example usage
Example wfvh( runner, "wait_first_value_het()", [](){
//[wait_first_value_het_ex
boost::variant< std::string, double, int > result =
wait_first_value_het(
[](){ return sleeper("wfvh_third", 150); },
[](){ return sleeper(3.14, 100); },
[](){ return sleeper(17, 50); });
std::cout << "wait_first_value_het() => " << result << std::endl;
assert(boost::get< int >( result) == 17);
//]
});
/*****************************************************************************
* when_all, simple completion
*****************************************************************************/
// Degenerate case: when there are no functions to wait for, return
// immediately.
void wait_all_simple_impl( std::shared_ptr< boost::fibers::barrier >) {
}
// When there's at least one function to wait for, launch it and recur to
// process the rest.
//[wait_all_simple_impl
template< typename Fn, typename ... Fns >
void wait_all_simple_impl( std::shared_ptr< boost::fibers::barrier > barrier,
Fn && function, Fns && ... functions) {
boost::fibers::fiber(
std::bind(
[]( std::shared_ptr< boost::fibers::barrier > & barrier,
typename std::decay< Fn >::type & function) mutable {
function();
barrier->wait();
},
barrier,
std::forward< Fn >( function)
)).detach();
wait_all_simple_impl( barrier, std::forward< Fns >( functions) ... );
}
//]
// interface function: instantiate barrier, launch tasks, wait for barrier
//[wait_all_simple
template< typename ... Fns >
void wait_all_simple( Fns && ... functions) {
std::size_t count( sizeof ... ( functions) );
// Initialize a barrier(count+1) because we'll immediately wait on it. We
// don't want to wake up until 'count' more fibers wait on it. Even though
// we'll stick around until the last of them completes, use shared_ptr
// anyway because it's easier to be confident about lifespan issues.
auto barrier( std::make_shared< boost::fibers::barrier >( count + 1) );
wait_all_simple_impl( barrier, std::forward< Fns >( functions) ... );
barrier->wait();
}
//]
// example usage
Example was( runner, "wait_all_simple()", [](){
//[wait_all_simple_ex
wait_all_simple(
[](){ sleeper("was_long", 150); },
[](){ sleeper("was_medium", 100); },
[](){ sleeper("was_short", 50); });
//]
});
/*****************************************************************************
* when_all, return values
*****************************************************************************/
//[wait_nchannel
// Introduce a channel facade that closes the channel once a specific number
// of items has been pushed. This allows an arbitrary consumer to read until
// 'closed' without itself having to count items.
template< typename T >
class nchannel {
public:
nchannel( std::shared_ptr< boost::fibers::buffered_channel< T > > chan,
std::size_t lm):
chan_( chan),
limit_( lm) {
assert(chan_);
if ( 0 == limit_) {
chan_->close();
}
}
boost::fibers::channel_op_status push( T && va) {
boost::fibers::channel_op_status ok =
chan_->push( std::forward< T >( va) );
if ( ok == boost::fibers::channel_op_status::success &&
--limit_ == 0) {
// after the 'limit_'th successful push, close the channel
chan_->close();
}
return ok;
}
private:
std::shared_ptr< boost::fibers::buffered_channel< T > > chan_;
std::size_t limit_;
};
//]
// When there's only one function, call this overload
//[wait_all_values_impl
template< typename T, typename Fn >
void wait_all_values_impl( std::shared_ptr< nchannel< T > > chan,
Fn && function) {
boost::fibers::fiber( [chan, function](){
chan->push(function());
}).detach();
}
//]
// When there are two or more functions, call this overload
template< typename T, typename Fn0, typename Fn1, typename ... Fns >
void wait_all_values_impl( std::shared_ptr< nchannel< T > > chan,
Fn0 && function0,
Fn1 && function1,
Fns && ... functions) {
// process the first function using the single-function overload
wait_all_values_impl< T >( chan, std::forward< Fn0 >( function0) );
// then recur to process the rest
wait_all_values_impl< T >( chan,
std::forward< Fn1 >( function1),
std::forward< Fns >( functions) ... );
}
//[wait_all_values_source
// Return a shared_ptr<buffered_channel<T>> from which the caller can
// retrieve each new result as it arrives, until 'closed'.
template< typename Fn, typename ... Fns >
std::shared_ptr< boost::fibers::buffered_channel< typename std::result_of< Fn() >::type > >
wait_all_values_source( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
typedef typename std::result_of< Fn() >::type return_t;
typedef boost::fibers::buffered_channel< return_t > channel_t;
// make the channel
auto chanp( std::make_shared< channel_t >( 64) );
// and make an nchannel facade to close it after 'count' items
auto ncp( std::make_shared< nchannel< return_t > >( chanp, count) );
// pass that nchannel facade to all the relevant fibers
wait_all_values_impl< return_t >( ncp,
std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// then return the channel for consumer
return chanp;
}
//]
// When all passed functions have completed, return vector<T> containing
// collected results. Assume that all passed functions have the same return
// type. It is simply invalid to pass NO functions.
//[wait_all_values
template< typename Fn, typename ... Fns >
std::vector< typename std::result_of< Fn() >::type >
wait_all_values( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
typedef typename std::result_of< Fn() >::type return_t;
typedef std::vector< return_t > vector_t;
vector_t results;
results.reserve( count);
// get channel
std::shared_ptr< boost::fibers::buffered_channel< return_t > > chan =
wait_all_values_source( std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// fill results vector
return_t value;
while ( boost::fibers::channel_op_status::success == chan->pop(value) ) {
results.push_back( value);
}
// return vector to caller
return results;
}
//]
Example wav( runner, "wait_all_values()", [](){
//[wait_all_values_source_ex
std::shared_ptr< boost::fibers::buffered_channel< std::string > > chan =
wait_all_values_source(
[](){ return sleeper("wavs_third", 150); },
[](){ return sleeper("wavs_second", 100); },
[](){ return sleeper("wavs_first", 50); });
std::string value;
while ( boost::fibers::channel_op_status::success == chan->pop(value) ) {
std::cout << "wait_all_values_source() => '" << value
<< "'" << std::endl;
}
//]
//[wait_all_values_ex
std::vector< std::string > values =
wait_all_values(
[](){ return sleeper("wav_late", 150); },
[](){ return sleeper("wav_middle", 100); },
[](){ return sleeper("wav_early", 50); });
//]
std::cout << "wait_all_values() =>";
for ( std::string const& v : values) {
std::cout << " '" << v << "'";
}
std::cout << std::endl;
});
/*****************************************************************************
* when_all, throw first exception
*****************************************************************************/
//[wait_all_until_error_source
// Return a shared_ptr<buffered_channel<future<T>>> from which the caller can
// get() each new result as it arrives, until 'closed'.
template< typename Fn, typename ... Fns >
std::shared_ptr<
boost::fibers::buffered_channel<
boost::fibers::future<
typename std::result_of< Fn() >::type > > >
wait_all_until_error_source( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
typedef typename std::result_of< Fn() >::type return_t;
typedef boost::fibers::future< return_t > future_t;
typedef boost::fibers::buffered_channel< future_t > channel_t;
// make the channel
auto chanp( std::make_shared< channel_t >( 64) );
// and make an nchannel facade to close it after 'count' items
auto ncp( std::make_shared< nchannel< future_t > >( chanp, count) );
// pass that nchannel facade to all the relevant fibers
wait_first_outcome_impl< return_t >( ncp,
std::forward< Fn >( function),
std::forward< Fns >( functions) ... );
// then return the channel for consumer
return chanp;
}
//]
// When all passed functions have completed, return vector<T> containing
// collected results, or throw the first exception thrown by any of the passed
// functions. Assume that all passed functions have the same return type. It
// is simply invalid to pass NO functions.
//[wait_all_until_error
template< typename Fn, typename ... Fns >
std::vector< typename std::result_of< Fn() >::type >
wait_all_until_error( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
typedef typename std::result_of< Fn() >::type return_t;
typedef typename boost::fibers::future< return_t > future_t;
typedef std::vector< return_t > vector_t;
vector_t results;
results.reserve( count);
// get channel
std::shared_ptr<
boost::fibers::buffered_channel< future_t > > chan(
wait_all_until_error_source( std::forward< Fn >( function),
std::forward< Fns >( functions) ... ) );
// fill results vector
future_t future;
while ( boost::fibers::channel_op_status::success == chan->pop( future) ) {
results.push_back( future.get() );
}
// return vector to caller
return results;
}
//]
Example waue( runner, "wait_all_until_error()", [](){
//[wait_all_until_error_source_ex
typedef boost::fibers::future< std::string > future_t;
std::shared_ptr< boost::fibers::buffered_channel< future_t > > chan =
wait_all_until_error_source(
[](){ return sleeper("wauess_third", 150); },
[](){ return sleeper("wauess_second", 100); },
[](){ return sleeper("wauess_first", 50); });
future_t future;
while ( boost::fibers::channel_op_status::success == chan->pop( future) ) {
std::string value( future.get() );
std::cout << "wait_all_until_error_source(success) => '" << value
<< "'" << std::endl;
}
//]
chan = wait_all_until_error_source(
[](){ return sleeper("wauesf_third", 150); },
[](){ return sleeper("wauesf_second", 100, true); },
[](){ return sleeper("wauesf_first", 50); });
//[wait_all_until_error_ex
std::string thrown;
//<-
try {
while ( boost::fibers::channel_op_status::success == chan->pop( future) ) {
std::string value( future.get() );
std::cout << "wait_all_until_error_source(fail) => '" << value
<< "'" << std::endl;
}
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_all_until_error_source(fail) threw '" << thrown
<< "'" << std::endl;
thrown.clear();
//->
try {
std::vector< std::string > values = wait_all_until_error(
[](){ return sleeper("waue_late", 150); },
[](){ return sleeper("waue_middle", 100, true); },
[](){ return sleeper("waue_early", 50); });
//<-
std::cout << "wait_all_until_error(fail) =>";
for ( std::string const& v : values) {
std::cout << " '" << v << "'";
}
std::cout << std::endl;
//->
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_all_until_error(fail) threw '" << thrown
<< "'" << std::endl;
//]
});
/*****************************************************************************
* when_all, collect exceptions
*****************************************************************************/
// When all passed functions have succeeded, return vector<T> containing
// collected results, or throw exception_list containing all exceptions thrown
// by any of the passed functions. Assume that all passed functions have the
// same return type. It is simply invalid to pass NO functions.
//[wait_all_collect_errors
template< typename Fn, typename ... Fns >
std::vector< typename std::result_of< Fn() >::type >
wait_all_collect_errors( Fn && function, Fns && ... functions) {
std::size_t count( 1 + sizeof ... ( functions) );
typedef typename std::result_of< Fn() >::type return_t;
typedef typename boost::fibers::future< return_t > future_t;
typedef std::vector< return_t > vector_t;
vector_t results;
results.reserve( count);
exception_list exceptions("wait_all_collect_errors() exceptions");
// get channel
std::shared_ptr<
boost::fibers::buffered_channel< future_t > > chan(
wait_all_until_error_source( std::forward< Fn >( function),
std::forward< Fns >( functions) ... ) );
// fill results and/or exceptions vectors
future_t future;
while ( boost::fibers::channel_op_status::success == chan->pop( future) ) {
std::exception_ptr exp = future.get_exception_ptr();
if ( ! exp) {
results.push_back( future.get() );
} else {
exceptions.add( exp);
}
}
// if there were any exceptions, throw
if ( exceptions.size() ) {
throw exceptions;
}
// no exceptions: return vector to caller
return results;
}
//]
Example wace( runner, "wait_all_collect_errors()", [](){
std::vector< std::string > values = wait_all_collect_errors(
[](){ return sleeper("waces_late", 150); },
[](){ return sleeper("waces_middle", 100); },
[](){ return sleeper("waces_early", 50); });
std::cout << "wait_all_collect_errors(success) =>";
for ( std::string const& v : values) {
std::cout << " '" << v << "'";
}
std::cout << std::endl;
std::string thrown;
std::size_t errors = 0;
try {
values = wait_all_collect_errors(
[](){ return sleeper("wacef_late", 150, true); },
[](){ return sleeper("wacef_middle", 100, true); },
[](){ return sleeper("wacef_early", 50); });
std::cout << "wait_all_collect_errors(fail) =>";
for ( std::string const& v : values) {
std::cout << " '" << v << "'";
}
std::cout << std::endl;
} catch ( exception_list const& e) {
thrown = e.what();
errors = e.size();
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_all_collect_errors(fail) threw '" << thrown
<< "': " << errors << " errors" << std::endl;
});
/*****************************************************************************
* when_all, heterogeneous
*****************************************************************************/
//[wait_all_members_get
template< typename Result, typename ... Futures >
Result wait_all_members_get( Futures && ... futures) {
// Fetch the results from the passed futures into Result's initializer
// list. It's true that the get() calls here will block the implicit
// iteration over futures -- but that doesn't matter because we won't be
// done until the slowest of them finishes anyway. As results are
// processed in argument-list order rather than order of completion, the
// leftmost get() to throw an exception will cause that exception to
// propagate to the caller.
return Result{ futures.get() ... };
}
//]
//[wait_all_members
// Explicitly pass Result. This can be any type capable of being initialized
// from the results of the passed functions, such as a struct.
template< typename Result, typename ... Fns >
Result wait_all_members( Fns && ... functions) {
// Run each of the passed functions on a separate fiber, passing all their
// futures to helper function for processing.
return wait_all_members_get< Result >(
boost::fibers::async( std::forward< Fns >( functions) ) ... );
}
//]
// used by following example
//[wait_Data
struct Data {
std::string str;
double inexact;
int exact;
friend std::ostream& operator<<( std::ostream& out, Data const& data)/*=;
...*/
//<-
{
return out << "Data{str='" << data.str << "', inexact=" << data.inexact
<< ", exact=" << data.exact << "}";
}
//->
};
//]
// example usage
Example wam( runner, "wait_all_members()", [](){
//[wait_all_members_data_ex
Data data = wait_all_members< Data >(
[](){ return sleeper("wams_left", 100); },
[](){ return sleeper(3.14, 150); },
[](){ return sleeper(17, 50); });
std::cout << "wait_all_members<Data>(success) => " << data << std::endl;
//]
std::string thrown;
try {
data = wait_all_members< Data >(
[](){ return sleeper("wamf_left", 100, true); },
[](){ return sleeper(3.14, 150); },
[](){ return sleeper(17, 50, true); });
std::cout << "wait_all_members<Data>(fail) => " << data << std::endl;
} catch ( std::exception const& e) {
thrown = e.what();
}
std::cout << "wait_all_members<Data>(fail) threw '" << thrown
<< '"' << std::endl;
//[wait_all_members_vector_ex
// If we don't care about obtaining results as soon as they arrive, and we
// prefer a result vector in passed argument order rather than completion
// order, wait_all_members() is another possible implementation of
// wait_all_until_error().
auto strings = wait_all_members< std::vector< std::string > >(
[](){ return sleeper("wamv_left", 150); },
[](){ return sleeper("wamv_middle", 100); },
[](){ return sleeper("wamv_right", 50); });
std::cout << "wait_all_members<vector>() =>";
for ( std::string const& str : strings) {
std::cout << " '" << str << "'";
}
std::cout << std::endl;
//]
});
/*****************************************************************************
* main()
*****************************************************************************/
int main( int argc, char *argv[]) {
runner.run();
std::cout << "done." << std::endl;
return EXIT_SUCCESS;
}
|