What is a non-blocking semaphore?

It seems like a contradiction, because the semaphore must be blocked for work.

An Internet search does not display anything useful in determining what this means.

+6
source share
3 answers

If the semaphore has a value of 0, the down operation on it is blocked until someone releases the resource and increases the semaphore.

A non-blocking semaphore is not blocked during the down operation if the resource is unavailable but gives an error. This can be useful if the program needs this resource immediately or without pausing execution, and if the resource is unavailable, the program logic can rather do something else.

+7
source

A non-blocking semaphore usually refers to a semaphore that allows you to try to acquire without blocking. If the semaphore cannot be “locked”, it will fail, but not locked. If it can be locked, it will lock it and return true (it is locked).

+5
source

The associated wait operation WaitForSingleObject() , for example, allows you to specify a dwMilliseconds timeout value. If this parameter is set to 0 , the result of the call will always be returned immediately. Thus, the state of the semaphore can be tested without blocking.

+1
source

Source: https://habr.com/ru/post/922096/


All Articles