c,正确使用线程

时间:2011-08-11 09:42:28

标签: c pthreads grand-central-dispatch

当你使用thread_create创建线程并传递函数时,如果函数中存在无限循环,函数是否会永远存在? 例如

for(;;){
    //dosomthing
}

在线程被销毁或程序完成之前,线程是否保持“做某事”?

感谢名单

1 个答案:

答案 0 :(得分:1)

当你调用thread_create()时,创建的线程本身会调用你传递的函数。例如:

pthread_t thread1;
pthread_create(&thread1, NULL, thread_do, NULL);

将创建一个新线程,新线程将运行函数thread_do()。 如果现在您已经将thread_do()定义为:

void* thread_do(){

  for(;;){
    // do something
  }

  return NULL;
}
然后,线程确实会进入无限循环。