in reply to Frustration with changing lock type

Am I missing the point or are you over complicating the problem. LOCK_EX will block until it gets the lock unless you LOCK_EX |LOCK_NB to make it non blocking. If you don't get a lock and wander off to do other code you will surely be wasting time unless you have a signal to indicate the lock is now available, exit your current code, LOCK_EX, doit() and return to what you were doing. Even if you do this is a race. Why not just block for lock or ask for a LOCK_EX|LOCK_NB and if you don't get it loop, sleeping 1 retying until you timeout or get lock?

Given that a lock write will take milliseconds you can calculate that the statistical probability of getting a lock on the first pass is N% so for each retry your probability of failure is (1-N%)**retries. In other words a wait for lock strategy will slow you app by a fraction of a percent overall on average but the alternatives are.....

cheers

tachyon

  • Comment on Re: Frustration with changing lock type

Replies are listed 'Best First'.
Re: Re: Frustration with changing lock type
by demerphq (Chancellor) on Apr 14, 2004 at 11:47 UTC

    Am I missing the point or are you over complicating the problem.

    Yes. :-) The exclusive lock means that we can do our task. The LOCK_EX is nonblocking so that if another task has it LOCK_SH it means the task is in process. The follow up LOCK_SH is meant to be blocking so that we dont try to read the tasks owner until it has finished writing to the lockfile.

    Why not just block for lock or ask for a LOCK_EX|LOCK_NB and if you don't get it loop, sleeping 1 retying until you timeout or get lock?

    Because I wanted to know the status and owner of the previous attempt basically. I know its not a good reason but I wanted to know, and things didnt work as I expected. :-)


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi