I’m really new to Erlang and currently I have problems writing a reader-writer program in Erlang. Basically, a shared memory location can be concurrently read by any number of tasks, but when a task must write to the shared memory location, it must have exclusive access.
My thought would be to spawn reader/write methods to different processes and in those methods just print out something like “Reader reading”/”Writer writing”.
However, the usage of semaphore/mutex really bugged me and I have no background in multithreading/concurrency. Can anyone please give some clues how to write such program?
Mutexes and Semaphores are just a way of defining synchronization points between two concurrent processes/threads. In erlang these are mostly replaced by sending and receiving messages between erlang processes. An idiomatic way to do this in erlang would be to:
or sending data to be written.
The message box for your data process will ensure that nobody else can write to the data at the same time as everyone else.