-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_read_vector.cpp
More file actions
88 lines (70 loc) · 1.91 KB
/
multi_read_vector.cpp
File metadata and controls
88 lines (70 loc) · 1.91 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
86
87
88
#include <pthread.h>
#include <fstream>
#include <cstring>
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;
clock_t start,mid,end;
#define THREAD_NUM 8
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
struct param{
vector<string> vstr;
int offset;
int tidx;
};
void * std_output(void *pa){
struct param *local = (struct param *) pa;
vector<string> vstr = local->vstr;
int offset = local->offset;
int tidx = local->tidx;
pthread_mutex_lock(&count_mutex);
int count = 0;
int i = offset;
while(i < vstr.size()){
//if (tidx==5){
// cout << i << endl;
//}
count++;
i+=THREAD_NUM;
}
cout << " thread " << tidx << " has " << count << " lines" << endl;
pthread_mutex_unlock(&count_mutex);
}
int main(){
start = clock();
ifstream input_data("file.txt");
int line_num = 0;
string line;
vector<string> vstring;
while(input_data.is_open()){
if(!input_data.eof()){
getline(input_data, line);
vstring.push_back(line);
line_num++;
}
else{
cout << "total lines: "<< line_num << endl;
break;
}
}
input_data.close();
mid = clock();
//int LINE_PER_THREAD = line_num / THREAD_NUM;
pthread_t threads[THREAD_NUM];
for(int i = 0; i < THREAD_NUM; i++){
struct param *str_param = new param();
str_param->vstr = vstring;
str_param->offset = i;
str_param->tidx = i;
pthread_create(&threads[i], NULL, std_output, (void *) str_param);
}
for(int i = 0; i < THREAD_NUM; i++)
pthread_join(threads[i], NULL);
end = clock();
double endtime = (double)(end-start)/CLOCKS_PER_SEC;
double midtime = (double)(mid-start)/CLOCKS_PER_SEC;
cout << "midtime: "<<midtime<<"endtime "<<endtime<<endl;
return 0;
}