在多线程中的信号和等待概念 [英] Signal and wait concept in multithreading

查看:85
本文介绍了在多线程中的信号和等待概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解这个程序中的并发概念。我试图理解它,但我无法理解主要逻辑。我不确定信号等待实际上在这里做什么。



核心逻辑在这里

I am trying to understand concurrency concepts in this program. I am trying to understand it but I am not able to understand the main logic. I am not sure what signal and wait are actually doing here.

the core logic is here

export operation Hi
			if ! flip then
				stdout.putstring["Waiting Hi\n"]
				wait c
			end if
			stdout.putstring["Hi\n"]
			flip <- false
			signal c
		end hi
		export operation Ho
			if flip then
				stdout.putstring["Waiting Ho\n"]
				wait c
			end if
			stdout.putstring["Ho\n"]
			flip <- true
			signal c
		end ho





我尝试过:



输出为



What I have tried:

The output is

Hi
Waiting Hi
Ho
Hi
Ho
Hi
Ho
Hi
Ho
Hi
Ho
Hi
Ho
Waiting Ho
Hi
Ho
Hi
Ho
Hi
Ho
Hi
Ho





任何人都可以解释这个输出是怎么回事生成信号和等待如何执行输出



Can anyone explain how this output is generated how signal and wait are executing the output

推荐答案

不知道那种语言是什么,但它不是C.在我看来,有两个独立的活动, hi 。每个人都测试一个标志,然后等待或继续处理。在其他活动发送重新启动信号的信号之前,等待活动不执行任何操作。然后他们继续在相反的轨道上。输出显示执行顺序,
No idea what that language is, but it is not C. It looks to me like there are two independent activities, hi and ho. Each one tests a flag and either waits, or continues processing. The waiting activity does nothing until the other activity sends a signal which restarts it. They then continue on opposite tracks. The output shows the order of execution,


这篇关于在多线程中的信号和等待概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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