Mutex: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
|||
Line 641: | Line 641: | ||
t = res_thread() |
t = res_thread() |
||
t.start()</lang> |
t.start()</lang> |
||
=={{header|Racket}}== |
|||
Racket has semaphores which can be used as mutexes in the usual way. With other language features this can be used to implement new features -- for example, here is how we would implement a protected-by-a-mutex function: |
|||
<lang Racket>(define foo |
|||
(let ([sema (make-semaphore 1)]) |
|||
(lambda (x) |
|||
(dynamic-wind (λ() (semaphore-wait sema)) |
|||
(λ() (... do something ...)) |
|||
(λ() (semaphore-post sema))))))</lang> |
|||
and it is now easy to turn this into a macro for definitions of such functions: |
|||
<lang Racket>(define-syntax-rule (define/atomic (name arg ...) E ...) |
|||
(define name |
|||
(let ([sema (make-semaphore 1)]) |
|||
(lambda (arg ...) |
|||
(dynamic-wind ( () (semaphore-wait sema)) |
|||
( () E ...) |
|||
( () (semaphore-post sema))))))) |
|||
;; this does the same as the above now: |
|||
(define/atomic (foo x) |
|||
(... do something ...))</lang> |
|||
But more than just linguistic features, Racket has many additional synchronization tools in its VM. Some notable examples: OS semaphore for use with OS threads, green threads, lightweight OS threads, and heavyweight OS threads, synchronization channels, thread mailboxes, CML-style event handling, generic synchronizeable event objects, non-blocking IO, etc, etc. |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |