问题描述
在多线程应用程序中,有一种方法可以确保Critical Section只初始化一次,除了将代码放在DLL main()中。
In a multi threaded application, is there a way to ensure that a Critical Section is initialized only once except for putting the code in DLL main() ??
推荐答案
我建议用一个类来包装CRITICAL_SECTION,该类将处理其构造函数和析构函数中的临界区对象的初始化和未初始化。这样,在大多数情况下,你将是线程安全的(你必须确保没有人在对象的构造函数完成之前访问对象,但这是相对容易的)。
I'd suggest wrapping the CRITICAL_SECTION with a class that will handle the initialization and uninitialization of the critical section object in its constructor and destructor. This way, you'll be thread safe in most cases (you'll have to make sure noone accesses the object before its constructor completes, but that's relatively easy).
有几个常用的包装器,您可以使用CRITICAL_SECTION。 MFC的是显而易见的选择,但您也可以创建自己的。
There are several common wrappers for CRITICAL_SECTION you can use. MFC's CCriticalSection is the obvious choise, but you can create your own as well.
这篇关于仅对进程初始化临界节一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!