退出pthread会导致分段错误

时间:2014-10-25 11:51:35

标签: c multithreading join exit

代码会产生分段错误,我不知道为什么......

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

1 个答案:

答案 0 :(得分:1)

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

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

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