在Scheme(Bigloo)中更正线程使用

时间:2009-02-11 17:33:36

标签: multithreading scheme bigloo

我正在尝试使用Bigloo实现在Scheme中编写应用程序服务器。代码:

(module server
    (library fthread)
    (main main))

(define *port-num* 8080)

(define (main argv)
    (let* ((socket0 (make-server-socket *port-num*))
           (ts (thread-start! (make-thread (lambda () (start-server socket0))))))
         (scheduler-start!)
         (fprint (current-error-port) "Shutting down...")))

(define (start-server socket0)
        (lambda ()
        (print "Starting server...")
             (let loop ()
            (let ((s (socket-accept socket0)))
                 (print "New connection: " s)
                 (thread-start! (make-thread (lambda () (handle-request s))))
                 (thread-yield!)
                 (loop)))))

(define (handle-request s)
        (print "new request")
    (socket-shutdown s #f))

编译正常,但程序在启动后立即终止:

$ bigloo -o server server.scm
$ ./server 
Shutting down...

我哪里错了?

非常感谢。

1 个答案:

答案 0 :(得分:3)

我不知道Bigloo的fthread库,但可能是你立即从start-server返回lambda而不是做工作吗?

就像我说的,我不知道fthread,所以也许这就是你应该如何使用它。除了在handle-request中你没有这个lambda包装器,你以完全相同的方式调用make-thread

(make-thread (lambda () (start-server socket0)))
; vs
(make-thread (lambda () (handle-request s)))

如果这是问题,您可以通过删除start-server内的lambda,或将第一个make-thread调用更改为

来解决此问题
(make-thread (start-server socket0))