我有一个程序试图通过已实现的池使用创建和取消。
创作如下:
int threadsNum=10;
while (created<threadsNum){
pthread_t newThread;
pthread_struct *st; //Open the thread that handle the deleting of the sessions timeout.
st = (pthread_struct*)malloc(sizeof(pthread_struct));
st->id = created;
st->t = newThread;
pthread_mutex_lock( &mutex_threadsPool );
readingThreadsPool[created] = st;
pthread_mutex_unlock( &mutex_threadsPool );
if((threadRes1 = pthread_create( &newThread, NULL, pcapReadingThread, (void*)created)))
{
syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d failed.",created);
printf( "Creating Pcap-Reading Thread %d failed.\n",created);
exit(1);
}
syslog(LOG_INFO, "Created Pcap-Reading Thread %d Successfully.",created);
created++;
}
后来我尝试取消它们并重新启动它们:
pthread_t t;
pthread_struct* tstr;
int i;
pthread_mutex_unlock( &mutex_threadsPool );
//first go on array and kill all threads
for(i = 0; i<threadsNum ; i++ ){
tstr = readingThreadsPool[i];
if (tstr!=NULL){
t = tstr->t;
if (pthread_cancel(t)!=0){
perror("ERROR : Could not kill thread");
}
else{
printf("Killed Thread %d \n",i);
}
}
}
到目前为止一切顺利,但唯一的问题是输出是错误:无法杀死线程:非法寻求被杀死的线程1
杀死线程 2
杀死线程 3
杀死线程 4
杀死线程 5
杀死线程 6
杀死线程 7
杀死线程 8
杀死线程 9
为什么它也不杀死 0 索引中的线程?
而且我找不到任何关于非法搜索的信息。
感谢您的帮助人们
谢谢