manpagez: man pages & more
info bigloo
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.1 Thread Common Functions

This section describes the functions that are available independently of the multi-threading library.

SRFI-18 function: mutex? obj
SRFI-18 function: make-mutex [name]
SRFI-18 function: mutex-name mutex
SRFI-18 function: mutex-specific mutex
SRFI-18 function: mutex-specific-set! mutex obj
SRFI-18 function: mutex-state mutex
SRFI-18 function: mutex-lock! mutex [timeout [thread]]
SRFI-18 function: mutex-unlock! mutex
 
(let ((m (make-mutex)))
   (thread-start!
    (make-thread (lambda ()
                    (let loop ()
                       (if (mutex-lock! m 0)
                           (begin
                              (display "locked")
                              (mutex-unlock! m))
                           (begin
                              (thread-yield!)
                              (loop))))))))
  -| locked

(let ((res '()))
   (define (mutex-lock-recursively! mutex)
      (if (eq? (mutex-state mutex) (current-thread))
          (let ((n (mutex-specific mutex)))
             (mutex-specific-set! mutex (+ n 1)))
          (begin
             (mutex-lock! mutex)
             (mutex-specific-set! mutex 0))))
   (define (mutex-unlock-recursively! mutex)
      (let ((n (mutex-specific mutex)))
         (if (= n 0)
             (mutex-unlock! mutex)
             (mutex-specific-set! mutex (- n 1)))))
   (thread-start!
    (make-thread
     (lambda ()
        (let ((m (make-mutex)))
           (mutex-lock-recursively! m)
           (mutex-lock-recursively! m)
           (mutex-lock-recursively! m)
           (set! res (cons (mutex-specific m) res))
           (mutex-unlock-recursively! m)
           (mutex-unlock-recursively! m)
           (mutex-unlock-recursively! m)
           (set! res (cons (mutex-specific m) res))))))
   res)
  ⇒ (0 2)
Bigloo function: with-lock mutex thunk

The function with-lock evaluates the body of the thunk. The mutex mutex is acquired and released before thunk gets invoked. The function with-lock might be implemented as:

 
(define (with-lock mutex thunk)
   (mutex-lock! mutex)
   (unwind-protect
      (thunk)
      (mutex-unlock! mutex)))
SRFI-18 function: condition-variable? obj
SRFI-18 function: make-condition-variable [name]
SRFI-18 function: condition-variable-name cv
SRFI-18 function: condition-variable-specific cv
SRFI-18 function: condition-variable-specific-set! cv obj
SRFI-18 function: condition-variable-wait! cv mutex
SRFI-18 function: condition-variable-signal! cv
SRFI-18 function: condition-variable-broadcast! cv
 
(let ((res 0))
   (define (make-semaphore n)
      (vector n (make-mutex) (make-condition-variable)))
   (define (semaphore-wait! sema)
      (mutex-lock! (vector-ref sema 1))
      (let ((n (vector-ref sema 0)))
         (if (> n 0)
             (begin
                (vector-set! sema 0 (- n 1))
                (mutex-unlock! (vector-ref sema 1)))
             (begin
                (condition-variable-wait! (vector-ref sema 2) (vector-ref sema 1))
                (mutex-unlock! (vector-ref sema 1))
                (semaphore-wait! sema)))))
   (define (semaphore-signal-by! sema increment)
      (mutex-lock! (vector-ref sema 1))
      (let ((n (+ (vector-ref sema 0) increment)))
         (vector-set! sema 0 n)
         (if (> n 0)
             (condition-variable-broadcast! (vector-ref sema 2)))
         (mutex-unlock! (vector-ref sema 1))))
   (let ((sema (make-semaphore 10)))
      (let ((t1 (thread-start! (make-thread
                                (lambda ()
                                   (semaphore-wait! sema)
                                   (set! res (current-time))))))
            (t2 (thread-start! (make-thread
                                (lambda ()
                                   (let loop ((n 10))
                                      (if (> n 0)
                                          (begin
                                             (semaphore-signal-by! sema 1)
                                             (thread-yield!)
                                             (loop (- n 1))))))))))
         (scheduler-start!)
         res)))
  ⇒ 2
Bigloo function: thread-parameter ident

Returns the value of the parameter ident in the current thread. If no value is bound to this parameter, #f is returned.

A thread parameter is implemented by a chunk of memory specific to each thread. All threads are created with an empty set of parameters.

Bigloo function: thread-parameter-set! ident value

Associates a value to a parameter named ident.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2026
Individual documents may contain additional copyright information.