DLL线程安全

发布于 2024-10-10 04:37:27 字数 700 浏览 3 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2024-10-17 04:37:27

此处使用的 Windows API 为 CreateMutex 。创建一个命名的互斥对象。当您需要操作共享数据时,请使用互斥体句柄调用 WaitForSingleObject,完成后调用 ReleaseMutex。每个调用 WaitForSingleObject 的线程都获得互斥锁的所有权,并且任何其他调用 WaitForSingleObject 的线程都将停止,直到拥有该互斥锁的线程调用 ReleaseMutex。

当然,我不相信你可以做你想做的事:

  1. Dll 可能被映射到每个进程空间的不同地址。如果是这样,所有的指针都将是错误的。
  2. C++ 不允许对分配进行细粒度控制,并且有许多隐式分配,尤其是在处理 STL 对象时。我不相信你可以得到向量来将所有相关数据存储在共享区域中。

您必须使用 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:

  1. Dlls may be mapped in at different addresses in each process space. If so, all pointers will be incorrect.
  2. C++ does not allow fine grained control over allocations and has many implicit allocations, especially when dealing with STL objects. I don't belive that you can get the vector to store all the relevant data in the shared area.

You are going to have to do this with C style raw arrays.

江城子 2024-10-17 04:37:27

看起来您需要一个系统范围的互斥体来保护您的关键代码部分(不得同时运行的代码)。使函数静态化与此无关,因为它不会阻止不同的应用程序同时运行它。

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.

与往事干杯 2024-10-17 04:37:27

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文