Below is my code snippet.
int main ( )
{
some instructions;
while ( 1 )
{
/* Block 1 : Starts*/
if ( selection == 1 )
{
ret = pthread_create ( &tid, NULL, &select_n_process_req, NULL );
if ( ret != 0 )
{
printf ("Error Creating Thread");
}
}
/* Block 1 : Ends*/
/* Block 2 : Starts*/
printf ("Sleeping for [%d]", retry_time * 60 * 60);
for ( i = 0; i < retry_time * 60 * 60; i++ )
{
if ( stop_flag == 1 )
{
printf ("Process Stopped\n");
break;
}
sleep ( 1 );
}
/* Block 2 : ENDS*/
}
some instructions;
return SUCCESS;
}
int select_n_process_req ( void )
{
some instructions;
return SUCCESS;
}
代码说明:
- block 1:变量“selection”始终为 1,并且线程 总是为了执行某些任务而创建。
- block 2:线程创建后,该 block 将等待 再次“retry_time(通常会1小时)”创建一个新线程 完成同样的任务。
问题:
- 我没有调用任何 pthread_join 或 pthread_detach,但我 从函数返回(这将终止线程) “选择_n_进程_请求”。这样会不会清空分配的资源 pthread_create?
- 我发现奇怪的是,经过一些加载后,pthread_create 没有 根本没有打电话。这是因为我没有使用 pthread_detach 或 pthread_join?
谢谢。
最佳答案
您必须调用pthread_join(从父线程)或pthread_detach,或者通过将附加属性传递给pthread_create来创建处于分离状态的线程强>。参见这里:http://man7.org/linux/man-pages/man3/pthread_create.3.html
关于c - 不使用pthread_detach或pthread_join,不会为其他新创建的线程清理资源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816518/