使用 AppDomains 并行化非线程安全的 DLL [英] Using AppDomains to Parallelize Non-thread-safe DLL

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

问题描述

我有一个非托管 C++ DLL,我的 .NET 应用程序通过 p/invoke 使用它.我需要从这个 DLL 获得的方法相当耗时,我想并行化方法调用.问题是它使用了一堆静态变量和全局变量,因此它不是线程安全的(并且无法更改).我的计划是通过从多个 AppDomain 并行调用非托管 DLL 来克服这个非线程安全问题.

I have an unmanaged C++ DLL that my .NET application uses via p/invoke. The method that I need from this DLL is fairly time consuming and I would like to parallelize the method calls. The problem is that it uses a bunch of statics and globals, so it is not thread-safe (and can't be changed). My plan was to overcome this non-thread-safe issue by calling the unmanaged DLL from multiple AppDomains in parallel.

我可以从多个 AppDomain 调用非托管代码而不会出现任何问题,只要我不并行执行,但是一旦我并行调用,我就会收到 AccessViolationException.我正在使用 Parallel.For() 进行并行调用.

I can call the unmanaged code from the multiple AppDomains without any problems as long as I don't do it in parallel, but as soon as I make the calls in parallel, I get an AccessViolationException. I am using Parallel.For() to make the parallel calls.

是否可以通过简单地从多个 AppDomain 进行调用来使非线程安全的非托管 DLL 成为线程安全的"?

Is it possible to make a non-thread-safe unmanaged DLL "thread-safe" by simply making the calls from multiple AppDomains?

推荐答案

从多个 AppDomain 实例调用本机方法在这里根本没有帮助.AppDomain 边界不适用于本机 DLL,它们不会提供任何好处

Calling the native method from multiple AppDomain instances won't help you at all here. AppDomain boundaries don't apply to native DLL's and they won't provide any benefit

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

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