-
Notifications
You must be signed in to change notification settings - Fork 1
/
pthread_multithreading.c
68 lines (54 loc) · 1.61 KB
/
pthread_multithreading.c
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
#include<stdio.h>
#include<pthread.h>
#define SIZE 7
#define NO_OF_THREADS 8
int a[SIZE]={0,3,5,7,10,1,2};
void* unique_pair(void *param)
{
long start = (long)param;
int i, j, k, flag;
//each thread will start one iteration of the outmost for loop and will increment by 8 since
//the rest 7 threads will do the previous 7 iterations
for(i=start; i<SIZE-1; i+=8)
{
for(j=i+1; j<SIZE; j++)
{
//flag is initially set to 1 before starting comparing
flag=1;
for(k=0; k<SIZE; k++)
{
if(a[i]<a[j] && a[k]>a[i] && a[k]<a[j] )//if a[i]<a[j] and a[k] is in bet a[i] and a[j]
{
//flag = 0 if value any value is found between a[i] and a[j]
flag=0;
break;
}
else if(a[j]<a[i] && a[k]<a[i] && a[k]>a[j])//if a[i]>a[j] and a[k] is in bet a[i] and a[j]
{
//flag = 0 if value any value is found between a[i] and a[j]
flag=0;
break;
}
}
//if flag still stays 1 throughout the iterations that means no value is found in bet a[i] and a[j]
//and a[i] and a[j] are unique pairs
if(flag ==1)
printf("(%d,%d) or (%d,%d) = %d\n",a[i],a[j],a[j],a[i],abs(a[i]-a[j]));
}
}
return NULL;
}
int main()
{
pthread_t thread[NO_OF_THREADS];
int i;
for(i =0; i<SIZE; i++)
printf("%d\t",a[i]);
printf("\n");
long j;
for(j=0; j<NO_OF_THREADS; j++)
pthread_create(&thread[j], NULL, unique_pair, (void *)j);//each thread will be created and will start executing unique_pair()
for(j=0; j<NO_OF_THREADS; j++)
pthread_join(thread[j], NULL);//all the threads created after completing the execution of the function will combine to form the main thread
return 0;
}