forked from Sinh9x0-zz/multithreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvowel_count.cpp
More file actions
85 lines (73 loc) · 1.85 KB
/
vowel_count.cpp
File metadata and controls
85 lines (73 loc) · 1.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<fstream>
#include<pthread.h>
#include<string>
#include<sstream>
using namespace std;
int num_threads = 20;
pthread_mutex_t my_mutex;
int a=0,e=0,ii=0,o=0,u=0;
void* vow_count(void* temp){
ifstream infile;
string *temp_ss = (string *)temp;
string temp_s = *temp_ss;
//delete temp_ss;
pthread_mutex_lock(&my_mutex);
cout <<"got file "<<(*temp_ss)<<endl;
cout <<"processing file " <<(temp_s)<<endl;
infile.open((temp_s.c_str()));
if(!infile){
cout <<"file "<<temp_s<<" not opened"<<endl;
pthread_exit(NULL);
}
char vowel;
while(infile >> noskipws >> vowel){
switch(vowel){
case 'a': a++;break;
case 'e': e++;break;
case 'i': ii++;break;
case 'o': o++;break;
case 'u': u++;break;
case 'A': a++;break;
case 'E': e++;break;
case 'I': ii++;break;
case 'O': o++;break;
case 'U': u++;break;
}
}
infile.close();
pthread_mutex_unlock(&my_mutex);
pthread_exit(NULL);
}
int main(int argc,char** argv){
if(argc <=1 ){
cout <<"give file names"<<endl;
return -1;
}
num_threads = argc-1;
pthread_t thread[num_threads];
pthread_mutex_init(&my_mutex,0);
string filename[argc];
for(int i=1;i<argc;i++){
filename[i] = argv[i];
string *temp = &filename[i];
cout <<(*temp)<<endl;
if(pthread_create(&thread[i-1],NULL,vow_count,(void*)temp)){
cout <<"error in creating thread no "<<i<<endl;
return -1;
}
}
for(int i=0;i<num_threads;i++){
if(pthread_join(thread[i],NULL)){
cout <<"unable to join threads"<<endl;
return -1;
}
}
pthread_mutex_destroy(&my_mutex);
cout <<"A = "<<a<<endl;
cout <<"E = "<<e<<endl;
cout <<"I = "<<ii<<endl;
cout <<"O = "<<o<<endl;
cout <<"U = "<<u<<endl;
return 0;
}