开发者

Questions on simple threading jargon [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

When a manual-reset event is signaled, all threads waiting on the event become sche开发者_运维百科dulable. When an auto-reset event is signaled, only one of the threads waiting on the event becomes schedulable.

I have some really noob questions here to ask, as I'm new to threading.

  1. What does it mean to "set" and "reset" an event?
  2. What is the difference between a manual-reset and auto-reset event?
  3. What does it mean when an event is "signaled"? Does it mean it is activated?
  4. What does it mean when a thread becomes "schedulable" ?
  5. What is "waiting on the event" ?


  1. Think about a light switch. When an event is "signaled," the light switch is turned ON. When the event is "reset," the light switch is turned OFF.

  2. An auto-reset event means that after the event is signaled (turned ON) and a thread is released, the event is automatically reset (turned OFF). A manual reset event remains signaled (turned ON) until it is manually reset (turned OFF).

  3. See #1 above.

  4. When a thread is waiting on an event, the thread is blocked, meaning that it cannot be scheduled for work by the operating system. When the event the thread is waiting on is set (signaled, turned ON), the thread is released, i.e., becomes schedulable. This simply means that the operating system can now schedule the thread for work.

  5. Waiting on an event simply means to call one of the Wait() functions on an event. The thread will "wait" until the event is set/signaled/turned ON before continuing any work it has.


Basically, a "WaitHandle" (which includes both Manual Reset Events and Auto Reset Events) are types that allow a thread to wait until something happens - in this case, until the WaitHandle is "Set".

A thread (Thread A) can "wait" on a WaitHandle, blocking until a separate thread (Thread B) "sets" (=="signals") the WaitHandle. This will allow thread A to continue on at that point.

The main difference occurs if you have multiple threads waiting on the WaitHandle. In this case, with a Manual reset event, all of the threads will be allowed to continue on (ie: they're now schedulable, which means the OS will set them up and run them again at some point, usually fairly quickly). With an auto reset event, one thread is allowed to continue, and the WaitHandle is "reset", which prevents the other threads from continuing (until the WaitHandle is signaled again, when the next thread is "released").


To address each of your questions:

1) When an event is reset, it means that another thread waiting on the event can now be signaled. In other words, a thread that is now sleeping (blocking) can be woken up to do work.

2) According to Event Objects (MSDN),

  • Manual-reset event: An event object whose state remains signaled until it is explicitly reset to nonsignaled by the ResetEvent function. While it is signaled, any number of waiting threads, or threads that subsequently specify the same event object in one of the wait functions, can be released.

  • Auto-reset event: An event object whose state remains signaled until a single waiting thread is released, at which time the system automatically sets the state to nonsignaled. If no threads are waiting, the event object's state remains signaled. If more than one thread is waiting, a waiting thread is selected. Do not assume a first-in, first-out (FIFO) order. External events such as kernel-mode APCs can change the wait order.

So basically with a manual reset event you can explicitly reset the event, triggering any number of threads to execute. With an auto reset event, the OS guarantees that only a single thread will ever execute when the event is signaled.

3) Right, when an event is signaled, a thread is using the event to perform work. Any other threads attempting to access (signal) the event will be blocked.

4) In this context, schedulable means that a thread can be executed. Depending upon the event settings, the OS will pick one or more threads waiting on the event, and will execute them.

5) Waiting on the event means that a thread is blocking on the event object. While blocked, a thread is not executing any CPU cycles and is basically "put to sleep" by the OS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜