DLL线程安全 [英] DLL thread safety

查看:249
本文介绍了DLL线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS VC express c ++中开发一个DLL,它将在多个客户端应用程序中同时加载,DLL具有使用 data_seg(。SHARED_SPACE_NAME)创建的共享内存空间。在这个共享内存空间中有一些可以修改的向量。
假设我们在DLL体中有一个名为doCalc()的函数:

  _DLLAPI void __stdcall doCalc )
{
//添加到向量
//循环通过向量
//从向量
//删除//等。
}

如果 doCalc 同时从两个或多个客户端应用程序调用系统将崩溃。
我想要doCalc调用等待在线为以前的调用完成 - 就像是一个单线程的应用程序。
这样,如果客户端1调用,然后立即在客户端2调用之后,则客户端1应该完成该函数,然后客户端2应该运行该函数。



最好的解决方案是将DLL作为一个单独的线程运行,但我已经在互联网上搜索了一个我不认为是可能的。



我试过搜索互联网这个问题,我已经提出了一些关于使函数 static 使它线程安全的问题。



也读过C ++ 0x不知何故会使这个线程安全。但是它不支持在MS VC express。



我没有多线程的经验,所以我希望你能帮助。提前感谢。

解决方案

这里使用的Windows API将是 CreateMutex 。创建命名的互斥对象。当你需要操作共享数据时,用互斥锁调用WaitForSingleObject,当你完成后,调用ReleaseMutex。每个调用WaitForSingleObject的线程都获取互斥量的所有权,任何调用WaitForSingleObject的线程都将停止,直到拥有线程调用ReleaseMutex为止。



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


  1. Dll可以映射在每个进程空间中的不同地址。

  2. C ++不允许对分配进行细粒度控制,并有许多隐式分配,尤其是在处理STL对象时。我不相信你可以得到向量来存储共享区域中的所有相关数据。

你将有使用C风格的原始数组。


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.

解决方案

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.

这篇关于DLL线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆