c - 退出 pthread 会导致段错误

标签 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

最佳答案

您必须将一个函数(具有正确的类型)传递给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/

相关文章:

c - "(datatype) (*ptrname) (datatype)"是什么意思?

c++ - 如何找到激活时打开给定 HMENU 的菜单项(如果有)?

c# - 时间限制 C# 中的方法

mysql - 在 mysql JOIN 上使用 OR

mysql - 简单且 Eloquent 地加入 laravel 5.3

c++ - C 中的 typedef、DLLEXPORT、__stdcall 帮助

谁能告诉我c中的列表文件和映射文件是什么?

c - 适本地存储线程特定变量

java - 禁用父框架而不停止线程?

Java Hibernate 注解(@JoinTable、@Filter)