【问题标题】:child threads returning wrong data to main thread?子线程向主线程返回错误数据?
【发布时间】:2015-12-10 12:25:51
【问题描述】:

这是我的代码:

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);
  }
}

输出:

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

为什么两个子线程返回相同的数据?我在 pthread_exit(28262416) 中设置了不同的数据,但在这两种情况下我仍然收到相同的数据。请告诉我原因。

谢谢。

【问题讨论】:

  • "new" 在 C 中是否支持?

标签: c++ linux pthreads embedded posix


【解决方案1】:

我看到两个问题:首先两个线程都修改并返回 same 指针。

其次,线程返回一个指向指针的指针(例如int **)。

第一个问题只会给您相同的结果(但与您现在得到的结果无关)。第二个问题是什么给了你现在得到的结果(即变量child_thread_data 在内存中的位置)。

要解决这两个问题,您首先需要 两个 值来分配和返回,并返回一个指针 (int *)。

哦,锁并不重要,因为最后运行的线程总是会覆盖结果。

您也有内存泄漏,您分配内存并分配给ret_data1ret_data2,然后当pthread_join 覆盖这些指针时,您只需丢弃该内存。

我的 建议是您返回几个步骤,find a good book about C 并从指针章节重新开始。并尝试了解何时何地应该使用指针。因为现在你在不需要的时候使用指针(例如,child_thread_data 不需要成为指针)。

【讨论】:

  • 如果线程 1 先被执行,pthread_join 先被调用,然后 thread2 被执行,在这种情况下,printf 将有两个不同的值...
  • @JeegarPatel 是的,但这是一个非常大的“如果”。而且不太可能,因为控制台输出很慢,并且变量child_thread_data 在打印之前可能有时间被第二个线程修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 2012-12-25
  • 2019-02-10
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多