Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
0b7ecad7
Unverified
Commit
0b7ecad7
authored
Aug 09, 2023
by
Dominik Charousset
Committed by
GitHub
Aug 09, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1517
Add example for caf::after
parents
35d6cdce
b39e7039
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
+62
-0
examples/CMakeLists.txt
examples/CMakeLists.txt
+1
-0
examples/message_passing/after.cpp
examples/message_passing/after.cpp
+61
-0
No files found.
examples/CMakeLists.txt
View file @
0b7ecad7
...
@@ -24,6 +24,7 @@ add_core_example(. hello_world)
...
@@ -24,6 +24,7 @@ add_core_example(. hello_world)
add_core_example
(
config read-json
)
add_core_example
(
config read-json
)
# basic message passing primitives
# basic message passing primitives
add_core_example
(
message_passing after
)
add_core_example
(
message_passing calculator
)
add_core_example
(
message_passing calculator
)
add_core_example
(
message_passing cell
)
add_core_example
(
message_passing cell
)
add_core_example
(
message_passing dancing_kirby
)
add_core_example
(
message_passing dancing_kirby
)
...
...
examples/message_passing/after.cpp
0 → 100644
View file @
0b7ecad7
// This example shows how to use caf::after.
#include "caf/after.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include <chrono>
#include <iostream>
#include <random>
#include <string>
using
std
::
cout
;
using
std
::
endl
;
using
namespace
caf
;
// Sends a random number of printable characters to `sink` and then quits.
void
generator
(
event_based_actor
*
self
,
actor
sink
)
{
std
::
random_device
rd
;
std
::
minstd_rand
gen
{
rd
()};
const
auto
count
=
std
::
uniform_int_distribution
<>
{
20
,
100
}(
gen
);
std
::
uniform_int_distribution
<>
dis
{
33
,
126
};
for
(
auto
i
=
0
;
i
<
count
;
i
++
)
{
self
->
send
(
sink
,
static_cast
<
char
>
(
dis
(
gen
)));
}
}
// Collects the incoming characters until no new characters arrive for 500ms.
// Prints every 60 characters.
behavior
collector
(
stateful_actor
<
std
::
string
>*
self
)
{
using
namespace
std
::
chrono_literals
;
return
{
[
self
](
char
c
)
{
self
->
state
.
push_back
(
c
);
constexpr
auto
flush_threshold
=
60
;
if
(
self
->
state
.
size
()
==
flush_threshold
)
{
cout
<<
"Received message length: "
<<
self
->
state
.
size
()
<<
endl
<<
"Message content: "
<<
self
->
state
<<
endl
;
self
->
state
.
clear
();
}
},
caf
::
after
(
500ms
)
>>
[
self
]()
{
cout
<<
"Timeout reached!"
<<
endl
;
if
(
!
self
->
state
.
empty
())
{
cout
<<
"Received message length: "
<<
self
->
state
.
size
()
<<
endl
<<
"Message content: "
<<
self
->
state
<<
endl
;
}
self
->
quit
();
},
};
}
void
caf_main
(
actor_system
&
system
)
{
auto
col
=
system
.
spawn
(
collector
);
system
.
spawn
(
generator
,
col
);
}
CAF_MAIN
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment