Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An actor-based framework for C++. Actors are light-weight objects, and they interact fully asynchronously with messages.

License

NotificationsYou must be signed in to change notification settings

artpaul/acto

Repository files navigation

An actor-based framework for C++. Actors are light-weight objects, and they interact fully asynchronously with messages.

#include<acto.h>staticconstexprint BALLS =1000;staticconstexprint DURATION =2000;staticconstexprint PLAYERS =10000;structmsg_start {int balls;msg_start(int balls_)    : balls(balls_) {  }};structmsg_finish { };structmsg_ball { };classPlayer :publicacto::actor {public:Player() {// Send a ball back toward the wall.    actor::handler<msg_ball>(      [](acto::actor_ref sender,const msg_ball& msg) { sender.send(msg); });  }};classWall :publicacto::actor {public:Wall() {    actor::handler<msg_ball>(&Wall::do_ball);    actor::handler<msg_finish>(&Wall::do_finish);    actor::handler<msg_start>(&Wall::do_start);for (int i =0; i < PLAYERS; i++)      players_[i] = acto::spawn<Player>();  }~Wall() {for (int i =0; i < PLAYERS; i++)// Actors should be destroyed explicitly.acto::destroy(players_[i]);  }private:voiddo_ball(acto::actor_ref, msg_ball msg) {    players_[(rand() % PLAYERS)].send(std::move(msg));  }voiddo_finish(acto::actor_ref,const msg_finish&) {actor::die();  }voiddo_start(acto::actor_ref,const msg_start& msg) {for (int i =0; i < msg.balls; i++)      players_[(rand() % PLAYERS)].send(msg_ball());  }private:  acto::actor_ref players_[PLAYERS];};intmain() {// Create main actor.  acto::actor_ref wall = acto::spawn<Wall>();// Run ping-pong loop.  wall.send<msg_start>(BALLS);// Wait a few seconds.acto::core::sleep(std::chrono::milliseconds(DURATION));// Stop the main actor.  wall.send(msg_finish());// Waits until the actor stops.acto::join(wall);return0;}

About

An actor-based framework for C++. Actors are light-weight objects, and they interact fully asynchronously with messages.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp