Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A file named pthread-data-sharing-mutex.cpp has been provided to you in the same project. Compile the program and execute it several times, at least 10. Make

A file named pthread-data-sharing-mutex.cpp has been provided to you in the same project.

Compile the program and execute it several times, at least 10. Make sure to pay close attention to the output that the program produces.

Answer the following questions about the programs behavior:

1. What does it do?

2. What output does it produce?

3. Examine the program code carefully. Is the program functioning correctly?

4. If you do not think that the program is working correctly, describe why?

#include #include

int count;

void* myFunction(void* arg) { int actual_arg = *((int*) arg); for(unsigned int i = 0; i < 10; ++i) { count++; std::cout << "Thread #" << actual_arg << " count = " << count << std::endl; } pthread_exit(NULL); }

int main() { int rc[2]; pthread_t ids[2]; int args[2]; count = 0; for(unsigned int i = 0; i < 2; ++i) { args[i] = i; rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]); } for(unsigned int i = 0; i < 2; ++i) { pthread_join(ids[i], NULL); } std::cout << "Final count = " << count << std::endl; pthread_exit(NULL); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

What are three reasons firms choose to diversify their operations?

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago