网站首页  词典首页

请输入您要查询的函数:

 

术语 initializecriticalsectionandspincount
释义 InitializeCriticalSectionAndSpinCount
语法:
C++
BOOL WINAPI InitializeCriticalSectionAndSpinCount(
__out LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);
InitializeCriticalSectionAndSpinCount功能
初始化临界区对象,并设置了关键部分旋转的罪名。当一个线程试图获得一个关键部分是锁定纺纱方法,线程进入一个循环,检查是否锁被释放,如果不释放锁,线程进入睡眠状态。
参数
lpCriticalSection [out]
一个临界区对象的指针。
dwSpinCount [in]
为临界区对象的旋转数。在单处理器系统,旋转数被忽略,关键节自旋计数设置为0(零)。多处理器系统上,如果临界区不可用,调用线程在执行自旋与关键部分相关联的信号等待操作dwSpinCount倍。如果临界区将在这旋转行动自由,避免调用线程等待操作。
Windows 2000的:如果高序位被设置,职能预分配的加锁函数所使用的事件。预分配保证进出临界区将不会提高,在内存不足的情况例外。不要设置此位如果您要创建一个关键节大量对象,因为它消耗了大量的非分页池。请注意,此事件与Windows XP和高序位出发的需求分配被忽略。
返回值
这个函数总是返回非零值。
Windows Server 2003和Windows XP/2000操作系统:
如果函数成功,返回值为非零。如果函数失败,返回值是零(0)。为了获得更多错误信息,调用GetLastError。这种行为是改变了Windows Vista的开始。
备注
单个进程的线程可以使用一个互斥同步临界区对象。目前没有关于以保证线程获得临界区的所有权,但是,该系统是公平的所有线程。
这个过程负责分配一个关键节对象,它可以通过声明类型的CRITICAL_SECTION的变量使用的内存。在使用一个关键部分,这一进程的某个线程必须初始化的对象。随后修改,您可以通过调用SetCriticalSectionSpinCount函数的旋转数。
经过一个临界区对象初始化,该进程的线程可以指定在加锁,TryEnterCriticalSection,或LeaveCriticalSection对象的功能,提供互相排斥的访问共享资源。对于不同进程之间的类似线程同步,使用互斥对象。
一个关键部分对象不能移动或复制。这一进程也绝不能修改该对象,但必须把它作为逻辑不透明。只能使用临界区的管理职能,关键节对象。当您使用完的关键部分,调用DeleteCriticalSection函数。
临界区对象必须删除才能重新初始化。初始化一个关键节已初始化未定义的行为的结果。
自旋计数有益的短暂时间,可以体验高争的关键部分。考虑最坏的情况,其中一个在SMP系统中的应用有两个或三个线程不断地分配和释放的堆内存。该应用程序序列化的关键节堆。在最坏的情况下,对关键节的争夺是恒定的,并且每个线程使得对昂贵WaitForSingleObject函数调用。然而,如果旋转计数设置正确,调用线程不会立即调用WaitForSingleObject时争发生。相反,调用线程可以获取临界区的所有权,如果是,在旋转操作释放。
您可以显着改善性能选择了短暂的临界区小旋转数。例如,堆管理器使用的每堆临界区的大约4000旋转计数。
要编译的应用程序使用此功能,定义_WIN32_WINNT为0x0403或更高版本。有关详细信息,请参阅使用Windows头。
实例
举一个例子,它使用InitializeCriticalSectionAndSpinCount,请参阅使用临界区对象。
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
参见
临界区对象
DeleteCriticalSection
InitializeCriticalSection
SetCriticalSectionSpinCount
同步功能
WaitForSingleObject
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月27日
==英文原文==InitializeCriticalSectionAndSpinCount Function
Initializes a critical section object and sets the spin count for the 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.
Syntax
C++
BOOL WINAPI InitializeCriticalSectionAndSpinCount(
__out LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);
Parameters
lpCriticalSection [out]
A pointer to the critical section object.
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 0 (zero). On multiprocessor systems, if the critical section is unavailable, the calling thread spin 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.
Windows 2000: If the high-order bit is set, the function pre-allocates the event used by the EnterCriticalSection function. Pre-allocation guarantees that entering or leaving the critical section will not raise an exception in low memory conditions. Do not set this bit if you are creating a large number of critical section objects, because it consumes a significant amount of nonpaged pool. Note that this event is allocated on demand starting with Windows XP and the high-order bit is ignored.
Return Value
This function always returns a nonzero value.
Windows Server 2003 and Windows XP/2000:
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error information, call GetLastError . This behavior was changed starting with Windows Vista.
Remarks
The threads of a single process can use a critical section object for mutual-exclusion synchronization. There is no guarantee about the order that threads obtain ownership of the critical section, however, the system is fair to all threads.
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 initialize the object. You can subsequently modify the spin count by calling the SetCriticalSectionSpinCount function.
After a critical section object is initialized, the threads of the process can specify the object in the EnterCriticalSection, TryEnterCriticalSection , or LeaveCriticalSection function to provide mutually exclusive access to a shared resource. For similar synchronization between the threads of different processes, use a mutex object.
A critical section object cannot be moved or copied. The process must also not modify the object, but must treat it as logically opaque. Use only the critical section functions to manage critical section objects. When you have finished using the critical section, call the DeleteCriticalSection function.
A critical section object must be deleted before it can be reinitialized. Initializing a critical section that is already initialized results in undefined behavior.
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 an expensive 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. For example, the heap manager uses a spin count of roughly 4,000 for its per-heap critical sections.
To compile an application that uses this function, define _WIN32_WINNT as 0x0403 or later. For more information, see Using the Windows Headers .
Examples
For an example that uses InitializeCriticalSectionAndSpinCount, see Using Critical Section Objects .
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
See Also
Critical Section Objects
DeleteCriticalSection
InitializeCriticalSection
SetCriticalSectionSpinCount
Synchronization Functions
WaitForSingleObject
Send comments about this topic to Microsoft
Build date: 8/27/2009
==原始网址==http://msdn.microsoft.com/en-us/library/ms683476(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 13:17:55