线程中的临界区是什么? [英] What is critical section in threading?

查看:52
本文介绍了线程中的临界区是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人用例子简单地告诉我临界区的意思是什么?用简单的语言

Please can some one briefly tell me with example what does the means of critical section? in simple language

推荐答案

critical section 是需要在没有外部干扰的情况下执行的一段代码 - 即没有另一个线程可能影响/受该部分内中间"状态的影响.

A critical section is a section of code that needs to be executed without outside interference - i.e. without another thread potentially affecting/being affected by "intermediate" states within the section.

例如,预订系统在预订某些东西时可能有一个临界区,因为它需要检查该项目是否可用,然后将其标记为不再可用,而不需要其他一些尝试预订房间的方法来改变它状态在中间.

For instance, a reservation system might have a critical section when reserving something in that it needs to both check to see if the item is available and then mark it as no longer available, without some other attempt at reserving the room changing that status in the middle.

因此,一段代码的临界区是一次只允许一个线程执行的地方,以防止出现竞争条件之类的情况.

Thus, the critical section of a piece of code is a place where only one thread of execution is allowed to be at a time, to prevent things like race conditions.

这篇关于线程中的临界区是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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