Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/fiber/all.hpp>
- int main() {
- boost::fibers::barrier barrier{ 2 };
- boost::fibers::mutex m;
- boost::fibers::condition_variable cv;
- boost::fibers::buffered_channel<int> channel{ 2 };
- int count = 2;
- std::thread([&] {
- boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
- barrier.wait();
- std::unique_lock<boost::fibers::mutex> l(m);
- cv.wait(l, [&] { return count == 0; });
- }).detach();
- boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(2);
- barrier.wait();
- boost::fibers::fiber([&] {
- for (;;) {
- int x;
- if (channel.pop_wait_for(x, std::chrono::milliseconds(100)) ==
- boost::fibers::channel_op_status::timeout) {
- std::cout << "timed out" << std::endl;
- }
- else {
- std::cout << "got value: " << x << std::endl;
- }
- }
- count--;
- }).detach();
- boost::fibers::fiber([&] {
- for (int x = 0;; x++) {
- channel.push(x);
- boost::this_fiber::sleep_for(std::chrono::seconds(1));
- }
- count--;
- }).detach();
- std::unique_lock<boost::fibers::mutex> l(m);
- cv.wait(l, [&] { return count == 0; });
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement