Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several AppDomains and native code

My C# application is using native code which is not thread safe.

I can run multiple processes of that native code, using inter-process communication to achieve concurrency.

My question is, can i use App Domains instead, so that several managed threads, each on a different App Domain, will call the native code and they will not interfere with each other?

The main goal is to prevent process seperation.

like image 547
Hadar Grubman Avatar asked Apr 29 '14 14:04

Hadar Grubman


People also ask

What is AppDomain in C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.

How many application domain can be used in single process?

Process A runs managed code with one application domain while Process B runs managed code has three application domains. Note that Process C which runs unmanaged code has no application domain.

What is application domain example?

6.3. Application domains include banks, insurance companies, or hospitals. In this book, equipment management for a small software company is our main example. Internet applications have become increasingly important, especially for the home and entertainment domains.


2 Answers

No, AppDomains are a pure managed code concept. It achieves isolation by keeping the managed object roots separate. One AppDomain cannot see the objects of another, makes it very safe to abort code and unload assemblies. Never an accident, it throws away all the data that might contain state.

Unmanaged code is completely agnostic of the GC heap and thus AppDomains, it will allocate in its data section and its own native heap (HeapAlloc). Such allocations are process-global. That makes a process the isolation boundary, you'd need a helper process that loads the DLL and talk to it with one of the .NET process interop mechanisms (socket, named pipe, memory-mapped file, remoting, WCF).

Technically you could create copies of the DLL, each with a different name. But that scales very poorly and the pinvoke is very awkward since you can't use [DllImport] anymore. You need a delegate declaration for each exported function and LoadLibrary() and GetProcAddress() to initialize the delegate objects.

like image 98
Hans Passant Avatar answered Oct 13 '22 06:10

Hans Passant


Yes it can be done but you should seriously measure if effort is repaid by benefits.

Windows won't load multiple copies of an unmanaged DLL and unmanaged DLLs are loaded per-process (not per AppDomain). What you can do is to create multiple temporary copies of same DLL then load them with LoadLibrary().

Each one will be loaded per-process but they'll be separated from each other (so they'll be thread-safe). All this stuff can be tied inside a class that wraps unmanaged calls (LoadLibrary, FreeLibrary, GetProcAddress and invocation itself). It'll use less resources and it'll be faster than multiple processes but you'll have to drop DllImport usage.

The only benefit I see is that this will scale much better than multiple processes (because it uses less resources) of course if you reuse instances keeping a cache (it's harder to keep a process cache than an object cache).

like image 30
Adriano Repetti Avatar answered Oct 13 '22 06:10

Adriano Repetti