Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program whose main function creates a second thread and then use a global variable to have the two threads ( main is always

Write a program whose main function creates a second thread and then use a global variable to have the two threads ( main is always the starting point of the first thread, no?) communicate with each other. In main, after the (successful) call to pthread_create, have main prompt the user for a non-zero integer and store that non-zero value in the global variable so that the second thread can see it (since all threads of a process share the same heap and global sections). Then have main spin while the global variable is not zero i.e., until something external to the main thread (like the second thread) sets the global variable back to zero. Spin? A thread is said to spin when it deliberately enters an apparently infinite loop until the hardware or some other process or thread, executing concurrently, changes the condition so that the spinning thread can exit its loop (which therefore wasn't really infinite after all, but it looks like it could go on forever, and in the absence of concurrency, it could and would). In the second thread, after it prints out its TID, have the second thread spin while the global varable is 0 thus "waiting" (technically, "busy waiting") to receive the non-zero value from the main thread. Once the second thread stops spinning, have it print out the value that caused it to exit from the loop and then set the global variable back to 0 so the main thread can exit its spin and terminate. This busy-waiting is, of course a very inefficient method of synchronizing our concurrent threads.

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

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago