-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlevel1_threads.cpp
More file actions
70 lines (64 loc) · 2 KB
/
level1_threads.cpp
File metadata and controls
70 lines (64 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <api.h>
using namespace api;
void do_threads_stuff()
{
/* Test micro threads. */
int a = 1, b = 2, c = 3;
microthread::oneshot(
[](int a, int b, int c)
{
print(
"Hello from thread 1! a = ", a, ", b = ", b, ", c = ", c,
"\n");
microthread::yield();
print("And we're back! a = ", a, ", b = ", b, ", c = ", c, "\n");
},
a, b, c);
print("Back in the main thread .. going back!\n");
a = 2;
b = 4;
c = 6;
microthread::yield();
print("Done, back in the main thread!\n");
/*auto thread = microthread::create(
[](int a, int b, int c)
{
print(
"Hello from thread 2! a = ", a, ", b = ", b, ", c = ", c,
"\n");
microthread::yield();
print("Anyone going to join us? Returning 666.\n");
// Since the main thread is joining this thread,
// we should be able to spin-yield a bit.
for (size_t i = 0; i < 100; i++)
microthread::yield();
return 666;
},
a, b, c);
print("Joining the thread any time now...\n");
auto retval = microthread::join(thread);
print("Full thread exited, return value: ", retval, "\n");
*/
/* GDB can be automatically opened at this point. */
Game::breakpoint();
/* This creates a new thread with no arguments and immediately starts
executing it. The sleep() will block the thread until some time has
passed, and then resume. At the end we make a remote function call
to a long-running process that sits in an event loop waiting for work.
*/
microthread::oneshot(
[](std::string mt)
{
print("Hello ", mt, " World!\n");
Timer::sleep(1.0);
print("Hello Belated Microthread World! 1 second passed.\n");
/* add_remote_work is implemented in events.hpp
NOTE: We cannot pass "anything" we want here,
because the shared pagetables between remote machines
are only in effect during a call, and not after.
Remote work is something that is executed at a later time.
But, we can still pass constant read-only data. */
},
"Microthread");
print("Back again in the start() function!\n");
}