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 fulladvantage 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!!!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.