Question Implementing shared/exclusive locking (readers/writer) lock

I am using semaphore to implement Single Writer Multiple Readers (Reader / writer) lock.
The problem is that semaphores are critical resources in linux systems, and there is a limit on the number of semaphores

that you can create.
So, after creating some semaphores, sometimes my application exceeds this limit, and I get an error like 'No space left on


I have to reboot the system sometimes, to release the semaphores, if my application is terminated abruptly!

How should I implement readers/writers lock using something else than semaphore, which is scalable?

I don't need reader/writer locks across multiple processes. I just need to use them in multiple threads of my application,

which is a single process.
But I want a very scalable solution, say I want 500 locks through out my application daemon's lifetime.

I read in the link below that postgres also suffered the same problem, but there it is recommended that the limit should be

increased, which I don't want users to do.