On this page:
Thread
thread
Thread.Keep
Thread.Keep.results
Thread.wait
Thread.break
Thread.Break
Thread.Break.interrupt
Thread.Break.hang_  up
Thread.Break.terminate
Thread.kill
Thread.handle
Thread.from_  handle
Thread.sleep
Thread.Pool
Thread.Pool.close
Thread.Pool.own
Thread.Pool.processor_  count
Thread.Pool.handle
Thread.Pool.from_  handle
8.18.0.13

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

class Thread():

  constructor (

    thunk :: Function.of_arity(0),

    ~pool: pool :: maybe(Thread.Pool || Any.of(#'own)) = #false,

    ~keep: keep :: maybe(Thread.Keep) = ~false

  )

 

expression

thread:

  option; ...

  body

  ...

 

option

 = 

~pool: pool_body; ...

 | 

~keep: keep_body; ...

 

enumeration

enum Thread.Keep:

  results

The Thread class represents a thread. The Thread constructor accepts a function to call in a newly created thread.

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.

method

method (th :: Thread).wait(

  fail_k :: Prcedure.of_arity(0) = Function.pass

) :: Any

Blocks until the thread th terminates.

If the thread completes normally—without throwing an exception of otherwise aborting to the thunk’s initial promptthen the results are any results saved by the thread (because it was created with ~keep: #'results) or #void if results are not kept.

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:

    thread:

      ~keep: #'results

      1 + 2

> th.wait()

3

method

method (th :: Thread).break(

  kind :: Thread.Break = #'interrupt

) :: Void

 

enumeration

enum Thread.Break:

  interrupt

  hang_up

  terminate

Asynchronously throws an Exn.Break exception in th, assuming that it has not yet terminated. If kind is #'hang_up or #'terminate, then the exception is more specifically Exn.Break.HangUp or Exn.Break.Terminate, respectively.

method

method (th :: Thread).kill() :: Void

Terminates the thread th. If the thread has already terminated, then Thread.kill has no effect.

property

property (th :: Thread).handle

 

function

fun Thread.from_handle(handle) :: Thread

The Thread.handle property accesses a thread object’s underlying Racket representation. The Thread.from_handle function constructs a Rhombus thread object from a Racket thread object.

function

fun Thread.sleep(secs :: NonnegReal) :: Void

Causes the current thread to pause for at least secs seconds.

The Thread.Pool class represents a parallel thread pool. Threads in the same pool share processor resources so that up to n threads run in parallel to each other and other threads. All threads (in the pool and otherwise) run concurrently to each other.

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.

Returns the total number of processors available on the current machine. A thread pool larger than this result is unlikely to offer any benefit over a pool whose size matches the result.

The Thread.Pool.handle property accesses a thread pool object’s underlying Racket representation. The Thread.from_handle function constructs a Rhombus thread pool object from a Racket thread pool object.