我有一個程序嘗試使用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