Code
Code

Reputation: 85

Why are not outputs the same after re-executing my multithreading code?

When I execute the following code, after every re-compile and re-execute, the answers(outputs) are not the same. What is a reason for it?

 #include <iostream>
#include <cstdlib>
//#include <pthread.h>

using namespace std;

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ ){
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, 
                          PrintHello, (void *)i);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

g++ test.cpp -o test -lpthread ./test

Output1:

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
Hello World! Thread ID, 0
main() : creating thread, 3
Hello World! Thread ID, 1
Hello World! Thread ID, 2
main() : creating thread, 4
Hello World! Thread ID, 3
Hello World! Thread ID, 4

g++ test.cpp -o test -lpthread ./test

Output2:

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
Hello World! Thread ID, 0
main() : creating thread, 4
Hello World! Thread ID, 3
Hello World! Thread ID, 2
Hello World! Thread ID, 4
Hello World! Thread ID, 1

Upvotes: 1

Views: 83

Answers (2)

Code
Code

Reputation: 85

yes. I have used of
pthread_mutex_lock(&mutex); and pthread_mutex_unlock(&mutex); as follows

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>

using namespace std;

#define NUM_THREADS     5
pthread_mutex_t   mutex = PTHREAD_MUTEX_INITIALIZER;
void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   //sleep(1);

 cout << "Hello World! Thread ID, " << tid << endl;

pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
//pthread_attr_t attr;
//pthread_attr_init(&attr);
   //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   for( i=0; i < NUM_THREADS; i++ ){
pthread_mutex_lock(&mutex);
      cout << "main() : creating thread, " << i << endl;

      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);

//sleep(0.5);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

and my outputs is always:

main() : creating thread, 0
Hello World! Thread ID, 0
main() : creating thread, 1
Hello World! Thread ID, 1
main() : creating thread, 2
Hello World! Thread ID, 2
main() : creating thread, 3
Hello World! Thread ID, 3
main() : creating thread, 4
Hello World! Thread ID, 4

Upvotes: 0

Martin J.
Martin J.

Reputation: 5118

Since your threads aren't synchronized, the execution order is not precisely determined, and depends on your execution context, such as what other processes are running at the same time, etc, which will probably be different each time you run your program.

Upvotes: 5

Related Questions