我有一个大约6个线程id的数组,我想在一个循环中取消。这是因为我正在面对某些seg故障,因为这些线程在清理后尝试访问一些无效的内存。当我将取消类型更改为asynchronous
时,即使在取消线程后,我仍然收到分段错误。如果我将取消类型更改为deferred
,并将取消点保留为pthread_join
,则在2个线程取消后,我的代码将被连接阻止并且不会退出。取消循环中的pthreads
你能提出什么问题可以解决吗?
/* 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);
}
}
你能添加相应的代码片段吗? – 2012-04-09 08:55:35