[Mono-list] P/Invoke and threading

Francis Brosnan Blazquez francis at aspl.es
Fri Aug 12 07:45:43 EDT 2005


El jue, 11-08-2005 a las 11:15 +0100, Steven Kirk escribió:
> Hello,

Hi Steven,

> 
> I am trying to wrap libjack in mono using P/Invoke but am having 
> problems with the process callback. However when libjack tries to call 
> the delegate I have registered with it, I get the following error:
> 
> ** ERROR **: Thread 0x409 calling into managed code is not registered 
> with the GC. On UNIX, this can be fixed by #include-ing <gc.h> before 
> <pthread.h> in the file containing the thread creation code.
> 

This is a long term issue already discused. Thread start here:
http://lists.ximian.com/pipermail/mono-list/2004-November/024666.html

Then, discuss keep on moving here (where a solution is described):
http://lists.ximian.com/pipermail/mono-list/2004-December/024864.html


> I assume what this is saying is that libjack is calling the delegate on 
> a thread created by itself, which the GC does not know about. I 
> obviously don't want to go adding #includes into the libjack code, is 
> there any other way around this?
> 

Maybe there is a solution. Depending on your requirements.

Inside the second thread you can find a solution which isolates
threading from unmanaged context from managed one. We are using this
approach on our project. 

If the previous solution doesn't work for you, you can try the
following:

1) Use the GLib main loop module to attach tasks inside a main loop
created by your library (or reusing the main loop the mono runtime
creates). You can find more about this issue here:
http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html

2) Then, write a glue, outside libjack library, that receives the thread
notification and queue the task as a job to be done inside the main loop
With this step you have converted the umanaged thread notification into
a managed event notification. Event notification will run inside the
context of mono.

I could provide you source code for a module which makes previous
description easy. That is, to execute callbacks inside the mono context.
This module will work as the previous "glue" described.

However, if your library is not based on glib, to implement previous
solution will make you mono specific.

Cheers,

-- 
Francis Brosnan Blazquez <francis at aspl.es>



More information about the Mono-list mailing list