chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Repetitive callback into scheme from C thread hangs


From: felix winkelmann
Subject: Re: [Chicken-users] Repetitive callback into scheme from C thread hangs
Date: Thu, 24 Nov 2005 08:04:38 +0100

On 11/23/05, Matthew David Parker <address@hidden> wrote:
>
> My program was using an SDL thread, actually, to run.  That's probably the
> problem.  I got it running using native threads, using the srfi-18 module.
> But, after trying it out I found that I couldn't run anything else at the
> same time, so it was sort of pointless.
>
> Consider this:
>
> (define jim (lambda ()
>         (begin
>                 (printf "a\n")
>                 (sleep 10)
>                 (printf "b\n"))))
>
> (define jim_thread (make-thread jim))
>
>
> Then when I run in the interpreter:
> > (thread-start! jim_thread)
>
> The read-eval-print loop input is no longer responsive.  It prints out "a"
> and then "b" and if i were to type (+ 4 3) in the console it would wait until
> the thread is finished and then do (+ 4 3).

The srfi-18 library implements user-level threads. Chicken does not use
native threads. You should replace `sleep' with `thread-sleep!' in
the example above.

>
> I just want to call my game_launch function in a thread and then be able
> to run other things in the interpreter.  Is there some easy way to do it,
> or is there some example I could look at that does this?

You can simply use SRFI-18 operations. If you invoke C code in one
of the threads, make sure to do a callback into Scheme from time
to time (possibly doing a `thread-yield!') to give the interpreter thread
a chance to run. But you should only call C from one thread, or you
get into trouble...

% cat x.scm
(use srfi-18)

#>!
void callback();

static void some_c_code()
{
  callback();
}
<#

(define count 0)

(define-external (callback) void
  (set! count (add1 count))
  (when (zero? (modulo count 100000)) (print* "\x07"))
  (thread-yield!) )

(thread-start!
 (rec (loop)
      (callback)
      (loop)))

% csc -s x.scm
% csi -q x.so
#;>  ... enter expressions here, waiting for the annoying beep to stop...


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]