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
c90a2ea7
Commit
c90a2ea7
authored
Apr 28, 2017
by
Sebastian Woelke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Fibonacci example
parent
e1a909db
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
0 deletions
+63
-0
examples/fibonacci.cpp
examples/fibonacci.cpp
+63
-0
No files found.
examples/fibonacci.cpp
0 → 100644
View file @
c90a2ea7
#include <iostream>
#include <cmath>
#include <vector>
#include <tuple>
#include <unistd.h>
#include "caf/all.hpp"
using
namespace
caf
;
using
std
::
endl
;
using
std
::
cout
;
using
std
::
cerr
;
using
std
::
string
;
behavior
fibonacci_fun
(
stateful_actor
<
int
>*
self
)
{
self
->
state
=
-
1
;
return
{
[
=
]
(
int
n
)
->
result
<
int
>
{
if
(
n
<=
1
)
return
n
;
auto
fib_actor_a
=
self
->
spawn
(
fibonacci_fun
);
auto
fib_actor_b
=
self
->
spawn
(
fibonacci_fun
);
auto
rp
=
self
->
make_response_promise
<
int
>
();
auto
f
=
[
=
]
(
int
result
)
mutable
{
if
(
self
->
state
<
0
)
self
->
state
=
result
;
else
rp
.
deliver
(
self
->
state
+
result
);
};
self
->
request
(
fib_actor_a
,
infinite
,
n
-
1
).
then
(
f
);
self
->
request
(
fib_actor_b
,
infinite
,
n
-
2
).
then
(
f
);
return
rp
;
},
};
}
class
my_config
:
public
actor_system_config
{
public:
int
fib_num
=
35
;
my_config
()
{
opt_group
{
custom_options_
,
"global"
}
.
add
(
fib_num
,
"fibnum,n"
,
"set the fib num to be calculated"
);
}
};
void
caf_main
(
actor_system
&
system
,
const
my_config
&
cfg
){
auto
fib_actor
=
system
.
spawn
(
fibonacci_fun
);
scoped_actor
self
{
system
};
self
->
request
(
fib_actor
,
infinite
,
cfg
.
fib_num
).
receive
(
[
&
](
int
result
){
cout
<<
"result: "
<<
result
<<
endl
;
},
[
&
](
error
&
err
){
cerr
<<
"Error: "
<<
system
.
render
(
err
)
<<
std
::
endl
;
}
);
}
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