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
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
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...
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
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
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.Recommended textbooks for you
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,