Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output value of myglobal? Does this program take full advantage of t

ID: 3645327 • Letter: W

Question

What is the output value of myglobal? Does this program take full
advantage of threads?
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>
#include <iostream>
using namespace std;
int myglobal;
void *thread_function(void *arg) {
int i,j;
for ( i=0; i<20; i++ ) {
j=myglobal;
j=j+1;
cout << "." << endl;
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void) {
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
cout << "error creating thread." << endl;
return -1;
}
if ( pthread_join ( mythread, NULL ) ) {
cout << "error joining thread." << endl;
return -1;
}
for ( i=0; i<20; i++) {
myglobal=myglobal+1;
cout << "o" << endl;
sleep(1);
}
cout << " myglobal equals " << myglobal << endl;
return 0;
}

Explanation / Answer

myglobal = 20 This program does not take full advantage of threads...as full advantage of threads come when we use more than 1 thread i.e. multithreading...one of the best example of multithreading is SOCKET PROGRAMMING!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote