#include <stdint.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
sem_t sem;
void *testfunc1(void *arg)
{
while(1)
{
sem_wait(&sem);
}
}
void *testfunc2(void *arg)
{
while(1)
{
sem_wait(&sem);
}
}
void *testfunc3(void *arg)
{
while(1)
{
sem_wait(&sem);
}
}
void *testfunc4(void *arg)
{
while(1)
{
sem_wait(&sem);
}
}
void *testfunc5(void *arg)
{
while(1)
{
sem_wait(&sem);
}
}
int main()
{
int value = 0;
pthread_t ps1;
pthread_t ps2;
pthread_t ps3;
pthread_t ps4;
pthread_t ps5;
sem_init(&sem, 0, 0);
pthread_create(&ps1,NULL,testfunc1,NULL);
pthread_create(&ps2,NULL,testfunc2,NULL);
pthread_create(&ps3,NULL,testfunc3,NULL);
pthread_create(&ps3,NULL,testfunc4,NULL);
pthread_create(&ps4,NULL,testfunc5,NULL);
sleep(3);
sem_getvalue(&sem, &value); //Ubuntu6.04
printf("value = %d\n",value); //0
pthread_join(ps1,NULL);
pthread_join(ps2,NULL);
pthread_join(ps3,NULL);
pthread_join(ps4,NULL);
pthread_join(ps5,NULL);
return 0;
}
/*
sem_getvalue:
获取信号量的值,不知道什么系统下,会输出负数。。
*/
sem_getvalue函数例程
猜你喜欢
转载自blog.csdn.net/qq_38158479/article/details/114987109
今日推荐
周排行