Commit f503ca4e authored by Dominik Charousset's avatar Dominik Charousset

added `spawn_link` and `spawn_monitor`

parent 7fb9fdb9
......@@ -785,6 +785,98 @@ actor_ptr spawn_hidden(Args&&... args) {
#endif
/**
* @brief Spawns a new context-switching or thread-mapped {@link actor}
* that executes <tt>fun(args...)</tt> and creates a link between the
* calling actor and the new actor.
* @param fun A function implementing the actor's behavior.
* @param args Optional function parameters for @p fun.
* @tparam Hint A hint to the scheduler for the best scheduling strategy.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
*/
template<scheduling_hint Hint, typename Fun, typename... Args>
actor_ptr spawn_link(Fun&& fun, Args&&... args) {
auto res = spawn<Hint>(std::forward<Fun>(fun), std::forward<Args>(args)...);
self->link_to(res);
return res;
}
/**
* @brief Spawns a new context-switching {@link actor}
* that executes <tt>fun(args...)</tt> and creates a link between the
* calling actor and the new actor.
* @param fun A function implementing the actor's behavior.
* @param args Optional function parameters for @p fun.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
* @note This function is equal to <tt>spawn<scheduled>(fun, args...)</tt>.
*/
template<typename Fun, typename... Args>
actor_ptr spawn_link(Fun&& fun, Args&&... args) {
auto res = spawn(std::forward<Fun>(fun), std::forward<Args>(args)...);
self->link_to(res);
return res;
}
/**
* @brief Spawns an actor of type @p ActorImpl and creates a link between the
* calling actor and the new actor.
* @param args Optional constructor arguments.
* @tparam ActorImpl Subtype of {@link event_based_actor} or {@link sb_actor}.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
*/
template<class ActorImpl, typename... Args>
actor_ptr spawn_link(Args&&... args) {
auto res = spawn<ActorImpl>(std::forward<Args>(args)...);
self->link_to(res);
return res;
}
/**
* @brief Spawns a new context-switching or thread-mapped {@link actor}
* that executes <tt>fun(args...)</tt> and adds a monitor to the
* new actor.
* @param fun A function implementing the actor's behavior.
* @param args Optional function parameters for @p fun.
* @tparam Hint A hint to the scheduler for the best scheduling strategy.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
*/
template<scheduling_hint Hint, typename Fun, typename... Args>
actor_ptr spawn_monitor(Fun&& fun, Args&&... args) {
auto res = spawn<Hint>(std::forward<Fun>(fun), std::forward<Args>(args)...);
self->monitor(res);
return res;
}
/**
* @brief Spawns a new context-switching {@link actor}
* that executes <tt>fun(args...)</tt> and adds a monitor to the
* new actor.
* @param fun A function implementing the actor's behavior.
* @param args Optional function parameters for @p fun.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
* @note This function is equal to <tt>spawn<scheduled>(fun, args...)</tt>.
*/
template<typename Fun, typename... Args>
actor_ptr spawn_monitor(Fun&& fun, Args&&... args) {
auto res = spawn(std::forward<Fun>(fun), std::forward<Args>(args)...);
self->monitor(res);
return res;
}
/**
* @brief Spawns an actor of type @p ActorImpl and adds a monitor to the
* new actor.
* @param args Optional constructor arguments.
* @tparam ActorImpl Subtype of {@link event_based_actor} or {@link sb_actor}.
* @returns An {@link actor_ptr} to the spawned {@link actor}.
*/
template<class ActorImpl, typename... Args>
actor_ptr spawn_monitor(Args&&... args) {
auto res = spawn<ActorImpl>(std::forward<Args>(args)...);
self->monitor(res);
return res;
}
/** @} */
/**
......
......@@ -685,7 +685,7 @@ int main() {
// and kill them all through killing the link
auto twenty_thousand = spawn([]() {
for (int i = 0; i < 20000; ++i) {
self->link_to(spawn<event_testee>());
spawn_link<event_testee>();
}
receive_loop (
others() >> []() {
......@@ -696,10 +696,8 @@ int main() {
send(twenty_thousand, atom("EXIT"), exit_reason::user_defined);
await_all_others_done();
self->trap_exit(true);
auto ping_actor = spawn(ping, 10);
auto pong_actor = spawn(pong, ping_actor);
self->monitor(pong_actor);
self->monitor(ping_actor);
auto ping_actor = spawn_monitor(ping, 10);
auto pong_actor = spawn_monitor(pong, ping_actor);
self->link_to(pong_actor);
int i = 0;
int flags = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment