我正在處理一個項目,該項目需要我使用void指針存儲對pthread的所有引用,並使用包裝函數創建並取消這些線程。pthread_cancel()在傳入類型轉換void pointer時不起作用
因此我結束了以下內容:
typedef void * ThreadHandle_t;
void * endlessWhileLoop(void * p){
while(1);
}
int createThread(ThreadHandle_t * handle){
pthread_t thread;
int ret = pthread_create(&(thread), NULL, endlessWhileLoop, NULL);
if (ret != 0) {
return -1;
}
/* Configure the ThreadHandle to point to the task */
if (handle != NULL) { /* If handle was passed in */
*handle = &thread;
}
//ret = pthread_cancel(*(pthread_t *)*handle); <--This works
return ret;
}
int deleteThread(ThreadHandle_t handle){
int ret = pthread_cancel(*(pthread_t *)handle);
if(ret != 0){
printf("Failed to delete task, return code: %d", ret);
return -1;
}
return ret;
}
int main(void){
ThreadHandle_t temp = 0;
createThread(&temp);
deleteThread(temp);
}
不過,我收到找不到錯誤在deleteThread的cancel_thread調用線程。
如果我將pthread_cancel調用轉移到createThread函數中,即使在使用ThreadHandle的情況下,它也可以工作,並且線程被取消。
難道我沒有通過正確的引用傳遞使用ThreadHandle_t的pthread_t嗎?我很迷惑......
你的邏輯真的沒有任何意義。由於'temp'是一個指向void的指針,你將不得不使用它來指向'pthread_t'。但是你在哪裏分配任何'pthread_t'來指向? –