To cite from the v17 docs on the NOTIFY SEMAPHORE statement:

If the counter is 0, and a connection is blocked on a WAITFOR SEMAPHORE statement on this semaphore, the NOTIFY SEMAPHORE statement notifies the connection.

In case several connections are blocked by that semaphore (say, a consumer-producer model with several consumers waiting on the producer to bring out another piece of work), is there a particular order on which connection will be notified - the one that has been blocked as first?

asked 23 Nov '15, 03:21

Volker%20Barth's gravatar image

Volker Barth
30.3k301453660
accept rate: 32%

edited 24 Nov '15, 08:10


When there is more than one connection blocked on a semaphore the connection that was blocked first is the one that is unblocked.

permanent link

answered 23 Nov '15, 09:03

Mark%20Culp's gravatar image

Mark Culp
23.0k9129270
accept rate: 40%

Sorry, I should have asked it in one go:

Is the same true (i.e. a FIFO order) for several connections waiting on the same mutex?

(Aside: I'm primarily asking as these rules are different for Win32 sync objects: AFAIK there is no guaranteed order.)

(23 Nov '15, 14:20) Volker Barth
Replies hidden

No, the rules are different if more than one connection is being released: if there are more than one connection then all waiters are woken up and it is a race to see which one(s) get the semaphore. I.e. no strict ordering.

(23 Nov '15, 14:39) Mark Culp

You say they are all woken up and then all but one will be suspended/locked, again? - Is this noticeable from "outside"? (I would think LOCK MUTEX IN EXCLUSIVE MODE is an atomic operation?)

(23 Nov '15, 14:44) Volker Barth
2

The "all woken up" behavior is for a NOTIFY SEMAPHORE ... INCREMENT BY num with num > 1.

In case of a mutex being RELEASEd, the observed behavior will depend on whether the next connection in line requires a shared or an exclusive lock. If the next connection (first in line) requires a shared lock, then all other connections waiting for a shared lock will also be woken up. If exclusive lock is required, the first-in-queue connection will acquire the lock.

(23 Nov '15, 16:35) Elmi Eflov

OK, so for mutexes, there is a strict ordering, too. And particular thanks for the details w.r.t. to share mode!

(24 Nov '15, 07:43) Volker Barth
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×4
×4

question asked: 23 Nov '15, 03:21

question was seen: 655 times

last updated: 24 Nov '15, 08:10