C
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
long long sum=0,num=40000000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *mySumFun(void *vargp){
int step=*(int *) vargp;
for(int i=0;i<num;i++){
pthread_mutex_lock(&mutex);
sum+=step;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t tid1,tid2;
int step1=1,step2=-1;
pthread_create(&tid2, NULL, mySumFun, &step2);
pthread_create(&tid1, NULL, mySumFun, &step1);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run