退出pthread会导致分段错误

桃木

该代码创建了一个分段错误,我不知道为什么...

pthread_t thread[1];

void Thread_without_function()
{
  int rc;
  rc = pthread_create(&thread[0], NULL, NULL, NULL);
  if(rc == 0)
    printf("Thread created.\n");
  else
    printf("Thread creating failed!(ret = %d)\n", rc);
}

int main(int argc, char const *argv[]) {

  Thread_without_function();
  sleep(10);
  pthread_join(thread[0], NULL);
  return 0;
}

以下几行是输出:

Thread created.
Segmentation fault
2501

您必须将函数(具有正确的类型)传递给pthread_create传递null会导致您的细分错误。

void* Func( void* param )
{
    return param ;
}

rc = pthread_create(&thread[0], NULL, Func, NULL);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章