【发布时间】:2011-09-04 16:51:03
【问题描述】:
如果一个信号量值为 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;
}
【问题讨论】:
-
“i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)”。如果有帮助的话。
-
顺便说一句,
pthread_exit()之后的任何代码都是死代码。该函数不返回。