Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang Reader-Writer program

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?

like image 621
geeknerd Avatar asked Jan 16 '23 21:01

geeknerd


1 Answers

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:

  1. spawn a process in erlang that stores your data and listens for messages.
  2. spawn other processes that send messages to your storage process asking for data back 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.

like image 186
Jeremy Wall Avatar answered Jan 25 '23 14:01

Jeremy Wall