Skip to content

Latest commit

 

History

History
97 lines (75 loc) · 5.11 KB

nf-synchapi-setcriticalsectionspincount.md

File metadata and controls

97 lines (75 loc) · 5.11 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NF:synchapi.SetCriticalSectionSpinCount
SetCriticalSectionSpinCount function (synchapi.h)
Sets the spin count for the specified critical section.
SetCriticalSectionSpinCount
SetCriticalSectionSpinCount function
_win32_setcriticalsectionspincount
base.setcriticalsectionspincount
synchapi/SetCriticalSectionSpinCount
winbase/SetCriticalSectionSpinCount
base\setcriticalsectionspincount.htm
base
4d435c70-2e9b-4923-8726-9c8143dceb15
02/05/2024
SetCriticalSectionSpinCount, SetCriticalSectionSpinCount function, _win32_setcriticalsectionspincount, base.setcriticalsectionspincount, synchapi/SetCriticalSectionSpinCount, winbase/SetCriticalSectionSpinCount
synchapi.h
Windows.h on Windows Server 2003, Windows Vista, Windows 7, Windows Server 2008 Windows Server 2008 R2
Windows
Windows XP [desktop apps \| UWP apps]
Windows Server 2003 [desktop apps \| UWP apps]
Kernel32.lib
Kernel32.dll
Windows
19H1
SetCriticalSectionSpinCount
synchapi/SetCriticalSectionSpinCount
c++
APIRef
kbSyntax
DllExport
Kernel32.dll
API-MS-Win-Core-Synch-l1-1-0.dll
KernelBase.dll
API-MS-Win-Core-Synch-l1-2-0.dll
API-MS-Win-Core-Synch-l1-2-1.dll
API-MS-Win-DownLevel-Kernel32-l1-1-0.dll
MinKernelBase.dll
vertdll.dll
SetCriticalSectionSpinCount

SetCriticalSectionSpinCount function

-description

Sets the spin count for the specified critical section. Spinning means that when a thread tries to acquire a critical section that is locked, the thread enters a loop, checks to see if the lock is released, and if the lock is not released, the thread goes to sleep.

-parameters

-param lpCriticalSection [in, out]

A pointer to the critical section object.

-param dwSpinCount [in]

The spin count for the critical section object. On single-processor systems, the spin count is ignored and the critical section spin count is set to zero (0). On multiprocessor systems, if the critical section is unavailable, the calling thread spins dwSpinCount times before performing a wait operation on a semaphore associated with the critical section. If the critical section becomes free during the spin operation, the calling thread avoids the wait operation.

-returns

The function returns the previous spin count for the critical section.

-remarks

The threads of a single process can use a critical section object for mutual-exclusion synchronization. The process is responsible for allocating the memory used by a critical section object, which it can do by declaring a variable of type CRITICAL_SECTION. Before using a critical section, some thread of the process must call the InitializeCriticalSection or InitializeCriticalSectionAndSpinCount function to initialize the object. You can subsequently modify the spin count by calling the SetCriticalSectionSpinCount function.

The spin count is useful for critical sections of short duration that can experience high levels of contention. Consider a worst-case scenario, in which an application on an SMP system has two or three threads constantly allocating and releasing memory from the heap. The application serializes the heap with a critical section. In the worst-case scenario, contention for the critical section is constant, and each thread makes a processing-intensive call to the WaitForSingleObject function. However, if the spin count is set properly, the calling thread does not immediately call WaitForSingleObject when contention occurs. Instead, the calling thread can acquire ownership of the critical section if it is released during the spin operation.

You can improve performance significantly by choosing a small spin count for a critical section of short duration. The heap manager uses a spin count of roughly 4000 for its per-heap critical sections. This gives great performance and scalability in almost all worst-case scenarios.

To compile an application that uses this function, define _WIN32_WINNT as 0x0403 or later. For more information, see Using the Windows Headers.

-see-also

Critical Section Objects

InitializeCriticalSection

InitializeCriticalSectionAndSpinCount

WaitForSingleObject

Synchronization Functions

Vertdll APIs available in VBS enclaves