Design and Implementation of user space locks

Source: Internet
Author: User

Design and Implementation of user space locks
Yesterday we completed the implementation of the lock-free queue, and then thought about the lock principle. The core of the lock is to ensure that a variable needs to be updated atomically. For example, we use a bool x, indicates the lock. When we obtain the lock, we need to ensure that the lock can be read and updated only by one of multiple threads at the same time, logically, there is the following code:
Bool GetLock (bool & x ){
If (x ){
X = false;
Return true;
Return false;

However, reading and updating are not atomic operations. If both threads find that x is true at the same time, but no thread has updated x, two threads update x at the same time, that is, two threads update the lock status at the same time. This is because the reading and updating of x is not atomic. There is a CMPXCHG command in X86, and an InterLockCompareExchange API in Windows can run such atomic operations. That is to say, we can use these two atomic operations to implement the lock. The following code is used:

# Include
# Pragma comment (lib, "kernel32.lib") class USLock {// user space lockpublic: USLock () {LockData = new int (1 );}~ USLock () {delete LockData;} public: bool Get () {return (InterlockedCompareExchange (LONG *) LockData, 0, 1);} void Release () {* LockData = 1;} private: int * LockData;}; dword winapi cs (void * lpAram); # include
# Include
Using namespace std; deque
Q; USLock lock; int main () {HANDLE hThread [2]; for (int I = 0; I <2; ++ I) {hThread [I] = :: createThread (NULL, 0, CS, (void *) I, 0, NULL);}: WaitForMultipleObjects (2, hThread, true, INFINITE );} dword winapi cs (void * lpAram) {while (true) {if (! LpAram) {// production for (int I = 0; I <1000;) {if (lock. get () {q. push_front (I ++); lock. release () ;}:: Sleep (rand () % 1000);} return 0 ;}else {if (lock. get () {// consume if (! Q. empty () {cout <
The following is a piece of test code. If you are interested, you can check it.

Related Article

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: 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.