semaphores do not access like volatile

Lex Spoon lex at
Mon Feb 10 14:36:06 UTC 2003

On Mon, Feb 10, 2003 at 11:38:16AM +0100, Niko Schwarz wrote:
> Hello,
> I'm doing this: I send my authentication to a server, and then wait on a 
> semaphore. A reading process checks for response. if it has response, it 
> changes an instance variable (results), and sends signal to the semaphore.

Simply change the state before signalling the semaphore.  Don't try to
avoid the race condition; make it unharmful.  If the data is already there,
then it doesn't matter how fast the other thread wakes up.  It can wake
up the very moment the semaphore is signalled, and be fine.  (You also
need to worry about the other way around, where the other thread takes
a LONG time to wake up.  Will it still do the right thing, if, say, anothe
request comes in before the first one is processed?)

Let me suggest that you try a SharedQueue.  Have your server stick the
request into a queue.  You can put the response in another queue.  If
you want, you can even attach a response queue to the request.

In general, programs that use SharedQueues tend to be easier to write
and debug than programs using semaphore's plus shared state.  So maybe
learn them and love them!


More information about the Squeak-dev mailing list