-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread3.cpp
More file actions
45 lines (36 loc) · 840 Bytes
/
thread3.cpp
File metadata and controls
45 lines (36 loc) · 840 Bytes
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
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void* thread_summation(void* arg);
int sum = 0;
// struct range
// {
// /* data */
// int num1[2] = { 1, 5 };
// int num2[2] = { 6, 10 };
// }range;
int main()
{
pthread_t t_id1, t_id2;
int range1[] = { 1, 5 };
int range2[] = { 6, 10 };
int sum1 = 0, sum2 = 0;
pthread_create(&t_id1, NULL, thread_summation, (void*)range1);
pthread_create(&t_id2, NULL, thread_summation, (void*)range2);
pthread_join(t_id1, NULL);
pthread_join(t_id2, NULL);
cout << "result: " << sum << endl;
return 0;
}
void* thread_summation(void* arg)
{
int start = ((int*)arg)[0];
int end = ((int*)arg)[1];
while (start <= end) {
sum += start;
++start;
}
return NULL;
}