[Openmcl-devel] how to create concurrent threads

Gary Byers gb at clozure.com
Thu Mar 20 13:42:02 PST 2003

On Mon, 20 Jan 2003, Habib Bhiri wrote:

> Hi,
> I wrote a simple example using xlib (used ffigen), and would like to create a new thread to run the main loop (XNextEvent), so I can have access to the listener while the X Window program is running in another thread. How can I do that? does the NativeThread module (pthreads) available?
> Thank you for your help.
> Kind regards
> Taoufik

This all needs to be documented, and some of the details will change
in OpenMCL 0.14.  I'm working on that (honest.)

The general idea (in both 0.13.x and 0.14) is to create a lisp thread
to run the X event loop, by doing something like:

? (process-run-function "some descriptive name"
     #'(lambda () (run-X-event-loop))) ; there are other ways to do this.

Somewhat confusingly, this'll create a "lisp process", which is closer
to the OS's idea of a thread than to a process.

In 0.13 and earlier, lisp threads are "cooperatively" scheduled: lisp
code runs that periodically and more-or-less automatically decides whether
the current thread should continue to run or whether another thread
should be scheduled.  If code blocks indefinitely in some foreign
function call, that periodic code ("the lisp scheduler") doesn't have
an opportunity to run and no other lisp threads will have an opportunity
to run until the blocking call completes.

In 0.14, lisp threads will be scheduled by the OS, and it's possible
to call blocking foreign functions without interfering with the scheduling
of other lisp threads.

You may find that it's easier to use the CLX interface to X than to
call X libraries directly; there's a copy of a very old version of
CLX on the clozure.com FTP server.

(And yes, I realize that this explanation of how threading works is
woefully inadequate; there are some examples of the use of the
multiprocessing API in the OpenMCL sources themselves and in the
examples folder, and I'm working on a more comprehensive document.)

Openmcl-devel mailing list
Openmcl-devel at clozure.com

More information about the Openmcl-devel mailing list