网站首页  词典首页

请输入您要查询的函数:

 

术语 entercriticalsection
释义 EnterCriticalSection
语法:
C++
void WINAPI EnterCriticalSection(
__inout LPCRITICAL_SECTION lpCriticalSection
);
加锁功能
等待指定的临界区对象的所有权。该函数返回时,调用线程被授予权。
参数
lpCriticalSection [ in , out ]
一个临界区对象的指针。
返回值
这个函数没有返回值。
此功能可以提高EXCEPTION_POSSIBLE_DEADLOCK如果在关键的第次等待操作了。超时间隔指定由以下注册表值:HKEY_LOCAL_MACHINE \\系统\\ CurrentControlSet \\控制\\会话管理器\\ CriticalSectionTimeout。不处理可能的僵局例外,而是调试应用程序。
Windows 2000:在内存不足的情况下,加锁可以抛出一个异常。不要试图处理这个异常,而是要么终止进程或允许例外,传递给未处理的异常过滤器。为了避免因异常低内存,调用InitializeCriticalSectionAndSpinCount函数预先分配的,而不是调用InitializeCriticalSection函数,强制加锁分配EnterCriticalSection的事件所使用的事件。预先根据该事件没有必要在Windows XP或更高版本,因为加锁保证不会失败,由于缺乏资源。
备注
单个进程的线程可以使用一个互斥同步临界区对象。这个过程负责分配一个关键节对象,它可以通过声明类型的CRITICAL_SECTION的变量使用的内存。在使用一个关键部分,这一进程的某个线程必须调用InitializeCriticalSection或InitializeCriticalSectionAndSpinCount初始化的对象。
为使相互排斥的访问共享资源,每个线程调用加锁或TryEnterCriticalSection功能要求在执行任何代码,访问受保护的资源部分临界区的所有权。不同的是,TryEnterCriticalSection立即返回,无论是否获得了临界区的所有权,而加锁块,直到线程可以利用临界区的所有权。当它已经完成了受保护的代码执行的线程使用LeaveCriticalSection放弃所有权的功能,使另一个线程,成为业主和访问受保护的资源。目前没有关于顺序等待线程将收购的关键部分所有权的保证。
经过线程临界区的所有权,它可以使更多的调用EnterCriticalSection的或TryEnterCriticalSection没有阻止其执行。这可以防止死锁的线程,而本身就是一个关键节,它已经拥有等待。该线程进入临界区每次EnterCriticalSection和TryEnterCriticalSection成功。线程必须要求每次LeaveCriticalSection一旦它进入临界区。
任何进程中的线程可以使用DeleteCriticalSection函数来释放分配制度时,关键节对象初始化的资源。在此之后的功能被称为临界区对象不能用于同步。
如果一个线程终止,同时它有一个关键部分所有权,临界区状态是不确定的。
如果一个关键部分删除,而它仍然拥有的对删除的关键部分所有权等待线程的状态是不确定的。
实例
举一个例子,它使用加锁,请参阅使用临界区对象。
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
参见
临界区对象
DeleteCriticalSection
InitializeCriticalSection
InitializeCriticalSectionAndSpinCount
LeaveCriticalSection
同步功能
TryEnterCriticalSection
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月27日
==英文原文==EnterCriticalSection Function
Waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.
Syntax
C++
void WINAPI EnterCriticalSection(
__inout LPCRITICAL_SECTION lpCriticalSection
);
Parameters
lpCriticalSection [in, out]
A pointer to the critical section object.
Return Value
This function does not return a value.
This function can raise EXCEPTION_POSSIBLE_DEADLOCK if a wait operation on the critical section times out. The timeout interval is specified by the following registry value: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\CriticalSectionTimeout. Do not handle a possible deadlock exception; instead, debug the application.
Windows 2000: In low memory situations, EnterCriticalSection can raise an exception. Do not attempt to handle this exception; instead, either terminate the process or allow the exception to pass to the unhandled exception filter. To avoid an exception due to low memory, call the InitializeCriticalSectionAndSpinCount function to preallocate the event used by EnterCriticalSection instead of calling the InitializeCriticalSection function, which forces EnterCriticalSection to allocate the event. Preallocating the event is not necessary on Windows XP or later because EnterCriticalSection is guaranteed not to fail due to lack of resources.
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 InitializeCriticalSection or InitializeCriticalSectionAndSpinCount to initialize the object.
To enable mutually exclusive access to a shared resource, each thread calls the EnterCriticalSection or TryEnterCriticalSection function to request ownership of the critical section before executing any section of code that accesses the protected resource. The difference is that TryEnterCriticalSection returns immediately, regardless of whether it obtained ownership of the critical section, while EnterCriticalSection blocks until the thread can take ownership of the critical section. When it has finished executing the protected code, the thread uses the LeaveCriticalSection function to relinquish ownership, enabling another thread to become owner and access the protected resource. There is no guarantee about the order in which waiting threads will acquire ownership of the critical section.
After a thread has ownership of a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns. The thread enters the critical section each time EnterCriticalSection and TryEnterCriticalSection succeed. A thread must call LeaveCriticalSection once for each time that it entered the critical section.
Any thread of the process can use the DeleteCriticalSection function to release the system resources that were allocated when the critical section object was initialized. After this function has been called, the critical section object can no longer be used for synchronization.
If a thread terminates while it has ownership of a critical section, the state of the critical section is undefined.
If a critical section is deleted while it is still owned, the state of the threads waiting for ownership of the deleted critical section is undefined.
Examples
For an example that uses EnterCriticalSection, 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
InitializeCriticalSectionAndSpinCount
LeaveCriticalSection
Synchronization Functions
TryEnterCriticalSection
Send comments about this topic to Microsoft
Build date: 8/27/2009
==原始网址==http://msdn.microsoft.com/en-us/library/ms682608(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 11:31:30