事件对象和条件变量之间的差异 [英] Difference between event object and condition variable

查看:884
本文介绍了事件对象和条件变量之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件对象和条件变量之间有什么区别?
我在WIN32 API的上下文中询问。

What is the difference between event objects and condition variables? I am asking in context of WIN32 API.

推荐答案

事件对象是内核级对象。它们可以跨进程边界共享,并在所有Windows操作系统版本上受支持。如果需要,它们可以用作它们自己的独立锁来共享资源。因为它们是内核对象,所以操作系统对每次可以分配的可用事件的数量有限制。

Event objects are kernel-level objects. They can be shared across process boundaries, and are supported on all Windows OS versions. They can be used as their own standalone locks to shared resources, if desired. Since they are kernel objects, the OS has limitations on the number of available events that can be allocated at a time.

条件变量是用户级对象。它们不能跨进程边界共享,并且仅在Vista / 2008和更高版本上受支持。它们不作为自己的锁,但需要一个单独的锁来与它们相关联,例如关键部分。由于它们是用户对象,可用变量的数量受可用内存的限制。当条件变量进入休眠状态时,它会自动释放指定的锁对象,以便另一个线程可以获取它。当条件变量被唤醒时,它会自动重新获取指定的锁定对象。

Condition Variables are user-level objects. They cannot be shared across process boundaries, and are only supported on Vista/2008 and later. They do not act as their own locks, but require a separate lock to be associated with them, such as a critical section. Since they are user- objects, the number of available variables is limited by available memory. When a Conditional Variable is put to sleep, it automatically releases the specified lock object so another thread can acquire it. When the Conditional Variable wakes up, it automatically re-acquires the specified lock object again.

在功能方面,将条件变量视为两个对象的逻辑组合一起工作 - 键控事件和锁定对象。当条件变量进入休眠状态时,它将重置事件,释放锁定,等待事件发出信号,然后重新获取锁定。例如,如果你使用临界区作为锁定对象, SleepConditionalVariableCS()类似于对 ResetEvent() LeaveCriticalSection() WaitForSingleObject() EnterCriticalSection / code>。如果您使用SRWL作为锁, SleepConditionVariableSRW()类似于对 ResetEvent()的调用序列 ReleaseSRWLock ...() WaitForSingleObject() AcquireSRWLock ... )

In terms of functionality, think of a Conditional Variable as a logical combination of two objects working together - a keyed event and a lock object. When the Condition Variable is put to sleep, it resets the event, releases the lock, waits for the event to be signaled, and then re-acquires the lock. For instance, if you use a critical section as the lock object, SleepConditionalVariableCS() is similar to a sequence of calls to ResetEvent(), LeaveCriticalSection(), WaitForSingleObject(), and EnterCriticalSection(). Whereas if you use a SRWL as the lock, SleepConditionVariableSRW() is similar to a sequence of calls to ResetEvent(), ReleaseSRWLock...(), WaitForSingleObject(), and AcquireSRWLock...().

这篇关于事件对象和条件变量之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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