Giter VIP home page Giter VIP logo

boost-future-queue-examples's Introduction

Examples of using boost::future to make blocking queues.

The advantage of boost::future over boost::condition_variable is the presence of wait_for_any and when_any, essentially granting the behavior of win32's WaitForMultipleObjects().

Here is an example of several threads that pass different kinds of messages to each other, essentially achieving actor-style computation without explicit actors:

#include "BlockingQueue.hpp"

#include <boost/thread.hpp>
#include <iostream>
#include <tuple>

using boost::thread;
using boost::future;
using boost::future_status;
using std::cout;
using std::cin;
using std::tuple;
using boost::optional;
using boost::shared_future;

int main() {
  promise<void> consumerTermination;
  future<void> consumerTerminated = consumerTermination.get_future();
  
  future<void> consoleTerminated = boost::async([]() { cin.getline(NULL, 0); });
  
  shared_future<void> terminated = when_any(std::move(consumerTerminated), std::move(consoleTerminated))
    .then([](future<tuple<future<void>,future<void>>> t) { });
  
  BlockingQueue<int> queue(2, 10);
  
  thread producer([&queue,&terminated] {
    for (int i=0; ; i++) {
      if (terminated.wait_for(boost::chrono::milliseconds(500)) == future_status::ready) {
        // Just feels more right somehow.
        terminated.get();
        queue.closeSink();
        break;
      }
      
      queue.feed({ i });
    }
  });

  thread consumer([&queue, &terminated] {
    int total = 0;
    for (;;) {
      future<optional<vector<int>>> queueNext = queue.next();
      unsigned which = wait_for_any(queueNext, terminated);
      if (which == 0) {
        optional<vector<int>> batch = queueNext.get();
        if (batch) {
          for (int x : batch.get()) {
            cout << x << "\n";
            total += 1;

            if (total >= 5)
              goto end;
          }
        }
        else {
          goto end;
        }
      }
      else if (which == 1) {
        goto end;
      }
    }
    
    end: ;
  });
  
  consumer.join();
  consumerTermination.set_value();
  producer.join();

  return 0;
}

boost-future-queue-examples's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.