File: UsingAout.rst

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (19 lines) | stat: -rw-r--r-- 947 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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).

.. literalinclude:: /examples/aout.cpp
   :language: C++