#include "so_test.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t bin_sem;
void *thread_function1(void *arg)
{
printf("thread_function1--------------sem_wait\n");
sem_wait(&bin_sem);
printf("sem_wait\n");
exit(1);
}
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = sem_init(&bin_sem, 0, 0);
if (res != 0)
{
perror("Semaphore initialization failed");
}
printf("sem_init\n");
res = pthread_create(&a_thread, NULL, thread_function1, NULL);
if (res != 0)
{
perror("Thread creation failure");
}
printf("thread_function1\n");
int count = 0;
while (1)
{
count++;
sem_post(&bin_sem);
printf("%d\n", count);
}
return 1;
}
sem_wait估计是按某种心跳来检查 ,
当发送大量sem_post或者cpu负荷高的时候
就会有明显的延迟触发现象,
随手测试下可以跳出去5千多才被触发