\section{Spawning Actors}

Actors are created using the function \lstinline^spawn^.
The easiest way to implement actors is to use functors, e.g., a free function or a lambda expression.
The arguments to the functor are passed to \lstinline^spawn^ as additional arguments.
The function \lstinline^spawn^ also takes optional flags as template parameter.
%The optional template parameter of \lstinline^spawn^ decides whether an actor should run in its own thread or takes place in the cooperative scheduling.
The flag \lstinline^detached^ causes \lstinline^spawn^ to assign a dedicated thread to the actor, i.e., to opt-out of the cooperative scheduling.
Convenience flags like \lstinline^linked^ or \lstinline^monitored^ automatically link or monitor to the newly created actor.
Naturally, these two flags are not available on ``top-level'' spawns.
Actors that make use of the blocking API---see Section \ref{Sec::BlockingAPI}---must be spawned using the flag \lstinline^blocking_api^.
Flags are concatenated using the operator \lstinline^+^, as shown in the examples below.

\begin{lstlisting}
#include "caf/all.hpp"
using namespace caf;

void my_actor1();
void my_actor2(event_based_actor*, int arg1, const std::string& arg2);
void ugly_duckling(blocking_actor*);

class my_actor3 : public event_based_actor { /* ... */ };
class my_actor4 : public event_based_actor {
  public: my_actor4(int some_value) { /* ... */ }
  /* ... */
};

// whenever we want to link to or monitor a spawned actor,
// we have to spawn it using the self pointer, otherwise
// we can use the free function 'spawn' (top-level spawn)
void server(event_based_actor* self) {
  // spawn functor-based actors
  auto a0 = self->spawn(my_actor1);
  auto a1 = self->spawn<linked>(my_actor2, 42, "hello actor");
  auto a2 = self->spawn<monitored>([] { /* ... */ });
  auto a3 = self->spawn([](int) { /* ... */ }, 42);
  // spawn thread-mapped actors
  auto a4 = self->spawn<detached>(my_actor1);
  auto a5 = self->spawn<detached + linked>([] { /* ... */ });
  auto a6 = self->spawn<detached>(my_actor2, 0, "zero");
  // spawn class-based actors
  auto a7 = self->spawn<my_actor3>();
  auto a8 = self->spawn<my_actor4, monitored>(42);
  // spawn and detach class-based actors
  auto a9 = self->spawn<my_actor4, detached>(42);
  // spawn actors that need access to the blocking API
  auto aa = self->spawn<blocking_api>(ugly_duckling);
  // compiler error: my_actor2 captures the implicit
  // self pointer as event_based_actor* and thus cannot
  // be spawned using the `blocking_api` flag
  // --- auto ab = self->spawn<blocking_api>(my_actor2);
}
\end{lstlisting}

%TODO: check how std::bind(..., self) behaves atm
%\textbf{Note}: \lstinline^spawn(fun, arg0, ...)^ is \textbf{not} equal to \lstinline^spawn(std::bind(fun, arg0, ...))^!
%For example, a call to \lstinline^spawn(fun, self, ...)^ will pass a pointer to the calling actor to the newly created actor, as expected, whereas \lstinline^spawn(std::bind(fun, self, ...))^ wraps the type of \lstinline^self^ into the function wrapper and evaluates \lstinline^self^ on function invocation.
%Thus, the actor will end up having a pointer \emph{to itself} rather than a pointer to its parent.
