Q. modify the Code Using set and test or swap?   #include #include #include long long sum=0,num=2000000; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void *mySumFun(void *vargp) { int step=*(int *) vargp; for(int i=0;i

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter2: Using Data
Section: Chapter Questions
Problem 17RQ: When you perform arithmetic operations with operands of different types, such as adding an int and a...
icon
Related questions
Question

Q. modify the Code Using set and test or swap?

 

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

long long sum=0,num=2000000;

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(&tid1, NULL, mySumFun, &step1);

pthread_create(&tid2, NULL, mySumFun, &step2);
pthread_join(tid2, NULL);
pthread_join(tid1, NULL);
printf("The sum is = %lld \n", sum);
exit(0);
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,