I have an operation in PHP that checks if an object with an ID is in a DB and creates an entry for it if it isn't. A request for the same ID can happen concurrently, so I want to prevent the ID from being inserted twice.
I am currently using the following code to lock/unlock access to the DB for that particular key:
$semaphore = sem_get($keyForID, 1);
sem_acquire($semaphore);
// 1.) check for id
// 2.) insert object if it doesn't exist
sem_release($semaphore);
My question is: Since I'm creating an individual semaphore for every object-ID, do I explicitly have to remove those semaphores with sem_remove
or does PHP do that automatically after sem_release
?
If I do have to remove the semaphore explicitly, is there an easy and reliable way of checking if that semaphore is currently acquired by another thread so that latter doesn't crash on sem_release
? Using shared memory comes to mind, but that would require yet another semaphore to read/write the number of threads.