Semaphores. Underwater rocks

Hi, guys. I want to start general discussion about semaphores.

Currently in my project i’m using them to create atomic access to some resources in various places.

I noticed, that sometimes they going crazy (or maybe not).

My question: After unexpected process crash - will semaphore be released for other processes? Docs of php says that semaphore will be released automatically after process end, but my experience telling me that its not always true.

Are you using semaphores in Yii2 projects? Can you share some code, please. It’s interesting.

i have few crons that works paralelly on same task and to prevent same data in them im doing next




            try {

                //atomic access to db

                $semKey = ftok(Yii::getAlias('@root'), 'd');

                $dbSemId = sem_get($semKey);

                sem_acquire($dbSemId);

                $targetQueue = UpdateTargetQueue::find()->limit($limit)->all();

                if (empty($targetQueue)) {

                    return;

                }

                //drop filtered result array from db

                UpdateTargetQueue::deleteAll([

                    'and',

                    ['in', 'id', array_keys($targetQueue)],

                ]);

            } catch (\Exception $ex) {

                Yii::error($ex->getMessage().' '.$ex->getTraceAsString();

            } finally {

                //release atomic access to db

                sem_release($dbSemId);

            }

            //actions with received data