信号量示例:想知道此代码中的错误 [英] Semaphore Example: Want to know what's wrong in this code

查看:105
本文介绍了信号量示例:想知道此代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include <semaphore.h>

sem_t semA, semB;


main()
{

  sem_init(&semA, 0, 0);
  sem_init(&semB, 0, 0);

int a=0,b=0;
	while(1)
	{
		a++;
		printf("Run aaa  %d\n",a);
		sem_post(&semB);
   		sem_wait(&semA);
		printf("go to bb  %d\n",a);
		sleep(1);

	}
	while(1)
	{
		b++;
		printf("post to bbb %d\n",b);
		sem_post(&semA);
		sem_wait(&semB);
		printf("go to aaa   %d\n",b);
		sleep(1);

	}
}

推荐答案

从一开始,就没有任何意义。你有没有了解信号量的用途?用于以某种方式同步线程。你的线程在哪里?这里没有什么可讨论的。



-SA
From the very beginning, it makes no sense at all. Did you learn what semaphores are for? The are used to synchronize threads in a certain way. And where are your threads? There is nothing to discuss here.

—SA


这篇关于信号量示例:想知道此代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆