0

This is my code:

pthread_t client_thread1, client_thread2;
int *child_thread_data = new int;

pthread_mutex_t testlock;

typedef struct
{
  int thread_no;
  char thread_name[100];
} thrdata;

int main()
{
  void *ret_data1 = new
  int[10];
  void *ret_data2 = new
  int[10];

  thrdata thr1;
  thrdata thr2;

  thr1.thread_no = 10;
  strcpy((thr1.thread_name), "thread one");
  thr2.thread_no = 20;
  strcpy((thr2.thread_name), "thread two");

  pthread_create(&client_thread1, &attr, &client_handler, &thr1);
  pthread_create(&client_thread2, &attr, &client_handler, &thr2);

  cout << "value of ret_data1 before thread 1 join " << *(int *) ret_data1
      << endl;
  cout << "value of ret_data2 before thread 2 join  " << *(int *) ret_data2
      << endl;

  pthread_join(client_thread1, &ret_data1);
  cout << "value returned by thread1 is " << *(int *) ret_data1 << endl;

  pthread_join(client_thread2, &ret_data2);
  cout << "value returned by thread2 is " << *(int *) ret_data2 << endl;

  return 0;
}

void *client_handler(void *arg)
{
  pthread_mutex_lock(&testlock);

  thrdata *client_thd;
  client_thd = (thrdata *) arg;

  cout << "inside client_handler value of private data no  is \n"
      << client_thd->thread_no << endl;
  cout << "inside client_handler value of private data name is \n"
      << client_thd->thread_name << endl;

  if (pthread_equal(client_thread1, pthread_self()))
  {
    *child_thread_data = 100;
    pthread_mutex_unlock(&testlock);
    pthread_exit((void *) &child_thread_data);
  }

  else if (pthread_equal(client_thread2, pthread_self()))
  {
    *child_thread_data = 200;
    pthread_mutex_unlock(&testlock);
    pthread_exit((void *) &child_thread_data);
  }
}

output:

value of ret_data1 before thread 1 join 0
value of ret_data2 before thread 2 join  0


inside client_handler value of private data no is 20
inside client_handler value of private data name is thread two


calling pthread_exit of thread2 and value of child_thread_data is 200


inside client_handler value of private data no  is 10
inside client_handler value of private data name is thread one

calling pthread_exit of thread1 and value of child_thread_data is 100

value returned by thread1 is 28262416
value returned by thread2 is 28262416

Why two child threads are returning the same data? I am setting different data in pthread_exit(28262416) still I am receiving same data in both cases. Please let me know the reason.

Thanks.

alk
  • 69,737
  • 10
  • 105
  • 255

1 Answers1

2

I see two problems: First of all both threads modify and returns the same pointer.

Secondly, the threads returns a pointer to the pointer (e.g. int **).

The first problem will just give you the same result (but not related to the results you get now). The second problem is what gives you the result you get now (which is the address of where the variable child_thread_data is located in memory).

To fix both those problems you first of all need two values to assign and return, and return a single pointer (int *).

Oh, and the lock doesn't really matter, as the last running thread will always overwrite the result anyway.

You also have a memory leak, you allocate memory and assign to ret_data1 and ret_data2 then you simply discard that memory when pthread_join overwrites those pointers.

My big suggestion is that you go back a couple of steps, find a good book about C and start over with the chapter on pointers. And also try to understand when and where pointers should be used. because right now you use pointers when you don't need to (no need for child_thread_data to be a pointer for example).

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • if thread 1 is executed first and also pthread_join called first and then thread2 is executed then, in this case, printf will have two different value... – Jeegar Patel Dec 10 '15 at 12:39
  • @JeegarPatel Yes but that's a very big "if". And very unlikely since console output is slow and the variable `child_thread_data` will probably have time to be modified by the second thread before it's printed. – Some programmer dude Dec 10 '15 at 12:43