読者です 読者をやめる 読者になる 読者になる

SICPゼミ第24回

練習問題3.46

スレッド1が(car cell)した結果falseが返ってくる→スレッド1が(set-car! cell true)する前にスレッド2が(car cell)してfalseを得る→同時に2つのスレッドがmutexを獲得できたものとして実行されてしまう。

練習問題3.47

(a)

(define (make-semaphore n)
  (let ((counter 0) (m (make-mutex)))
    (define (the-semaphore ord)
      (cond
        ((eq? ord 'acquire)
          (begin (m 'acquire)
            (if (< counter n) (begin (set! counter (+ counter 1)) (m 'release))
              (begin (m' release) (the-semaphore 'acquire)))
            ))
        ((eq? ord) (begin (m' acquire) (set! counter (- counter 1)) (m' release))))
       the-semaphore))

by dolicas

(b)

(define (make-semaphore)
  (let ((cell (make-cell n)))
    (define (list-test-and-set! cell)
      (if (test-and-set! cell)
          (if (null? (cdr cell))
              (the-semaphore 'acquire )
              (list-test-and-set! (cdr cell)))
          #f))
    (define (the-semaphore m)
      (cond ((eq? m 'acquire)
             (list-test-and-set! cell))
            ((eq? m 'release) (clear-cell! cell ))))
    the-semaphore ))
  
(define (make-cell n)
  (if (= n 0)
      '()
      (cons #f (make-cell (- n 1)) )))

clear-cell! は無理。

by tube