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;
void *mySumFun(void *vargp){
int step=*(int *) vargp;
for(int i=0;i<num;i++){
TestAndSet(&lock);
sum+=step;
lock = false;
}
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);
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run