Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tom Niget
typon
Commits
6c47a5ed
Commit
6c47a5ed
authored
Aug 11, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples/: Add programs illustrating fork overloads
parent
a2ff3992
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
rt/examples/fork_variations.cpp
rt/examples/fork_variations.cpp
+42
-0
rt/examples/naive_fibonacci_fork_into.cpp
rt/examples/naive_fibonacci_fork_into.cpp
+30
-0
No files found.
rt/examples/fork_variations.cpp
0 → 100644
View file @
6c47a5ed
#include <typon/typon.hpp>
#include <typon/logger.hpp>
using
namespace
typon
;
int
fibo
(
int
n
)
{
if
(
n
<
2
)
return
n
;
return
fibo
(
n
-
1
)
+
fibo
(
n
-
2
);
}
Task
<
int
>
hello
(
int
n
)
{
int
r
=
fibo
(
30
);
LOG
(
"hello(%d)"
,
n
);
co_return
r
;
}
Join
<
void
>
parallel
()
{
LOG
(
"Forked f = co_await fork(hello(10))"
);
Forked
f
=
co_await
fork
(
hello
(
1
));
LOG
(
"co_await fork(hello(10), {})"
);
co_await
fork
(
hello
(
2
),
{});
// Careful! r must not be unwound before the fork finishes.
LOG
(
"int r; co_await fork(hello(10), r)"
);
int
r
;
co_await
fork
(
hello
(
3
),
r
);
// Sync to ensure the fork finished before r is unwound.
// Assuming there are no exceptions...
co_await
Sync
();
}
Root
root
()
{
co_await
parallel
();
}
int
main
()
{
root
().
call
();
puts
(
"done"
);
return
0
;
}
rt/examples/naive_fibonacci_fork_into.cpp
0 → 100644
View file @
6c47a5ed
#include <typon/typon.hpp>
#include <cstdio>
using
namespace
typon
;
Join
<
int
>
fibo
(
int
n
)
{
if
(
n
<
2
)
{
co_return
n
;
}
// Danger !
// The memory for a and b could be freed before the forks complete
int
a
;
co_await
fork
(
fibo
(
n
-
1
),
a
);
int
b
;
co_await
fork
(
fibo
(
n
-
2
),
b
);
// This sync() ensures that the forks complete before a and b are freed
// As long as there are no exceptions...
co_await
Sync
();
co_return
a
+
b
;
}
Root
root
()
{
int
result
=
co_await
fibo
(
40
);
printf
(
"%d
\n
"
,
result
);
}
int
main
()
{
root
().
call
();
puts
(
"done"
);
return
0
;
}
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