[plt-scheme] Thread synchronization for dummies

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Thu May 28 14:55:51 EDT 2009

On Thu, May 28, 2009 at 2:49 PM, Erich Rast <erich at snafu.de> wrote:
> Hi,
>
> Suppose I have a function FOO with side effects (filesystem access) and
> would like to ensure that if this function is called by two different
> threads A and B at the same time, either A executes FOO entirely and then B
> executes FOO entirely or vice versa. In other words, I'm looking for
> something like "synchronized" does in Java.
>
> How to?
>
> Best regards,
>
> Erich

You'll find reference material for PLT Scheme's concurrency operations here:

http://docs.plt-scheme.org/reference/concurrency.

What I would suggest for your operation is spawning a dedicated thread
to handle FOO requests.  Whenever FOO gets called, it should send a
message to that thread, then block to wait for a response.  That
thread should wait for requests, and for each one should handle the
request and send a response before waiting for the next request.  This
will guarantee each request will be handled in order and will not
overlap.

Look at the documentation for creating threads (for your request
thread) and on channels (for sending, waiting for, and receiving
requests/responses).

I hope this helps!

-- 
Carl Eastlund


Posted on the users mailing list.