PrevUpHomeNext

How To

Cookbook
Build and Test this library

Although the library is in a preview state and the interface is expected to change, here are some constructs which implement common operations.

Split operation

It's a common task to dispatch an upstream queue to different downstream queues. The following example provides an example of how to decrease latency of priority requests by processing them in a separate pipeline:

void split_by_priority(
  ppl::queue_back<request> priority,
  ppl::queue_back<request> non_priority,
  const request& input
)
{
  if (input.is_priority)
  {
    priority.push(input);
  }
  else
  {
    non_priority.push(input);
  }
}
ppl::queue<request> priority_queue;
ppl::queue<request> non_priority_queue;

auto split = std::bind(
  split_by_priority,
  std::ref(priority_queue),
  std::ref(non_priority_queue),
  _1
);

auto reader = ppl::from(generate_requests) | split;

auto priority_processor = priority_queue | parse_request | request_id | to_stdout;
auto processor      = non_priority_queue | parse_request | request_id | process_later;

Please refer to example/split.cpp for the full source code.

Join operation

One might want to combine two different upstream queues into a single one, like the unix join command does. The following example matches persons to associated departments:

void join(
  ppl::queue_front<department> deps,
  ppl::queue_front<person> perss,
  ppl::queue_back<relation> downstream
)
{
  department dep;
  person pers{"", -1};

  while (deps.wait_pull(dep))
  {
    if (dep.id == pers.department_id)
    {
      relation rel{dep.name, pers.name};
      downstream.push(std::move(rel));
    }

    while (perss.wait_pull(pers))
    {
      if (dep.id == pers.department_id)
      {
        relation rel{dep.name, pers.name};
        downstream.push(std::move(rel));
      }
      else
      {
        break;
      }
    }
  }
}
auto relations = std::bind(join, std::ref(departments), std::ref(persons), _1);
auto plan = ppl::from<relation>(relations) | to_stdout;

Please refer to example/split.cpp for the full source code.

This library requires a compliant C++11 compiler. It's tested using GCC 4.8.1 and clang 3.4. The msvc shipped with Visual Studio 2013 fails to compile the tests.

The library is header only, nothing specific has to be compiled to use it, however, it depends on Boost.Thread, which has some libraries to link.

The tests, examples and documentation can be compiled using the following commands:

# build and run tests
test/$ BOOST_ROOT=/path/to/boost/ bjam toolset=gcc # or clang

# build examples
example/$ BOOST_ROOT=/path/to/boost/ bjam toolset=gcc # or clang

# build documentation: requires docbook-xsl
doc/$ BOOST_ROOT=/path/to/boost/ bjam

PrevUpHomeNext