Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:37:36+00:00 2026-06-15T01:37:36+00:00

I have an operation in PHP that checks if an object with an ID

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:37:37+00:00Added an answer on June 15, 2026 at 1:37 am

    sem_remove explicitly destroy a given semaphore, which will make it unavailable for further computation for any other running process or thread, until it is recreated using sem_get. For instance, if 3 processes are trying to access the same semaphore (with only 1 concurrent access), and the first one destroy it before the two others manage to acquire it, the two latter processes may end up running concurrently, instead of sequentially (as expected).

    You really don’t want to call sem_remove, unless you know that all other running processes won’t use it anymore.

    Anyway, in your situation, I would recommend relying on the DB itself for concurrent accessing. If you need to have only one entry created in your DB, use transactions. If you are using a DB which isn’t supporting transactions such as the MyISAM engine on MySQL, either switch to an engine supporting transactions, or in the specific case of MyISAM, try using atomic operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object-oriented parent-child tree in PHP that I want to clone. The
I have a php that makes some maintenance operations in my web and I
I have an operation that will be performed MANY times and so I need
I want to have an operation(Task) to be Scheduled after some time OR Based
I have a division operation inside a cycle that repeats many times. It so
I have a homework that include a new operation (a [n] b), given: a
I'm trying to use mongodb with PHP. For that, I have created a MongoHQ
I am new to taking an object oriented approach with php...actually I have a
I have two tables that i am inserting records into. I am using PHP
Please review this Stackoverflow post. I have the same PHP problem as bob_cobb. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.