Erlang Reader-Writer

I'm really new to Erlang and am currently having trouble writing a reader / writer program in Erlang. In principle, a shared memory location can be read by any number of tasks at the same time, but when a task should be written to a shared memory folder, it should have exclusive access. My idea would be to spawn read / write methods for different processes and in these methods just print something like "Read Reading" / "Writing".

However, using a semaphore / mutex really listened to me, and I have no background in multithreading / concurrency. Can anyone give some hints on how to write such a program?

+4
source share
1 answer

Mutexes and Semaphores are just a way of defining synchronization points between two parallel processes / threads. In erlang, they are most often replaced by sending and receiving messages between erlang processes. An idiomatic way to do this in erlang would be:

  • create a process in erlang that stores your data and listens for messages.
  • Launch other processes that send messages to your storage process, requesting data back or sending data for recording.

The message field for the data processing process ensures that no one can write data simultaneously with others.

+9
source

All Articles