Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One use of condition variables that's a bit more complicated than just a message queue, is to "share a lock", where different threads are waiting for subtly different conditions of the same basic nature. For instance, you have a (very shonky, simplified) web cache. Each entry in the cache has three possible states: not present, IN_PROGRESS, COMPLETE.</p> <pre><code>getURL: lock the cache three cases for the key: not present: add it (IN_PROGRESS) release the lock fetch the URL take the lock update to COMPLETE and store the data broadcast the condition variable goto COMPLETE COMPLETE: release the lock and return the data IN_PROGRESS: while (still IN_PROGRESS): wait on the condition variable goto COMPLETE </code></pre> <p>I have in practice used the pattern to implement a variant of the POSIX function <code>pthread_once</code> without any help from the scheduler. The reason I couldn't use a semaphore or lock per <code>once_control</code>, and just do the initialization under the lock, is that the function wasn't allowed to fail, and the <code>once_control</code> had only trivial initialization. For that matter, <code>pthread_once</code> itself has no defined error codes, so implementing it to possibly fail doesn't leave your caller with any good options...</p> <p>Of course with this pattern you have to be careful about scaling. Each time any initialization is completed, every waiting thread wakes up to grab the lock. So when you design the system you think very carefully about sharding, and then decide you can't be bothered doing anything to actually implement it until you see proven performance problems.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload