15.1 Threads
A thread is a concurrent thread of evaluation. Rhombus schedules threads preemptively, but conceptually on the same physical processor as a coroutine thread by default; coroutine threads provide concurrency, but not parallelism. Create a parallel thread by specifying a parallel thread pool when creating the thread. Threads can communicate via shared state, and they can synchronize using semaphores or other synchronizable events.
A Thread object is itself a synchronizable event that is ready for synchronization when the thread has terminated. The synchronization result is just the thread object itself.
class | ||||||||
| ||||||||
| ||||||||
expression | ||||||||
| ||||||||
| ||||||||
| ||||||||
| ||||||||
enumeration | ||||||||
|
If pool is #false, then the thread is created as a coroutine thread. If pool is a Thread.Pool, then the thread is created as a parallel thread in the given pool. Supplying #'own as pool is equivalent to supplying Thread.Pool(1), but also closing the pool with Thread.Pool.close immediately after the new thread is added.
If keep is #false, then the result of thunk is ignored. If keep is #'results, then the results from thunk are saved and returned by Thread.wait on the thread.
A thread: body; ... form is equivalent to Thread(fun (): body; ...). Using a ~pool or ~keep option in thread is the same as passing the corresponding argument to the Thread constructor.
A Thread object satisfies Evt and includes the Evt.sync method.
function | |
Calling Thread.current multiple times within a thread returns objects that are ==, but they may not be ===.
If the thread completes normally—
If the thread does not complete normally, then fail_k is called in tail position, so its results are the results of Thread.wait.
> def th:
~keep: #'results
1 + 2
3
method | ||||
| ||||
| ||||
enumeration | ||||
|
method | ||||
| ||||
| ||||
function | ||||
| ||||
function | ||||
| ||||
function | ||||
The Thread.send method queues a message for th. As long as th is still running, Thread.send immediately returns #void. If th has stopped running and fail is a function, it is called and its returned is returned. If th has stopped running and fail is #false, the result is #false.
The Thread.receive function blocks until the current thread has a message, and then it dequeues and returns that message. The Thread.maybe_receive function either dequeues and returns an immediately available message, or it returns #false immediately if no message is queued. The Thread.receive_evt function returns a synchronizable event that is ready when the current thread has a message; the event’s synchronization result is itself.
The Thread.receive, Thread.maybe_receive, and Thread.receive_evt functions are not methods, because a thread can only receive messages to itself, so Thread.current() is implicit.
> def orig = Thread.current()
> def t:
#false
> Thread.receive()
#'pong
> t.wait()
"thread isn't there"
function | |
|
class | ||
| ||
| ||
method | ||
| ||
value | ||
|
A thread can be added to a pool as long as the pool is still open. After Thread.Pool.close is called on a pool, no new threads can be added to the pool, and the pool’s resources are released back to the system a threads already within the pool terminate.
The Thread.Pool.own value #'own is useful with ~pool in thread.
function | |
property | |
| |
| |
function | |
|