|  |  | Threads

# Threads

Threads allow for expressing concurrent programs. The assumed model is shared memory with explicit synchronization and symmetric multiprocessing and is based heavily upon pthreads This section is preliminary and might change in the future. There are several limitations in the current implementation. It represents a rudimentary but usable subset of typical thread operations. Tables and vectors require user locks to ensure thread safety and no out of language crashes. Finally, the compiler, interpreter, and (re)definition machinery are thread unsafe and can only reliably be run in one thread.

`<thread>` | `(<any>)` | `C`
| Represents a thread of executation schedulable across multiple processors. Upon creation executes `thread-function` in separate thread. |
`thread-name` | `(thread|<thread> => (t? <sym>))` | `P`
`thread-priority` | `(thread|<thread> => <int>)` | `P`
`thread-function` | `(thread|<thread> => <fun>)` | `P`
`new` | `(x|(t< <thread>) inits|... => <thread>)` | `M`
| Creates thread and runs `thread-function` of created thread in separate OS thread. |
`SPAWN` | `(SPAWN ,@body)` | `S`
| == `(FAB <thread> thread-function (fun () ,@body))` |
`thread-yield` | `()` | `M`
| Surrenders processor to another thread. |
`thread-join` | `(thread|<thread>)` | `M`
| Causes current thread to wait for the termination of `thread`. |
`thread-current` | `(=> <thread>)` | `M`
`all-threads` | `(=> <tup>)` | `M`
`sleep` | `(secs|<flo>)` | `M`
| Pauses current thread for `secs` seconds. |
`<lock>` | `(<any>)` | `C`
| Represents a mutex. |
`lock-name` | `(lock|<lock> => (t? <sym>))` | `P`
`new` | `(x|(t< <lock>) inits|... => <lock>)` | `M`
`lock-lock` | `(lock|<lock>)` | `M`
| Obtain exclusive access to `lock` waiting if necessary. |
`lock-unlock` | `(lock|<lock>)` | `M`
| Free up exclusive access to `lock` potentially allowing another thread access. |
`WITH-LOCK` | `(WITH-LOCK ,lock ,@body)` | `S`
| == `(FIN (SEQ (lock-lock ,lock) ,@body) (lock-unlock ,lock))` |
`<event>` | `(<any>)` | `C`
| Represents a condition variable used for interthread notification. |
`event-name` | `(event|<event> => (t? <sym>))` | `P`
`new` | `(x|(t< <event>) inits|... => <event>)` | `M`
`event-signal` | `(event|<event> lock|<lock>)` | `M`
| Unblocks at least one thread waiting on `event`. |
`event-broadcast` | `(event|<event> lock|<lock>)` | `M`
| Unblocks all threads waiting on `event`. |
`event-wait` | `(event|<event> lock|<lock> => <log>)` | `M`
| Unlocks `lock` and places thread in waiting state to be resumed when `event` is signaled or broadcasted upon which time `lock` is reacquired and thread resumed. |
`event-wait-timed` | `(event|<event> lock|<lock> secs|<flo> => <log>)` | `M`
| Unlocks `lock` and places thread in waiting state to be resumed when `event` is signaled or broadcasted or timeout `secs` is reached upon which time `lock` is reacquired and thread resumed. |
`DDV` | `(DDV ,var ,form)` | `S`
| Defines a thread local variable named `(var-name ,var)` with an initial value `,form`. |
`DLET` | `(DLET ((,var ,val) ...) ,@body)` | `S`
| == `(LET ((,old-var ,var) ...) (FIN (SEQ (SET ,var ,val) ... ,@body) (SET ,var ,old-var) ...))` |
`<pipe>` | `(<flat> <seq!>)` | `C`
| Represents a synchronized FIFO queue allowing multiple readers and writers in separate threads. |
`enq!` | `(pipe|<pipe> x => <pipe>)` | `M`
| Adds `x` to `pipe`. |
`deq!` | `(pipe|<pipe> => <any>)` | `M`
| Removes and returns element from `pipe` or waits for one to be available. |

|  |  | Threads