DLL线程安全
我正在 MS VC Express C++ 中开发一个 DLL,它将同时加载到多个客户端应用程序中,该 DLL 有一个使用 data_seg(".SHARED_SPACE_NAME")
创建的共享内存空间。在这个共享内存空间中有一些可以修改的向量。 假设 DLL 主体中有一个名为 doCalc() 的函数:
_DLLAPI void __stdcall doCalc(int argument)
{
//Add to vector
//Loop through vector
//Erase from vector
//etc.
}
如果从两个或多个客户端应用程序同时调用 doCalc,系统将崩溃。 我希望 doCalc 调用“排队等待”前一个调用完成 - 就像它是一个单线程应用程序一样。 因此,如果客户端 1 调用,然后在客户端 2 调用之后立即调用,则客户端 1 应该完成该函数,然后客户端 2 应该运行该函数。
最好的解决方案是将 DLL 作为单线程运行,但我在互联网上搜索过,我认为这是不可能的。
我尝试在互联网上搜索这个问题,并且我想出了一些关于使函数 static
使其线程安全的方法。
我还读到 C++0x 会以某种方式使其线程安全。但 MS VC Express 不支持它。
我对多线程没有经验,所以希望你能帮忙。提前致谢。
I am developing a DLL in MS VC express c++ that will be loaded in multiple client applications at the same time, the DLL has a shared memory space created using data_seg(".SHARED_SPACE_NAME")
. In this shared memory space there are some vectors that can be modified.
Lets assume we have a function in the DLL body called doCalc():
_DLLAPI void __stdcall doCalc(int argument)
{
//Add to vector
//Loop through vector
//Erase from vector
//etc.
}
If doCalc
is called at the same time from two or more client applications the system will crash.
I want the doCalc calls to "wait in line" for the previous call to finish - like it was a single-threaded application.
So that if client 1 calls and then immediately after client 2 calls, then client 1 should finish the function, and then client 2 should run the function.
The best solution would be to run the DLL as a single thread but I have searched the internet a I do not think it is possible.
I have tried searching the internet for this issue, and I have come up with something about making the function static
would make it thread safe.
I have also read that C++0x somehow will make this thread-safe. But that it is not supported in MS VC express.
I have no experience in multithreading, so I hope you can help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处使用的 Windows API 为 CreateMutex 。创建一个命名的互斥对象。当您需要操作共享数据时,请使用互斥体句柄调用 WaitForSingleObject,完成后调用 ReleaseMutex。每个调用 WaitForSingleObject 的线程都获得互斥锁的所有权,并且任何其他调用 WaitForSingleObject 的线程都将停止,直到拥有该互斥锁的线程调用 ReleaseMutex。
当然,我不相信你可以做你想做的事:
您必须使用 C 风格的原始数组来执行此操作。
The Windows API to use here would be CreateMutex. Create a named mutex object. As you need to manipulate the shared data, call WaitForSingleObject with the mutex handle, and when you are done, call ReleaseMutex. Each thread that calls WaitForSingleObject takes ownership of the mutex and any other thread that calls WaitForSingleObject will stall, until the owning thread calls ReleaseMutex.
Of course, I don't belive you can do what you want to do:
You are going to have to do this with C style raw arrays.
看起来您需要一个系统范围的互斥体来保护您的关键代码部分(不得同时运行的代码)。使函数静态化与此无关,因为它不会阻止不同的应用程序同时运行它。
Looks like you need a system-wide mutex that will protect your critical section of code (the code that mustn't run simultaneously). Making the function static has nothing to do with it, because it doesn't prevent different applications from running it at the same time.
我认为 Boost.Interprocess 正是您所需要的。它将解决同步问题和 Jim Brissom 在他的评论中所说的你甚至还没有考虑过的问题。
I think that Boost.Interprocess is exactly what you need. It will solve both, the synchronization problem, and the one that Jim Brissom said in his comment that you even haven't thought about yet.