-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_thread_multi_file.cpp
58 lines (44 loc) · 1.2 KB
/
multi_thread_multi_file.cpp
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
#include <sstream>
#include <pthread.h>
#include <fstream>
#include <cstring>
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;
clock_t mid,end_time;
#define THREAD_NUM 64
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
struct param{
int tidx;
};
void * std_output(void *pa){
struct param *local = (struct param *) pa;
int tidx = local->tidx;
int count = 0;
std::string filename = std::to_string(tidx)+".txt";
cout << filename <<endl;
ifstream file(filename);
string line;
while(!file.eof()&&file.is_open()){
getline(file,line);
count++;
}
cout << " thread " << tidx << " has " << count << " lines" << endl;
}
int main(){
mid = clock();
pthread_t threads[THREAD_NUM];
for(int i = 0; i < THREAD_NUM; i++){
struct param *str_param = new param();
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_time = clock();
double endtime = (double)(end_time-mid)/CLOCKS_PER_SEC;
cout << "endtime "<<endtime<<endl;
return 0;
}