-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththr.c
More file actions
48 lines (43 loc) · 1.04 KB
/
thr.c
File metadata and controls
48 lines (43 loc) · 1.04 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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define lock pthread_mutex_lock
#define unlock pthread_mutex_unlock
typedef struct Lines {
char *line;
struct Lines *prev;
} Lines;
struct TT {
char done;
Lines *lines;
pthread_mutex_t q;
} TT;
void *find(void *_) {
size_t len;
char *line;
Lines *lines;
FILE *p = popen("./find .", "r");
while (line = 0, -1 != getline(&line, &len, p)) {
lines = malloc(sizeof(Lines)), lines->line = line;
lock(&TT.q), lines->prev = TT.lines, TT.lines = lines, unlock(&TT.q);
}
pclose(p);
lock(&TT.q), TT.done = 1, unlock(&TT.q);
return 0;
}
int main() {
char done, once_more = 0;
pthread_t find$;
Lines *lines;
pthread_mutex_init(&TT.q, 0);
pthread_create(&find$, 0, find, 0);
while (lock(&TT.q), done = TT.done, unlock(&TT.q), !done || !once_more++) {
lock(&TT.q);
while (TT.lines) {
printf("%s", TT.lines->line), free(TT.lines->line);
lines = TT.lines->prev, free(TT.lines), TT.lines = lines;
}
unlock(&TT.q);
}
}