-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
52 lines (41 loc) · 1 KB
/
test.c
File metadata and controls
52 lines (41 loc) · 1 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
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include "cb.h"
void *read_and_print(void *vargp)
{
clock_t last_update = clock();
byte out;
while(true)
{
if(read_buffer((circular_buffer*) vargp, &out) == OK_SIGNAL)
{
printf("%c", out);
last_update = clock();
}
if(((clock() - last_update) * 1000 / CLOCKS_PER_SEC) > 1000)
break;
}
}
void *write(void *vargp)
{
char str[] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'};
write_buffer((circular_buffer*) vargp, str, 14);
}
int main(int argc, char** argv)
{
circular_buffer cb;
if(init_circular_buffer(512, &cb) != OK_SIGNAL)
return ERROR_SIGNAL;
pthread_t thread_id_read;
pthread_create(&thread_id_read, NULL, read_and_print, (void*)&cb);
pthread_t thread_id_write;
for(int i = 0; i < 10; i++)
{
pthread_create(&thread_id_write, NULL, write, (void*)&cb);
pthread_join(thread_id_write, NULL);
}
pthread_join(thread_id_read, NULL);
destroy_circular_buffer(&cb);
return OK_SIGNAL;
}