-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread-3.cpp
46 lines (39 loc) · 946 Bytes
/
thread-3.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
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <stdio.h>
using namespace std;
bool exit_flag = false;
void *thread_func(void *threadid)
{
int num;
long long local_sum = 0;
while (scanf("%d", &num) != EOF) {
local_sum += num;
if (exit_flag) {
break;
}
sched_yield();
}
exit_flag = true;
return (void *)local_sum;
}
int main (int argc, char* argv[])
{
unsigned long n = strtoul(argv[1], NULL, 10);
unsigned long i;
pthread_t *threads = new pthread_t[n];
for(i = 0; i < n; i++) {
threads[i] = (pthread_t)i;
pthread_create(&threads[i], NULL, thread_func, (void *)threads[i]);
}
long long global_sum = 0;
for (i = 0; i < n; i++) {
void *ans;
pthread_join(threads[i], &ans);
global_sum += (long long)ans;
}
cout << global_sum << endl;
delete[] threads;
return 0;
}