[plt-scheme] Re: Using LibFFI along with threads

From: Hans Oesterholt-Dijkema (hdnews at gawab.com)
Date: Tue Jul 17 12:59:40 EDT 2007

I'm sorry. It may be a matter of taste, but I don't like this solution.

Also, starting a starting and synchronizing a thread is usually way
faster than starting a subprocess. Kernels typically have special
logic to support threads these days.

Did you have a look at the spike I sent to the list? It missed the
code in c-threads.c, but that can be found in the sqld-psql-c.plt
package.

Best wishes,

--Hans

Eli Barzilay schreef:
> On Jul 17, Hans Oesterholt-Dijkema wrote:
>   
>> The problem with C libraries is that they do not always provide
>> callback scenarios, nor other ways to divide up functions in smaller
>> parts to get some 'time slicing'.
>>     
>
> You should have *some* way to synchronize threads in C -- my point was
> that the one that is easiest to implement in C is the right solution.
>
> BTW -- here's an alternative:
>
> Creating a subprocess is easy, and probably not much more expensive
> than a thread.  Specifically, it sounds like you're paying enough
> price for DB operations that the difference between a system thread
> and a system process would be insignificant.  So it should be quite
> easy to do what you want to do by running a mzscheme subprocess, and
> communicate with it using stdin/stdout.  This way you don't need to
> deal with ports, tcp, or sockets.  The subprocess doesn't even need to
> load any special files -- you can feed it the code you want.  Here's a
> simple sketch:
>
>   (define (async/channel form)
>     (let-values ([(p pout pin perr)
>                   (subprocess #f #f (current-error-port)
>                               (find-system-path 'run-file)
>                               "-mve" (format "(write (let () ~s))" form))]
>                  [(c) (make-channel)])
>       (close-output-port pin)
>       (thread (lambda ()
>                 (channel-put c (read pout))
>                 (unless (sync/timeout 10 p)
>                   (subprocess-kill p #f)
>                   (unless (sync/timeout 10 p)
>                     (subprocess-kill p #t)))))
>       c))
>
> and an example:
>
>   (define (run-fib n)
>     (async/channel `(begin (define (fib n)
>                              (if (<= n 1) n (+ (fib (- n 1)) (fib (- n 2)))))
>                            (fib ,n))))
>   (map channel-get (map run-fib '(30 31 32 33)))
>
> This can be made much better.  (For example, you can send compiled
> code over to the subprocess, you can add a mechanism that keeps
> running processes around to avoid process creation overhead, you can
> have state in threads, etc.)
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070717/774f40e8/attachment.html>

Posted on the users mailing list.