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 8573515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:16:07+00:00 2026-06-11T19:16:07+00:00

How to make functions in PHP synchronized so that same function won’t be executed

  • 0

How to make functions in PHP synchronized so that same function won’t be executed concurrently ? 2nd user must wait till 1st user is done with the function. Then 2nd user can execute the function.

Thanks

  • 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-11T19:16:08+00:00Added an answer on June 11, 2026 at 7:16 pm

    This basically comes down to setting a flag somewhere that the function is locked and cannot be executed until the first caller returns from that function.
    This can be done in a number of ways:

    • use a lock file (first function locks a file name “f.lok”, second function checks if the lock file exists and executes or doesn’t based on that evaluation)
    • set a flag in the database (not recomended)
    • use semaphores as @JvdBerg suggested (the fastest)

    When coding concurrent application always beware of race conditions and deadlocks!

    UPDATE
    using semaphores (not tested):

    <?php
    
    define('SEM_KEY', 1000);
    
    function noconcurrency() {
        $semRes = sem_get(SEM_KEY, 1, 0666, 0); // get the resource for the semaphore
    
        if(sem_acquire($semRes)) { // try to acquire the semaphore. this function will block until the sem will be available
            // do the work 
            sem_release($semRes); // release the semaphore so other process can use it
        }
    }
    

    PHP needs to be compiled with sysvsem support in order to use sem_* functions

    Here’s a more in depth tutorial for using semaphores in PHP:

    http://www.re-cycledair.com/php-dark-arts-semaphores

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

Sidebar

Related Questions

Is there a way to make functions in PHP synchronized to make sure that
I've a project, with various subdirs that make use of a functions.php file located
I have a PHP function that I want to make available publically on the
How to create a simple Wordpress function in functions.php I want to make a
I'm trying to make a PHP regex to extract functions from php source code.
How can I make the deprecated functions to work in PHP's latest version? I
here is myproblem, i want to make a php function to insert some random
Is there any function in PHP to make the first character of the word
I can't make the following work. I know it's because of the PHP function
When using the function imagepng() in PHP, how can I make sure the images

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.