[Mono-list] pthreads in mono

Jestin Stoffel jestin.stoffel at gmail.com
Fri Mar 28 12:15:46 EDT 2008

On Fri, 2008-03-28 at 11:42 -0400, Chris Howie wrote:
> On Fri, Mar 28, 2008 at 8:10 AM, Paolo Molaro <lupus at ximian.com> wrote:
> >  The pointer is the one you got with gps_set_callback, so you can just
> >  ignore the fact that it is a pthread_t pointer and all this thread that
> >  went ballistic on that:
> >
> >         [DllImport("gps")]
> >         static extern int gps_set_callback (IntPtr gps_data, GpsCallback cb, out IntPtr handler);
> >
> >  ...
> >
> >         IntPtr handler;
> >         IntPtr gps = gps_open ("localhost", "2947");
> >         gps_set_callback (gps, mycb, out handler);
> >         ...
> >
> >  lupus
> Ah, it *creates* a thread.  That is a detail I missed when reading the manpage.
> Note that it's not that simple though.  The signature specifies a
> pthread_t*, not a pthead_t**.  It wants a pointer to a structure that
> will be filled in, not a pointer to a pointer (as is IntPtr&).  The
> code as writted above will certainly corrupt the memory next to the
> location that the "handler" local is stored in, unless
> sizeof(pthread_t) is <= IntPtr.Size.
> So there are a basically two options: declare a struct matching the
> pthread_t struct, or create a new byte[] of some size reasonably large
> enough to contain a pthread_t and pass that off to the function.

The only code I found that uses the gps_set_callback function is in
libgpsmm.cpp, which is part of the gpsd source.  Here's how it's used:

int gpsmm::set_callback(void){
	handler = new pthread_t;
	return gps_set_callback(gps_data,hook,handler);

where handler is declared

pthread_t *handler;

Unless I am mistaken, it is the caller's responsibility to create the
pthread_t structure before setting the callback.

Anyways, I went ahead and worked around this issue, and I think my new
solution is better for a few other reasons.  Thanks to everyone who
looked into this for me.


More information about the Mono-list mailing list