我有一个程序尝试使用create和cancel来通过已实现的池。pthread_cancel总是崩溃
创建如下:
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;
//Reaches here :-)
if (pthread_cancel(*t)!=0){
perror("ERROR : Could not kill thread");
}
else{
printf("Killed Thread %d \n",i);
}
//doesnt reach here
}
}
我检查在创建线程的内存地址中的第一部分和地址的第二部分即将被取消的线程..他们匹配.. 我读过关于线程管理器,如果有人叫killall()不能工作。
但我不..
任何人有什么想法?
感谢
这不是C吗?我在发布的代码中没有看到特定的C++。 – hmjd