【问题标题】:SegFault after the call of pthread_join调用 pthread_join 后的 SegFault
【发布时间】:2015-04-26 13:21:37
【问题描述】:

我是线程新手,使用 pthread_join 时遇到问题。

这是我的代码:

int             create_threads(t_lemin *lem)                                                  
{                                                                                             
  int           i;                                                                            
  pthread_t     **threads;                                                                    
  int           j;                                                                            
  void          *ret;                                                                         
  t_tree        *tmp;                                                                         

  j = -1;                                                                                     
  printf("starting algo\n");                                                                  
  threads = xmalloc(sizeof(pthread_t));                                                       
  i = count_leaf(lem->start);                                                                 
  tmp = lem->start;                                                                           
  printf("%d\n", tmp->visited[0]);                                                            
  while (++j != i)                                                                            
    {                                                                                         
      tmp->leaf[j]->thread_nbr = j;                                                           
      pthread_create(threads[j], NULL, find_way_out, tmp->leaf[j]);                           
      usleep(100);                                                                            
    }                                                                                         
  i = -1;                                                                                     
  while (++j != i)                                                                            
    (void)pthread_join (*threads[j], &ret);                                                   
  //printf("%d\n", *((int*)ret));                                                             
    return (0);                                                                               
} 

我的一个线程完成了他的工作,在第二个线程完成后,我遇到了分段错误。 我的函数 find_way_out 返回 pthread_exit((void*)j);或 pthread_exit(0); 其中 j 是一个整数指针。 你知道它来自哪里吗?

谢谢!

【问题讨论】:

  • 什么是 xmalloc?你在哪里为threads 元素分配内存?
  • xmalloc 只是一个验证 malloc 并在 malloc 失败时退出的函数。它返回 void * .

标签: c


【解决方案1】:

问题是你应该将一个指向实际pthread_t 变量的指针作为pthread_create 的第一个参数传递给pthread_create,作为模拟按引用传递的一种方式,然后pthread_create 将初始化对象。

由于您传递了一个未初始化的指针,当pthread_create 取消引用该指针时,您将拥有undefined behavior。当您尝试取消引用 pthread_join 调用的指针时,也会拥有它。

更糟糕的是,因为你只为一个 pthread_t 对象分配空间,所以你会超出“数组”的范围。

然后对于 pthread_join 循环,j 是一个小的正数,而 i 是一个负数,这意味着您的循环将迭代很多,因为它有迭代直到j 溢出并变为负数,然后继续循环直到j 等于-1

相反,我建议进行以下更改:

  • pthread_t 对象使用可变长度数组
  • 将数组的“大小”保存在您不会更改的变量中
  • 最后,改用 for 循环

类似

size_t count = count_leaf(lem->start);
pthread_t threads[count];

for (size_t i = 0; i < count; ++i)
    pthread_create(&threads[i], ...);

...

for (size_t i = 0; i < count; ++i)
    pthread_join(threads[i], NULL);

【讨论】:

  • 感谢您的建议,我会申请的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多