-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadtest.c
58 lines (50 loc) · 1.11 KB
/
threadtest.c
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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "linkedlist.h"
#include "thread.h"
int amount;
struct lock_t add;
struct lock_t threadfinish;
struct node* start = 0;
int threadbegin = 0;
void
add_int(int num)
{
printf(1, "Attempting to add %d to linked list\n", num);
lock_acquire(&add);
ll_add(&start, num);
lock_release(&add);
printf(1, "Added %d to linked list\n", num);
}
void
thread_add(int begin)
{
printf(1, "Starting child thread\n");
int i;
for(i=0; i<amount; i++)
add_int(begin + i);
printf(1, "Finishing child thread\n");
exit();
}
int
main (int argc, char* argv[])
{
amount = atoi(argv[1]);
int i = 0;
char* stack;
lock_init(&add);
if ((stack = malloc(4096)) > 0)
thread_create((void*)&thread_add, stack, 4096, &i);
else
printf(1, "Failed to malloc(4096) for new thread stack\n");
int begin = amount * 2;
add_int(begin++);
for(i=1; i<amount; i++)
add_int(begin++);
ll_print(start);
printf(1, "Main thread waiting for child thread finish\n");
wait();
ll_print(start);
exit();
}