0

I wrote a simple program using pthread but my results are random....

#define NTHREADS 2
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;


void *add(void* numbers){
  pthread_mutex_lock( &mutex1 );

  int *n = (int*) numbers;
  float sum;
  for(int i = 0; i < 5; i++){
     sum = sum + n[i] +5;
  }
  cout << sum/5<<endl;

  pthread_mutex_unlock( &mutex1 );

}


void *substract(void* numbers){
  pthread_mutex_lock( &mutex1 );

  int *n = (int*) numbers;
  float sum;
  for(int i = 0; i < 5; i++){
   sum = sum + n[i] -10;
  }
  cout << sum/5<<endl;

  pthread_mutex_unlock( &mutex1 );
}
main(){
  pthread_t thread_id[NTHREADS];
  int i, j;

  int *numbers = new int[5];
  numbers[0] = 34; numbers[1] = 2; numbers[2]= 77; numbers[3] = 40; numbers[4] = 12;
  pthread_create( &thread_id[0], NULL, add, (void*) numbers);
  pthread_create( &thread_id[1], NULL, substract, (void*) numbers );

  pthread_join( thread_id[0], NULL);
  pthread_join( thread_id[1], NULL);

  exit(EXIT_SUCCESS);
}

The output of the program is random....Sometimes it got

 -2.42477e+26
  23

Sometimes it got only one strange number such as

 235.69118e+13
 (empty space)

I have also tried only to use one thread, but the result is still random. For example, I only used thread to calculate "add", the result is sometimes 38, which is correct, but sometimes is a very strange number.

Where I did wrong? Thank you .

3
  • Welcome to SO. So let's be clear. You have verified the "random" result when using this code with NTHREADS=1 or with all the thread code removed? Commented Dec 7, 2015 at 21:33
  • 3
    subtract attempts to unlock the mutex twice instead of locking it then unlocking it. Commented Dec 7, 2015 at 21:34
  • 2
    Your need to make sure you return a value from your function. You currently do not. Commented Dec 7, 2015 at 21:34

2 Answers 2

1

The reason for random numbers, as I told you in your previous question, is that you do not initialize your sum before using. There are other issues with your code as well (see comments), but they are not directly responsible for the random result.

You also do not need to use any mutex at all in your current code. As a matter of fact, by using mutex you made your application effectively single-threaded, dumping all multithreading benefits. The only place where you might need a mutex is right before and after cout call - to ensure the output is not intertwined.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answering both of my questions......It is my bad that I did not initialize "sum"! This is just a simple testing program to learn pthread so that's why I have to use it. After initializing sum, sometimes I got "38 23" and sometimes I got "23 38". Why this happens? Is it because the time cost on each function is different?
It's because the order in which your threads are executing is not defined. Sometimes the first thread will be executed first (and grab the mutex first, making another thread to wait), sometimes the second will be the first.
Why is there an "order" of execution? Aren't they synchronized and supposed to start running at the same time? (Sorry I am new with pthread so I am really confused)
What you have written right now is two threads, starting at (approximately!) same time (they can start exactly at the same time, as they are started one after another, but the interval between those starts is very small), which than begin executing and after delay based on multituted of factors try to grab a mutex. This delay is unpredictable, it is based on general computer state (to simplify things). The first thread to grab the mutex wins, the second has to wait.
I see! So is there any way that I can make two or more thread functions end at the same time?
|
0

There are various things you need to fix in your code, but the most "burning issue", and the one causing you the problems is using an uninitialized variable -> sum.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.