-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_c.c
More file actions
72 lines (58 loc) · 1.64 KB
/
thread_c.c
File metadata and controls
72 lines (58 loc) · 1.64 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
#include "include/thread_control.h"
file_node flist;
counter ctr;
sem_t work_lock;
void* pasrse_thread(void* arg){
char *path = (char *)arg;
find_files(path, &flist);
}
void* counter_thread(void *arg){
char *file;
bool finished = false;
while (true)
{
sem_wait(&work_lock);
if(flist.work_at >= flist.count){
finished = true;
}
else{
file = flist.file_paths[flist.work_at];
flist.work_at++;
}
sem_post(&work_lock);
if(finished){
break;
}
single_file_count(file, &ctr);
}
return NULL;
}
void pcenter(char const* path){
pthread_t fp_parse;
init_file_list(&flist, 10);
init_counter(&ctr);
sem_init(&work_lock, 0, 1);
sem_init(&write_lock, 0, 1);
pthread_create(&fp_parse, NULL, pasrse_thread, (void *)path);
pthread_join(fp_parse, NULL);
printf("-----------------------------------------------\n");
printf("Total file %8d \n", flist.count);
printf("-----------------------------------------------\n");
pthread_t counters[8];
for(int i = 0; i < 8; i++){
pthread_create(&counters[i], NULL, counter_thread, NULL);
}
for(int i = 0; i < 8; i++){
pthread_join(counters[i], NULL);
}
char *sp, *bk, *ct, *ce, *tt, *sm;
sp = "";
bk = "Blank";
ct = "Comment";
ce = "Code";
tt = "Total";
sm = "Sum";
printf("%3s %10s %10s %10s %10s\n",sp, bk, ct, ce, tt);
printf("%-3s %10d %10d %10d %10d\n", sm,ctr.blank, ctr.comment, ctr.code, ctr.line);
printf("-----------------------------------------------\n");
}