I wish to implement a naive reader-writers lock using the basic primitives and thier respective methods that are in the pthread library (i.e. - <pthread.h>).
I went over couple of questions and thier answers but neither satisfied what I was looking for:
This, and this, also this, and finally this as well
It is mainly for educational purposes, I guess that there are built-in mechanisms for this kind of need nowadays.
The below implementation is the very basic solution I came up with:
Notes about this implementation: It gives priority to the readers before writers.
The way to overcome it can be as follows:
In the
void rw_lock_release_read(rw_lock *lock)method, the "signaling" when there are no readers left, should be change so that it will signal to a particular writer usiung the writers' condition variable, like so:While in addition, the
rw_lock_release_write(rw_lock *lock)method should be changed as follows:If there are any comments on the solutions I would be glad to know about them!