[Mono-list] Calls to CIL code from native threads
Tue, 27 Jan 2004 16:52:13 +0100
On 01/26/04 Patrick Hartling wrote:
> I am working on a project that mixes CIL code with natively compiled C
> and C++ in a multi-threaded environment. The natively compiled code may
> make calls into the CIL universe from different threads, and I am trying
> to figure out how to manage that with Mono's C API. In the
> documentation about embedding Mono, I see a reference to the function
> mono_thread_attach(). My (very preliminary) testing has allowed me to
> make a call from a native thread other than the primordial thread into
> the CIL universe by calling mono_thread_attach() first. I pass in a
> pointer to a MonoDomain object allocated on the heap by the calling
> thread via mono_domain_create().
Are you really using multiple domains in your app?
If that's not the case you should pass the MonoDomain you get back from
> After the call into the CIL code returns, however, I appear to get into
> a deadlock state later when I make another call to
> mono_thread_attach(). I have successfully dealt with a similar
mono_thread_attach() should be called only once per-thread.
If you're using multiple application domains, you may get all sort of
issues, since the mono_thread_attach() implementation is broken in that
case, there is already a bug filed about that.
> situation using Python/C where the Global Interpreter Lock (GIL) must be
> acquired and released to allow native code threads to call into the
> Python interpreter. I am wondering if calling mono_thread_attach() has
> the effect of acquiring a mutex somewhere that needs to be released by
> my code when I no longer need to hold it. Is that the case? If so,
> what is the call to make this happen? Or am I just on the wrong track
If you're using multiple appdomains you're hitting a mono bug. If you're
not, just try using mono_thread_attach() once per thread.
> Lastly, is there any documentation for doing multi-threaded programming
> with Mono? So far, I have mostly been looking through the code trying
> to find types and functions that look roughly like what I need.
The API is supposed to be thread-safe, so there should be no particular
thing to do apart from calling mono_thread_attach() in threads not
created by mono. Please, see the suggestions above and report bavk if
they solve your issue.
firstname.lastname@example.org Monkeys do it better