我有一个大约 6 个线程 ID 的数组,我想在一个循环中取消它们。这是因为我面临某些段错误,因为这些线程在清理后试图访问一些无效内存。当我将取消类型更改为时asynchronous
,即使在线程取消之后,我仍然会遇到分段错误。如果我将取消类型更改为deferred
并将取消点保持为pthread_join
,则在 2 个线程取消后,我的代码会被 join 阻塞并且它不会退出。
你能建议问题是什么吗?
/* The cancel type is deferred and cancellation point is pthread_join. After 2
iterations, it is unable to come out of join and gets blocked. Here is the code:*/
for (int i=0;i<N_BATCH_THREADS;i++)
{
rc = pthread_cancel(g_batch_th[i]);
if(rc!=0)
{
fprintf(stderr,"Error in pthread cancel\n");
exit(1);
}
else
{
fprintf(stderr,"Thread cancelled successfully %d\n",g_batch_th[i]);
}
rc = pthread_join(g_batch_th[i],&status);
if(rc!=0)
{
fprintf(stderr,"Error in pthread join\n");
exit(1);
}
else
{
fprintf(stderr,"Return from pthread join successful %d\n",g_batch_th[i]);
}
if( status != PTHREAD_CANCELED)
{
fprintf(stderr,"Unexpected thread status \n");
exit(1);
}
}