Scene:
1. When multithreaded programming, it is sometimes necessary to count the number of times a variable or object is created, or whether it is necessary to continue execution depending on the value of a variable, which is one of the more efficient schemes for the Interlock function.
Description
1. Atomic access: (Interlock function family)
--atomic access, which means that threads can access resources to ensure that all other threads do not access the same resources at the same time.
--Invoking an interlock function typically requires only a few CPU cycles (typically less than 50) and does not require a conversion from user mode to kernel mode (typically this takes 1000 CPU cycles).
--The Interlock function is supported by the CPU hardware, and if it is a x86 processor, the Interlock function sends a hardware signal to the bus to prevent other processors from accessing the same memory address.
--no matter how much the compiler generates code, no matter how many processors are installed on the computer, you can ensure that a value is modified in atomic operation.
--You must ensure that the variable addresses passed to the function are correctly aligned, otherwise these functions will fail.
(1) The CPU runs the most efficient when it handles accurately aligned data. The memory address is modeled with the size of the data, and the result is 0 o'clock, and the data is aligned. For example, a value of type word
You should always start with an address that can be removed by 2, and the value of the DWORD type should always start with an address that can be removed by 4.
Test_interlock.cpp
Test_interlock.cpp: Defines the entry point of the console application. Atomic access: The interlocking function family. #include "stdafx.h" #include <Windows.h> #include <stdint.h> #include <assert.h># Include <iostream>using namespace std;static int32_t gcount = 0;static int32_t gexecutecount = 0;static const int KT Hreadnum = 10;static const int kloopnum = 1000;static uint32_t setted = 0; #define APR_ALIGN (Size, boundary) (((size) + ((b Oundary)-1)) & ~ ((boundary)-1)) DWORD WINAPI ThreadFunc (PVOID pvparam) {long* g_x = (long*) pvparam;for (int i = 0; i< Kloopnum; ++i) {//cout << "Sync_func currentthreadid:" << getcurrentthreadid () << Endl;interlockedexchangeadd ( g_x,1); InterlockedExchange (&setted,i%2);//1. Let other threads also have the opportunity to execute. Sleep (10);} return 0;} void Testinterlockedexchangeadd () {HANDLE hthreads[kthreadnum];//"Windows core programming" says the addresses are aligned, although x86 will do an extra-aligned instruction operation.// But you can align yourself or align yourself. For example, AMD CPU misalignment may cause problems. Long g_x = 0;for (int i = 0; i< kthreadnum; ++i) {DWORD Dwthreadid;hthreads[i] = creat Ethread (null,0,threadfunc,&g_x,0,&dwThreadID);} DWORD rc = WaitForMultipleObjects (Kthreadnum, Hthreads, TRUE, INFINITE); assert (rc = = WAIT_OBJECT_0); for (int i = 0; i< k Threadnum; ++i) {CloseHandle (hthreads[i]);} ASSERT (g_x = = 10000); assert (setted = = 1);} int _tmain (int argc, _tchar* argv[]) {testinterlockedexchangeadd (); return 0;}
[Concurrent parallel]_[thread synchronization]_[windows The interlocking function of atomic access under user-state]