我正在我的項目中,它是至關重要的,一切正常清理,所有緩衝的日誌消息保存到一個文件等..我打電話退出另一個線程和我正在考慮在程序完全退出之前使用信號量等待主線程中的清理。問題是,當我從退出處理程序調用sem_post並使用atexit註冊時,sem_wait不會立即減少信號量,因此,退出處理程序中的sem_wait將立即退出,因爲信號量大於零。sem_post沒有優先考慮在其他線程sem_wait調用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
static sem_t sem;
void *do_work(void *arg) {
// if something fails
printf("Something failed!\n");
exit(1);
}
void exit_handler() {
sem_post(&sem); // wake up main thread
sem_wait(&sem); // wait for cleanup in main
sem_destroy(&sem);
}
int main() {
pthread_t worker_thread;
sem_init(&sem, 0, 0);
atexit(exit_handler);
pthread_create(&worker_thread, NULL, do_work, NULL);
sem_wait(&sem); // block this thread until work is done
// simulate some cleanup
usleep(1000000);
printf("This never gets called!\n");
sem_post(&sem); // destroy semaphore
}
這個example演示我的問題。有沒有解決這個問題的方法?我不能把清理放到我的退出處理程序中,因爲我的主函數中有很多本地資源需要在我的實際程序中清理。
@Mat對不起,忘了包括。但它仍然不起作用。 – Linus
我在這裏看不到sem_ *的好處。你不能只使用pthread_join()來等待線程終止,然後進行清理嗎? – Ctx
@Ctx嗯,在這種特殊情況下,我可以。然而,我的實際程序中有很多其他線程需要終止,而其他操作必須在main中完成,因爲我不想過度使用我使用的每個資源的全局變量。 – Linus