Windows core programming Note (6)----thread Synchronization in user mode

Source: Internet
Author: User
Tags throw exception

1. Atomic LockUsing the InterlockedExchangeAdd function to achieve atomic growth, Interlockedexchange\interlockedexchangepointer is used to exchange two variable
The value of the quantity, interlockedcompareexchange the comparison value, the equivalent is exchanged (corresponding interlockedcompareexchangepointer). corresponding to the
There are also 64-bit functions.
Interlockedincrement\interlockeddecrement is a relatively old function that can only increase or decrease the flexibility of 1,interlockedexchangeadd more
Big.
2. Interlocked one-way list operation function (linked list supporting atomic operation)InitializeslistheadCreate an empty stack
InterlockedpushentryslistInto the stack
InterlockedpopentryslistOut of the stack
InterlockedflushslistEmpty stack
QuerydepthslistGets the number of stack elements
3. Cache LineWhen the CPU takes the instruction out of memory, the cache row size byte (32, 64, 128 varies depending on the CPU model) is removed at a time, and the CPU does not have access to the memory bus,
Read instructions from the cache are much faster than in-memory reads.
The memory data is inconsistent because multiple CPUs take out the same piece of memory data to their respective cache rows. CPU is designed when a CPU modifies its told slow
When a row is saved, the other CPUs receive notifications and invalidate their cache lines.
This means that we should organize the data of the application according to the size of the cache rows and align the data with the boundaries of the cache rows. The purpose of doing this
To ensure that different CPUs have access to different memory addresses, and that they are not in the same cache line.
4. Use the volatile keywordVolatile tells the compiler not to optimize the variable in any way, but to always read the value of the variable from the location in the memory where the variable resides.
Compiler optimizations, the compiler reads the data into the CPU register and takes the value directly from the register the next time it is needed, so that even if the value of the variable has changed
Cannot know. After the use of volatile, the CPU each time the variable is in memory read, the variable can be retrieved in time.
5, the Use of critical section (critical section) criticalsectionEntercriticalsection\leavecriticalsection, need to initialize initializecriticalsection before use, need to release after use
DeleteCriticalSection.
Key segment Advantages: Easy to use, fast execution; disadvantage: threads cannot be synchronized between multiple processes.
While there are two threads accessing the resource, EnterCriticalSection will make one get the resource and the other switch to the wait state. If the wait time is too long,
End-of-throw exception, time-out saved in registry: hkey_local_machine\system\currentcontrolset\control\
Session Manager\criticalsectiontimeout. The default is about 30 days.
You can use TryEnterCriticalSection to replace entercriticalsection,tryentercriticalsection immediately returns whether the resource is
Be occupied.
6. Key section and Rotary lockWhen a thread attempts to enter a critical segment, and the critical segment is being occupied by another thread, the function immediately switches the calling thread to the waiting state. This
This means that the thread must switch from user mode to kernel mode (about 1000 CPU cycles), which is a very expensive one. To improve the performance of critical segments,
Microsoft has incorporated rotation locks into critical sections. When calling EnterCriticalSection, a rotation lock is called to loop continuously, trying to get the resource's
Access rights. The kernel state is switched on only if the attempt fails. In order to use a key segment with a rotation lock, you need to use the API
InitializeCriticalSectionAndSpinCount to initialize the critical segment, the second parameter formulates the number of cycles, if it is on a single CPU machine this
Parameters will be ignored.
The Setcriticalsectionspincount is used to change the rotation lock cycle times. The number of loops used to protect the critical segment of the process heap is approximately 4000, which can be used as a reference for us.
EnterCriticalSection can cause an exception when not in sufficient memory, but there is no return value, and the return value is False when the initializecriticalsectionandspincount is insufficient, making it easy to test whether the creation was successful.
7. Slim read/write lockThis function is only present in Vista above, ignored.
8, the performance of the above several thread synchronization mode:If you want your application to get the best performance, first try not to share the data, then use volatile read, volatile write, interlocked
Api,srwlock and key segments. The kernel object (which needs to be between user mode and kernel mode each time) is currently only available if none of these are sufficient
Switching, CPU overhead is very high).
9, the use of skills(1) A lock is used when manipulating a set of objects atomically, with the disadvantage of reducing scalability: At any moment the system allows only one thread to run.
(2) When multiple resources are accessed at the same time, each resource has its own lock. When a resource is fetched, all threads must be executed in the same order.

(3) Do not take a long time to occupy the lock, other threads may enter the waiting state, affect program performance


Here is the test code:

<span style= "White-space:pre" ></span>//gets the number of CPU cores and the size of cache lines per CPU
<span style= "White-space:pre" ></span>psystem_logical_processor_information buffer = NULL;DWORD DwLen = 0; while (true) {if (getlogicalprocessorinformation (buffer, &dwlen)) Break;if (GetLastError ()! = Error_insufficient_ BUFFER) {cout<< "Error code =" <<getlasterror () <<endl;return 1;} if (buffer) free (buffer), buffer = (psystem_logical_processor_information) malloc (Dwlen); if (NULL = = buffer) {cout<& lt; " Error to malloc "<<endl;return 2;}} int nproccorecount= 0;int nbyteoffset= 0; Psystem_logical_processor_information ptr = Buffer;while (nbyteoffset<dwlen) {switch (ptr->relationship) {case Relationprocessorcore:nproccorecount++;break;case relationcache:cout<< "CPU" <<nProcCoreCount<< "' s cache size is" <<ptr->Cache.LineSize<< "byte" <<endl;break;default:break;} Nbyteoffset + = sizeof (system_logical_processor_information);p tr++;} cout<< "Processorcore count is" <<nproccorecount<<endl;free (buffer);
The/*////////////////////////////////////////////spin lock CPU is constantly comparing two values and consumes CPU time. This assumes that all threads are running at the same priority, and for threads that require a rotation lock, you may need to use the Setprocesspriorityboost or SetThreadPriorityBoost function to suppress thread-priority elevation. Machines on a single processor should not use a rotary lock, otherwise it is easy to cause a deadlock. */dwordwinapi Thread1 (LPVoid lpparam) {while (InterlockedExchange (&g_buse, TRUE) = = True) {//Returns TRUE indicates being used, Continue waiting for sleep (0);} Returns false, indicating that it is not currently being used, and we have set it to be used//do somethingg_nindex++; Sleep (300);//after use, set the status to not use InterlockedExchange (&g_buse, FALSE); return 0;}




Windows core programming Note (6)----thread Synchronization in user mode

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