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() ??
相关问题
- How to know full paths to DLL's from .csproj f
- the application was unable to start correctly 0xc0
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- vs2017wpf项目引用dll的路径不正确的问题
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Signing an F# Assembly (Strong name component)
- Why windows 64 still makes use of user32.dll etc?
On Windows Vista you can use the one-time initialization functions. Using One-Time Initialization shows how to use them to make sure an event is initialized only once.
You can initialize a global critical section in
DllMain
forDLL_PROCESS_ATTACH
(and clean up forDLL_PROCESS_DETACH
).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 no one accesses the object before its constructor completes, but that's relatively easy.)
There are several common wrappers for CRITICAL_SECTION you can use. MFC's CCriticalSection is the obvious choice, but you can create your own as well.
You can also use a wrapper class and declare a global object of that class. The constructor of the global object will be invoked only once at the startup.
Sure there are many many ways.
and so on. This is no different from any other question of trying to create a single instance of some thing in your code.