What is the principle of this concurrent lock?

Source: Internet
Author: User
Tags usleep
The locks we have seen are similar to defining a variable true first. After the related operation is completed, the related value is changed to false. {code ...} the locks we have seen are similar to defining a variable = true first. After the related operation is completed, the related value is changed to false.

$ Lock_num = 0; while (! Redis_helper: lock ("lottery") {if ($ lock_num> = 300) {return "dear, many people are waiting in queue to grab red packets. Please go to the page again ";} // 0.1 s usleep (100000); $ lock_num ++;} The following is a function related to concurrent locking. You can understand that this lock changes the real value every 6 s, but I don't understand what the concurrency situation is based on the above $ lock_num? 6 s does not change the real value, 6 s/0.1 s = 60 times, how can it reach 300, the page is stuck, and others just set the lock to false again? /*** Concurrent lock * @ param string $ name lock name * @ param int $ expires timeout and expiration time, in seconds * @ return bool true: the lock is obtained successfully, false: failed to get lock */public static function lock ($ name, $ expires = 5) {self: init_redis (); $ key = $ name. '_ lock'; $ now = time (); $ val = $ now + $ expires + 1; if (self: $ redis-> setnx ($ key, $ val) {return true;} else {$ old_val = self ::$ redis-> get ($ key ); // if ($ now >$ old_val & $ now> self ::$ redis-> getSet ($ key, $ val) {return true ;}} return false ;}

Reply content:

The locks we have seen are similar to defining a variable = true first. After the related operation is completed, the related value is changed to false.

$ Lock_num = 0; while (! Redis_helper: lock ("lottery") {if ($ lock_num> = 300) {return "dear, many people are waiting in queue to grab red packets. Please go to the page again ";} // 0.1 s usleep (100000); $ lock_num ++;} The following is a function related to concurrent locking. You can understand that this lock changes the real value every 6 s, but I don't understand what the concurrency situation is based on the above $ lock_num? 6 s does not change the real value, 6 s/0.1 s = 60 times, how can it reach 300, the page is stuck, and others just set the lock to false again? /*** Concurrent lock * @ param string $ name lock name * @ param int $ expires timeout and expiration time, in seconds * @ return bool true: the lock is obtained successfully, false: failed to get lock */public static function lock ($ name, $ expires = 5) {self: init_redis (); $ key = $ name. '_ lock'; $ now = time (); $ val = $ now + $ expires + 1; if (self: $ redis-> setnx ($ key, $ val) {return true;} else {$ old_val = self ::$ redis-> get ($ key ); // if ($ now >$ old_val & $ now> self ::$ redis-> getSet ($ key, $ val) {return true ;}} return false ;}

In my understanding, I tried to obtain the lock for 300 times. If the lock fails, I will tell the user that the lock failed.
If the lock is obtained successfully, there will be 6 seconds of exclusive time. This time is used to process subsequent business logic for the program, such as generating orders. 6 seconds later, the next user comes in and continues to get the lock.

Lock-> do something.-> release
Lock-> do something.-> release
Lock-> do something.-> release
Lock-> do something.-> release

$ Lock_num has nothing to do with the lock. $ lock_num is only used to record the number of retries. For example, 1000 users are currently scrambling for the lock, and if you retry 300 times (30 seconds) the lock has not been obtained yet, so we will try again later.
A loop retries every 0.1 seconds, but because a lock takes 6 seconds, in fact, there are only five chances to compete for the lock in 300 cycles (30 seconds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.