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.
 
     
     
    