-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhello_world.c
More file actions
92 lines (79 loc) · 1.9 KB
/
hello_world.c
File metadata and controls
92 lines (79 loc) · 1.9 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2017 Jens Hauke. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* A "hello world" for copy threads.
*
* Demonstrate the usage of
* cth_start(), cth_yield() and cth_run().
*
* 2017-05-10 Jens Hauke <jens.hauke@4k2.de>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cthread.h>
void athread(void *arg) {
char *name = (char *)arg;
unsigned i;
printf("Hello World!\n");
for (i = 0; i < 4; i++) {
printf("Thread %s loop %u\n", name, i);
// Start an additional thread from "th1" at i==1
if (i == 1 && strcmp(name, "th1") == 0) {
printf("Starting thread th3 from th1\n");
cth_start(athread, "th3");
}
cth_yield();
}
}
int main(int argc, char **argv)
{
cth_start(athread, "th1");
cth_start(athread, "th2");
cth_run();
printf("All threads are done\n");
return 0;
}
/*
* Example output:
*
* ./hello_world
Hello World!
Thread th1 loop 0
Hello World!
Thread th2 loop 0
Thread th1 loop 1
Starting thread th3 from th1
Thread th2 loop 1
Hello World!
Thread th3 loop 0
Thread th1 loop 2
Thread th2 loop 2
Thread th3 loop 1
Thread th1 loop 3
Thread th2 loop 3
Thread th3 loop 2
Thread th3 loop 3
All threads are done
*/
/*
* Local Variables:
* compile-command: "make hello_world && ./hello_world"
* End:
*/