开发者

what is the difference between semaphore and critical region?

the only thing i understood is

  • semaphore is a pri开发者_如何学Pythonmitive way
  • critical region has a GUARD variable (semaphore also does but the name is not GUARD!)

??

so whats the difference?


Generally, a critical region is a place where, if two separate threads of execution were to be present, a race condition or some other undesirable effect would occur. Semaphores are one way of preventing two threads from being in the critical region at the same point in time.


The GUARD would only allow 1 thread to enter the critical region at a time, whereas the semaphore can allow n threads (you specify n) to concurrently enter the critical region.


When a process executes code that manipulates shared data (or resource), we say that the process is in it’s critical section (CS) (for that shared data) and semaphore is Nonnegative integer variable used as a flag and Signals if and when resource is free


There are two interpretations of "critical region":

  1. A region of code that will produce undefined results if executed simultaneously by two threads.

  2. A region of code that is isolated from all executors except for the current thread. An example of this would be an interrupt handler. These regions are more commonly called "critical sections". On Intel CPUs you can begin/end a critical section with the CLI/STI instructions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜