信号量的例子
//gcc -lpthread *.c -o
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <semaphore.h>
#include <unistd.h>
int number;
sem_t sem_id;
void *thread_one_fun(void *arg)
{
sem_wait(&sem_id);
printf("thread one have the semaphore\n");
number++;
printf("number =%d\n",number);
sem_post(&sem_id);
}
void *thread_two_fun(void *arg)
{
sem_wait(&sem_id);
printf("thread two have the semaphore\n");
number--;
printf("number=%d\n",number);
sem_post(&sem_id);
}
int main(int argc,char *argv[])
{
number = 1;
pthread_t id1,id2;
sem_init(&sem_id,0,1);
pthread_create(&id1,NULL,thread_one_fun,NULL);
pthread_create(&id2,NULL,thread_two_fun,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
printf("Main...\n");
return 0;
}