mirror of
https://github.com/boostorg/fiber.git
synced 2026-01-25 06:12:15 +00:00
- buffered_channel: MPMC with lock-free guarantees - unbuffered_channel: rendezvous point
48 lines
956 B
C++
48 lines
956 B
C++
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
#include <boost/fiber/all.hpp>
|
|
|
|
typedef boost::fibers::unbuffered_channel< unsigned int > channel_t;
|
|
|
|
void foo( channel_t & chan) {
|
|
chan.push( 1);
|
|
chan.push( 1);
|
|
chan.push( 2);
|
|
chan.push( 3);
|
|
chan.push( 5);
|
|
chan.push( 8);
|
|
chan.push( 12);
|
|
chan.close();
|
|
}
|
|
|
|
void bar( channel_t & chan) {
|
|
for ( unsigned int value : chan) {
|
|
std::cout << value << " ";
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
channel_t chan;
|
|
|
|
boost::fibers::fiber f1( & foo, std::ref( chan) );
|
|
boost::fibers::fiber f2( & bar, std::ref( chan) );
|
|
|
|
f1.join();
|
|
f2.join();
|
|
|
|
std::cout << "done." << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
} catch ( std::exception const& e) {
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
} catch (...) {
|
|
std::cerr << "unhandled exception" << std::endl;
|
|
}
|
|
return EXIT_FAILURE;
|
|
}
|