[Ecls-list] Multithreaded code

Juan Jose Garcia Ripoll lisp at arrakis.es
Fri Jan 21 01:41:06 UTC 2005


[Sorry if duplicated, but this message somehow did not arrive to the list]

Valery Syssik wrote:

>> In  either  configuration,  MzScheme  can  co-exist with additional OS
>> threads  that  are  created  by  an extension or an embedding program.
>> However, the additional OS threads must not call any scheme_ 
>> function.   
>
> In my case it is impossible model. 

Well, you have to understand one thing, which is that in a lisp 
implementation, each thread needs to have some resources: special 
variables, stacks for setting up CATCH and UNWIND-PROTECT frames, a lisp 
stack for interpreting, etc. This means, you cannot start arbitrarily a 
thread with your C code and expect that from this thread the lisp 
implementation will work.

You will find the same problem whenever you want code implemented in two 
languages that require different runtime support. You have to 
communicate the other language that you create a thread, and that you 
want to run code on that thread.

In the Common Lisp model, threads are called "processes". To each 
process you associate the resources I mentioned before. This is done at 
creation time with MP:MAKE-PROCESS. Each process has an associated entry 
point, a function which is called when the thread is actually started. 
This is done with MP:ENABLE-PROCESS. In a typical program you do the 
following

(defun entry-point (text)
   (loop
      (sleep 2)
      (print text)))

(mp::process-run-function #'entry-point "thread is running")

I agree that sometimes you want to create the thread in the C world. An 
example that comes to my mind is a webserver that you have extended 
using ECL. For each connection you might want to spawn a thread which 
calls lisp code. Sometimes the thread is given to you and cannot be 
created by ECL.

Well, in that case one should import the thread into the lisp world. The 
code for that is not yet done, but I can create easily a C function 
called ecl_import_thread()  which creates the appropiate resources for 
running code in the given thread.

Juanjo






More information about the ecl-devel mailing list