Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include using namespace std; int sum; const int num _ threads = 4 ; void foo ( int val ) { for ( int i

#include using namespace std; int sum; const int num_threads =4; void foo(int val){ for(int i =0; i <5; i++){ sum = sum + val; }} void bar_1(){ thread t[num_threads]; for (int i =0; i < num_threads; i++){ t[i]= thread(foo, i +1); t[i].join(); } cout << "Sum is "<< sum << endl; } void bar_2(){ thread t[num_threads]; for (int i =0; i < num_threads; i++){ t[i]= thread(foo, i +1); } cout << "Sum is "<< sum << endl; for (int i =0; i < num_threads; i++){ t[i].join(); }} void bar_3(){ thread t[num_threads]; for (int i =0; i < num_threads; i++){ t[i]= thread(foo, i +1); } for (int i =0; i < num_threads; i++){ t[i].join(); } cout << "Sum is "<< sum << endl; } int main(){ sum =0; bar_1(); sum =0; bar_2(); sum =0; bar_3(); } a) The code on the left outputs three values for sum (via the cout statements in the bar_1, bar_2, and bar_3 functions). sum is a global variable and see that its value is set to 0 before each call of bar functions. What is the maximum possible value one can see on the screen? (Hint: The maximum is the same for all outputs)._____________ b)(10 pts) What is the minimum possible value for each output? (Hint: The minimum values are different for different bar function calls). bar_1: Sum is (Min)________ bar_2: Sum is (Min)________ bar_3: Sum is (Min)________

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions

Question

What is global software development?

Answered: 1 week ago