forked from cgaebel/pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_ring.c
99 lines (69 loc) · 1.83 KB
/
thread_ring.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
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
93
94
95
96
97
98
99
/*
* thread_ring.c - The classic `thread ring' benchmark, using pipes as its
* communication medium.
*/
#include "pipe.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREADS 8
typedef struct {
int threadnumber;
pipe_consumer_t* in;
pipe_producer_t* out;
} thread_context_t;
static thread_context_t contexts[THREADS];
static void* thread_func(void* context)
{
thread_context_t* ctx = context;
int n = 0;
while(pipe_pop(ctx->in, &n, 1))
{
if(n == 0)
{
printf("%i\n", ctx->threadnumber);
break;
}
--n;
pipe_push(ctx->out, &n, 1);
}
pipe_producer_free(ctx->out);
pipe_consumer_free(ctx->in);
return NULL;
}
static pthread_t thread;
static void spawn_thread(thread_context_t* ctx)
{
pthread_create(&thread, NULL, &thread_func, ctx);
}
int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Usage: %s [N]\nN = the number of times to pass around a token.\n", argv[0]);
return 255;
}
int passes = atoi(argv[1]);
pipe_t* last_pipe = pipe_new(sizeof(int), 0);
pipe_producer_t* first = pipe_producer_new(last_pipe);
for(int i = 0; i < THREADS - 1; ++i)
{
thread_context_t* ctx = contexts + i;
ctx->threadnumber = i + 1;
ctx->in = pipe_consumer_new(last_pipe);
pipe_free(last_pipe);
last_pipe = pipe_new(sizeof(int), 0);
ctx->out = pipe_producer_new(last_pipe);
spawn_thread(ctx);
}
contexts[THREADS-1] = (thread_context_t) {
.threadnumber = THREADS,
.in = pipe_consumer_new(last_pipe),
.out = first
};
pipe_free(last_pipe);
spawn_thread(contexts + THREADS - 1);
pipe_push(first, &passes, 1);
pthread_join(thread, NULL);
return 0;
}