Using aout – A Concurrency-safe Wrapper for cout

When using cout from multiple actors, output often appears interleaved. Moreover, using cout from multiple actors – and thus from multiple threads – in parallel should be avoided regardless, since the standard does not guarantee a thread-safe implementation.

By replacing std::cout with caf::aout, actors can achieve a concurrency-safe text output. The header caf/all.hpp also defines overloads for std::endl and std::flush for aout, but does not support the full range of ostream operations (yet). Each write operation to aout sends a message to a “hidden” actor. This actor only prints lines, unless output is forced using flush. The example below illustrates printing of lines of text from multiple actors (in random order).

#include "caf/actor_ostream.hpp"
#include "caf/actor_system.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <random>

using namespace caf;

behavior printer(event_based_actor* self, int32_t num, int32_t delay) {
  aout(self).println("Hi there! This is actor nr. {}!", num);
  auto timeout = std::chrono::milliseconds{delay};
  self->mail(timeout_atom_v).delay(timeout).send(self);
  return {
    [=](timeout_atom) {
      aout(self).println("Actor nr. {} says goodbye after waiting for {}ms!",
                         num, delay);
    },
  };
}

void caf_main(actor_system& sys) {
  std::random_device rd;
  std::minstd_rand re{rd()};
  std::uniform_int_distribution<int32_t> dis{1, 99};
  for (int32_t i = 1; i <= 50; ++i)
    sys.spawn(printer, i, dis(re));
}

CAF_MAIN()