day29练习:
编写3个线程任务,线程1循环打印"A",线程2循环打印"B",线程3循环打印"C",要求打印出的字符顺序总为ABC
#include "../head.h"
sem_t sem_a;
sem_t sem_b;
sem_t sem_c;
void *thread1(void *arg)
{
while (1)
{
sem_wait(&sem_a);
printf("A");
sem_post(&sem_b);
}
return NULL;
}
void *thread2(void *arg)
{
while (1)
{
sem_wait(&sem_b);
printf("B");
sem_post(&sem_c);
}
return NULL;
}
void *thread3(void *arg)
{
while (1)
{
sem_wait(&sem_c);
printf("C");
sem_post(&sem_a);
}
return NULL;
}
int main(void)
{
pthread_t tid1;
pthread_t tid2;
pthread_t tid3;
sem_init(&sem_a, 0, 1);
sem_init(&sem_b, 0, 0);
sem_init(&sem_c, 0, 0);
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_create(&tid3, NULL, thread3, NULL);
pthread_join(tid1, NULL);
pthread_join

最低0.47元/天 解锁文章
1039

被折叠的 条评论
为什么被折叠?



