#include "semaphore.h"
#include "pthread.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int count = 0;
sem_t sem;
void *readData(void *arg)
{
while(true)
{
sem_wait(&sem);
printf("%d\n", count);
}
return NULL;
}
void *writeData(void *arg)
{
while(true) {
_sleep(1000);
count++;
sem_post(&sem);
sem_post(&sem);
}
return NULL;
}
int main()
{
pthread_t thread[2];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
sem_init(&sem,0,0);
pthread_create(&thread[0],&attr,readData,NULL);
pthread_create(&thread[1],&attr,writeData,NULL);
_sleep(10000);
system("pause");
return 0;
}
semaphore
最新推荐文章于 2023-05-09 13:09:11 发布