代码创建了一个段错误,我不知道为什么......
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
最佳答案
您必须将一个函数(具有正确的类型)传递给pthread_create
。传递 null 会导致段错误。
void* Func( void* param )
{
return param ;
}
rc = pthread_create(&thread[0], NULL, Func, NULL);
关于c - 退出 pthread 会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26562041/