简体   繁体   English

C#使用静态成员的外部库防止多线程

[英]C# External library using static members preventing multithreading

I am using a library that was created to perform a simulation based on input data, with a single entry point, something like Run(Data data) . 我正在使用一个库,该库是根据输入数据和单个入口点(例如Run(Data data)基于输入数据执行模拟的。

However unfortunately the library internally stores values as static members (I don't know why, but I can't change it) so that issues arise when attempting to perform multiple simulations at the same time as the multiple threads are affecting the same data internally. 但是不幸的是,该库在内部将值存储为静态成员(我不知道为什么,但是我不能更改它),以便在尝试同时执行多个模拟时出现问题,因为多个线程在内部影响同一数据。

The reason why I want to run multiple simulations at the same time is to give users the ability to specify a range of values and have all their output aggregates and presented in a comparable format. 我想同时运行多个模拟的原因是使用户能够指定一个值范围,并使所有输出汇总并以可比较的格式显示。

Originally I thought the easiest way to get around this issue would be to write a simple console application which could be spawned as a separate process to perform the calculation and dump the result. 最初,我认为解决此问题的最简单方法是编写一个简单的控制台应用程序,该应用程序可以作为单独的进程生成,以执行计算和转储结果。 However a large amount of data needs to be loaded into memory for the simulation to run and spawning separate processes means that this data needs to be loaded multiple times and is a great deal slower then running the simulations sequentially and is likely to hog a few gigabytes of memory. 但是,要运行模拟并生成单独的进程,需要将大量数据加载到内存中,这意味着该数据需要多次加载,并且比顺序运行模拟要慢得多,并且可能占用数千兆字节的记忆。

So basically I am looking for a way to create local storage for each thread, if I could modify the library I would be looking at code like this: 因此,基本上,我正在寻找一种为每个线程创建本地存储的方法,如果我可以修改该库,那么我将查看如下代码:

[ThreadStatic]
public static int Foo { get; set; }

Is there a way to specify a assembly/static class declaration, without modifying it, to use local thread storage? 有没有一种方法可以指定程序集/静态类声明,而无需对其进行修改,以使用本地线程存储? Or maybe a way to even efficiently create multiple references to the same assembly at runtime? 还是一种甚至可以在运行时高效地创建对同一程序集的多个引用的方法?

You're on the right track with a separate console application, but since the start-up cost is so high, you need a more complicated approach. 您使用单独的控制台应用程序在正确的轨道上,但是由于启动成本很高,因此您需要一种更复杂的方法。

Instead of spawning a new console application each time, create a pool of child processes which you can communicate with (either via stdin/stdout or some other inter-process communication like wcf, remoting, or named pipes). 与其每次都生成一个新的控制台应用程序,不如创建一个您可以与之通信的子进程池(通过stdin / stdout或其他一些进程间通信,例如wcf,远程处理或命名管道)。 Create a wrapper/manager class that keeps track of these processes, spawns new ones as needed, and knows which processes are in use. 创建一个包装器/管理器类,以跟踪这些进程,根据需要生成新的进程,并知道正在使用哪些进程。 When a process is not in use, it can send it a new call and wait for the result. 当一个进程不使用时,它可以向它发送一个新的调用并等待结果。

You could also do the same thing in memory by loading the library multiple times into separate AppDomains, but I personally think separate processes is easier and safer. 通过将库多次加载到单独的AppDomain中,您也可以在内存中执行相同的操作,但是我个人认为单独的过程更容易,更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM