This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Threads support and pthreads


Eric Buddington <eric@sparrow.vgernet.net> writes:

> Are guile threads compatible with pthreads? IOW, does using guile
> preclude use of pthreads elsewhere in my code, or will it work if I
> confine Guile access to one of my pthreads?

You must confine Guile to one of your threads,  This of course means
that no other thread is allowed to call Guile functions.  Furthermore,
it is the thread which is using Guile which should call Guile's init
function, and it shouldn't return until it is finished using it.
(You'll see what I mean when you look at it.)

> I take it from the discussion that they're not really the same animal
> (guile-threads and pthreads); there's discussion of guile code
> controlling thread switching, which implies that they explicitly yield
> control, right?

Yes.  Guile's current threads are really cooperative---a thread must
explicitly leave ontrol to another.  However, this is handled in the
Guile interpreter so that it will seem like preemptive threads to the
user.  There is a Guile variant of `select', called
scm_internal_select.  If you consistently use this in your
application, threading will never block.

> I don't need an elaborate answer - just a general idea so I don't
> design badly.

It seems to me that you'd like to use pthreads in your application.
I'd like to point out that it is actually quite possible to use
Guile's own threads, also in your application.

The recent snapshots (if you want to download it now, you should
rather wait until tomorrow, since that snapshot has some important
fixes) include a C interface to the threads which is similar to
pthreads.  For example, you can use:

scm_mutex_init (scm_mutex_t* m, ...) to init a mutex and
scm_mutex_lock(scm_mutex_t* m)  and scm_mutex_unlock (...) to lock and
unlock it.

You use `scm_spawn_thread' to start a thread.  It has an interface
which is identical to that of `scm_internal_catch'.

There is one more requirement if you want to use Guile's own threads
in your application: if you have code segments which run for a long
time without calling scm_internal_select to sleep or wait for I/O, you
should include a intermittent calls to SCM_TICK to make thread
switching possible.  (SCM_TICK won't exist under that name until
tomorrow's snapshot!.)

I should add that at least I regard the current Guile threads only as
an interim solution.  We should really move over to pthreads at some
time, otherwise we won't be able to use SMP.

Best regards,
/mdj