如果信号量的值是0,而你在等待它,我总是认为线程阻塞了。为什么下面的代码没有阻塞。
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sA;
void* funcA(void* param) {
sem_wait(&sA);
printf("Thread A\n");
pthread_exit(0);
}
int main() {
sem_init(&sA, 0, 0);
pthread_t tA;
pthread_create(&tA, NULL, funcA, NULL);
pthread_exit(0);
sem_destroy(&sA);
return 0;
}发布于 2011-09-05 00:57:58
来自man sem_destroy:
Destroying a semaphore that other processes or threads are currently
blocked on (in sem_wait(3)) produces undefined behavior.看起来你的实现和我的实现对于如何处理这种未定义的行为采取了不同的选择。不过,任何事都行。
https://stackoverflow.com/questions/7300716
复制相似问题