Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

练习3.47的疑问 这里在release的时候是不是也需要给n加锁 还是说默认这里的set!就是原子化的 #61

Open
DaveAimee opened this issue Sep 26, 2019 · 0 comments

Comments

@DaveAimee
Copy link

;;; 47-semaphore-using-test-and-set.scm

(define (make-semaphore n)

(define (acquire)
    (if (test-and-set! n)
        (acquire)
        'ok))

(define (release)
    (set! n (+ n 1))
    'ok)

(define (dispatch mode)
    (cond ((eq? mode 'acquire)
            (acquire))
          ((eq? mode 'release)
            (release))
          (else
            (error "Unknown mode MAKE-SEMAPHORE" mode))))

dispatch)

(define (test-and-set! n)
(if (= n 0)
#t
(begin (set! n (- n 1))
#f)))

#|
; 根据注释 174
; 以下是一个可以在采用时间片模型的单处理器的 MIT Scheme 里实际运行的 test-and-set!

(define (test-and-set! n)
(without-interrupts
(lambda ()
(if (= n 0)
#t
(begin (set! n (- n 1))
#f)))))

|#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant