Question: Consider the following code using the POSIX Pthreads API: thread2.c #include #include #include #include int myglobal; void *thread_function(void *arg) { int i,j; for ( i=0;

Consider the following code using the POSIX Pthreads API: thread2.c

#include #include #include #include

int myglobal;

void *thread_function(void *arg) { int i,j;

for ( i=0; i<20; i++ ) { j=myglobal; j=j+1; printf(.); fflush(stdout); sleep(1); myglobal=j; }

return NULL;

}

int main(void) {

pthread_t mythread; int i;

if ( pthread_create( &mythread, NULL, thread_function,NULL) ) { printf(ldquo;error creating thread.); abort(); }

for ( i=0; i<20; i++) { myglobal=myglobal+1; printf(o); fflush(stdout); sleep(1); }

if ( pthread_join ( mythread, NULL ) ) { printf(error joining thread.); abort(); }

printf( myglobal equals %d ,myglobal); exit(0); }

Explain what code is doing. comments within the code are acceptable

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!