Question: Considering the following code int y = 3; x = 1; void foo () { y = y + 1; x = y; } void

Considering the following code

int y = 3; x = 1;

void foo () {

y = y + 1;

x = y;

}

void bar () {

y = y * 2;

}

main () {

Thread t1 = createThread(foo);

Thread t2 = createThread(bar);

WaitForAllDone();

cout << x << y <

}

Note that function WaitForAllDone blocks the main thread until both threads it creates are done. Assume that memory load and memory store are atomic, but add and time are not atomic. Note,

y = y + 1, consists of three assemby instructions :

Load R1, y

Add R1, 1, R1

Store R1, y

x = y, consists of two assemby instructions :

Load R1, y

Store R1, x

y = y * 2, consists of three assemby instructions :

Load R1, y

Mul R1, 2, R1

Store R1, y

Registers not shared between the threads.

a.) What is the maximum number of threads that are ever alive (this includes those one the ready or waiting lists, or running) in this program?

b.) Give all possible outputs of this program. Explain your answer and be specific.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!