Question: IVIC ope In main() we first declare a variable called mythread, which has a type of pthread_t. This is essentially an ID for a thread.

 IVIC ope In main() we first declare a variable called mythread,

IVIC ope In main() we first declare a variable called mythread, which has a type of pthread_t. This is essentially an ID for a thread. Next, the if statement creates a thread associated with mythread. The call pthread_create() re- turns zero on success and a nonzero error code on failure. The third argument of pthread_create() is the name of a function that the new thread will ex- ecute when it starts. When this thread_function() returns, the thread terminates. Meanwhile the main program itself defines a thread, so there are two threads executing. The pthread_join() function causes the main thread to wait until the new thread completes. Questions 1. What does this program accomplish? Please describe what happens with the global value myglobal. What does the thread_function() do and when does it run? What is the main() funciton doing? What is sleep () and what purpose does it serve? 2. Here is output from the above program being executed $ ./thread2 ..0.0.0.0.00.0.0.0.0.0.0.0.0.0.0.0.0.00 my global equals 21 Is this the output you would expect? If not, what has gone wrong and how would you fix it (please show code)? IVIC ope In main() we first declare a variable called mythread, which has a type of pthread_t. This is essentially an ID for a thread. Next, the if statement creates a thread associated with mythread. The call pthread_create() re- turns zero on success and a nonzero error code on failure. The third argument of pthread_create() is the name of a function that the new thread will ex- ecute when it starts. When this thread_function() returns, the thread terminates. Meanwhile the main program itself defines a thread, so there are two threads executing. The pthread_join() function causes the main thread to wait until the new thread completes. Questions 1. What does this program accomplish? Please describe what happens with the global value myglobal. What does the thread_function() do and when does it run? What is the main() funciton doing? What is sleep () and what purpose does it serve? 2. Here is output from the above program being executed $ ./thread2 ..0.0.0.0.00.0.0.0.0.0.0.0.0.0.0.0.0.00 my global equals 21 Is this the output you would expect? If not, what has gone wrong and how would you fix it (please show code)

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!