Thread synchronization--user mode thread synchronization--interlocked implementation

Source: Internet
Author: User

1 thread synchronization is divided into thread synchronization in user mode and thread synchronization of kernel objects. 2 3 Of course, thread synchronization in user mode is faster than kernel mode, but functions are also4 5  6 7 8 //1. Using Atomic access: Interlocked series functions, about interlocked series functions, what I need to know is that they execute extremely fast9 Ten //(1) The first is the InterlockedExchangeAdd brothers function, One //The first parameter, Addend, represents the address of the operand, A //The second parameter, value, represents the added value, and if you want to subtract, pass a negative number -  - LONG InterlockedExchangeAdd ( theLONGvolatile*Addend, - LONG Value); -  - LONG InterlockedExchangeAdd64 ( +LONG64volatile*Addend, - LONG64 Value); + //(2) There are 3 other interlocked functions A  at LONG InterlockedExchange ( -LONGvolatile*Target, - LONG Value); -  - Longlong InterlockedExchange64 ( -LONG64volatile*Target, in LONG64 Value); -  to PVOID InterlockedExchangePointer ( +PVOIDvolatile*Target, - PVOID Value); the  * //InterlockedExchange and InterlockedExchangePointer $ //The current value of the memory address pointed to by the first parameter is replaced atomically with the value specified by the second parameter .Panax Notoginseng  - //(3) The last other interlocked Exchange function the Plong InterlockedCompareExchange ( +LONGvolatile*Destination, A LONG Exchange, the LONG Comperand); +  - Plong Interlockedcompareexchangepointer ( $PVOIDvolatile*Destination, $ PVOID Exchange, - PVOID Comperand); - //pseudo code for function execution the { - if(Destination = =Comperand)Wuyi { theDestination =Exchange; - } Wu } - //The function compares the current value destination with the parameter comparand, if the two values are the same, About //then the function modifies *destination to exchange, otherwise the Destination value is unchanged. $  - //InterlockedExchange and its useful when implementing a rotary lock - //The following shows the rotation lock -BOOL G_fresourceinuse =FALSE; A voidFunc1 () + { the     //waiting to receive resources -      while(InterlockedExchange (&g_fresourceinuse,true) = =TRUE) $Sleep (0); the  the     //Receive Resources the  the     //we do not need to receive resources -InterlockedExchange (&g_fresourceinuse,false); in } the   the  About  the#include"windows.h" the#include"iostream" the using namespacestd; + LongG_x =0 ; -  the //defining thread Functions 1Bayi DWORD WINAPI threadfunone (PVOID pvparam); the  the //Defining thread Functions 2 - DWORD WINAPI threadfuntwo (PVOID pvparam); -  the intMain () the { the     //Create thread 1 theHANDLE Hthreadone = CreateThread (NULL,0, Threadfunone,0,0, NULL); - CloseHandle (hthreadone); the  the     //Create thread 2 theHANDLE hthreadtwo = CreateThread (NULL,0, Threadfuntwo,0,0, NULL);94 CloseHandle (hthreadtwo); the  the     //let the main thread hang first to make sure that the other threads perform the completion theSleep (10000); 98cout<<g_x<<Endl; About     return 0 ; - }101 102 DWORD WINAPI threadfunone (PVOID pvparam)103 {104InterlockedExchangeAdd (&g_x,1) ; the     return 0;106 }107 108 DWORD WINAPI threadfuntwo (PVOID pvparam)109 { theInterlockedExchangeAdd (&g_x,1) ;111     return 0; the }113   the  theLimit, so we should consider thread synchronization in user mode first when using thread synchronization

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.