David K. Storrs
A small library for working with semaphores.
procedure
( call/semasemathnk)→any
This is useful when you two functions that share a semaphore and one of them needs to call the other – it lets you tell the inner one not to use the semaphore since the outer one is already doing so.
;Note: The following code is intended for simplicity of example. In real use it would be;better to, e.g., not share mutable state between threads and, at a minimum, to expose;separate versions of the get-* functions that do not allow passing a semaphore in. Also,;handle the case where a user is not already in the hashes.;(call/semasemadept-id;(set-add (get-users dept-id) name))))) ; WRONG! This will deadlock! `sema` is already in use!;Pass #f as the semaphore so that we don't deadlock;;If the following functions were running in different threads, the call/sema code would ensure that the 'get-*' functions;did not interleave with a call to 'add-user!' and thereby see inconsistent state> (get-usersalice-dept-id)#<set: alice bob>
> (add-user!'evanalice-dept-id)> (get-usersalice-dept-id)#<set: evan alice bob>