Locking data for writes, but not reads
Thu Feb 9 08:09:17 CET 2006
I've been trying to learn a bit about Erlang, and I'm not clear on how
to implement this common idea using the Erlang mailbox paradigm for
communication between processes.
For example, let's say I have some process that manages a complex data
structure. When other processes ask to read some information from the
structure, it should read this information and deliver this
information to the requesting process. In the meantime, it should
continue processing messages (because it should be able to handle read
requests in parallel). However, if it receives a message to modify or
write to the data structure, it can't allow any more reads to take
place until the write is completed.
How can this be elegantly done in Erlang? There seems to be a couple
of problems. First, it's not clear how to process the read commands
in parallel. Erlang can't spawn off another process to handle a read
request, because the new process won't have access to the data
structure stored in the state of the managing process. The other
tricky part seems to be that you can't start writing to the data
structure until all reads are complete. Is there an easy way to find
out that all pending reads are complete?
More information about the erlang-questions