SHARE
TWEET

Untitled

a guest Sep 15th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <boost/fiber/all.hpp>
  3.  
  4. int main() {
  5.  
  6.     boost::fibers::barrier barrier{ 2 };
  7.  
  8.     boost::fibers::mutex m;
  9.     boost::fibers::condition_variable cv;
  10.  
  11.     boost::fibers::buffered_channel<int> channel{ 2 };
  12.  
  13.     int count = 2;
  14.  
  15.     std::thread([&] {
  16.         boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
  17.         barrier.wait();
  18.  
  19.         std::unique_lock<boost::fibers::mutex> l(m);
  20.         cv.wait(l, [&] { return count == 0; });
  21.     }).detach();
  22.  
  23.     boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
  24.     barrier.wait();
  25.  
  26.     boost::fibers::fiber([&] {
  27.         for (;;) {
  28.             int x;
  29.             if (channel.pop_wait_for(x, std::chrono::milliseconds(100)) ==
  30.                 boost::fibers::channel_op_status::timeout) {
  31.                 std::cout << "timed out" << std::endl;
  32.             }
  33.             else {
  34.                 std::cout << "got value: " << x << std::endl;
  35.             }
  36.         }
  37.         count--;
  38.     }).detach();
  39.  
  40.     boost::fibers::fiber([&] {
  41.         for (int x = 0;; x++) {
  42.             channel.push(x);
  43.             boost::this_fiber::sleep_for(std::chrono::seconds(1));
  44.         }
  45.         count--;
  46.     }).detach();
  47.  
  48.     std::unique_lock<boost::fibers::mutex> l(m);
  49.     cv.wait(l, [&] { return count == 0; });
  50.  
  51.     return 0;
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top