Class Thread
Object
Represents a thread of execution within a program. Each program begins with a single thread, and may
create more. By default, any living threads prevent a program from exiting, even if its main()
method ends. The optional preventsExit
argument to run()
changes this behavior.
As Frost has neither global nor class-level mutable variables, threads do not normally have any access to each other's mutable data. Eliminating memory sharing means that there is generally no need for mutexes, semaphores, or other common thread synchronization constructs; mutable objects are only accessible from a single thread each and there is therefore no contention.
Threads communicate with each other using MessageQueue
objects, which only permit Immutable
objects to be sent. This ensures that only one thread can access a given mutable object at a time.
In situations where it is actually necessary to have multiple threads access potentially-shared
mutable objects, the UnsafeMessageQueue
class allows for passing mutable objects between threads.
If the object (including any other mutable objects it directly or indirectly points to) is truly
'handed off' and no longer accessed by the sending thread, no danger exists. If the sending thread
continues to access a shared object, Lock
must be used to synchronize access.
- Source Code:
- View Source
Class Method Summary
start (run :
):()=&>*() Thread - Creates and starts a new thread which executes the specified method.
start (run :
,()=&>*() preventsExit :
):Bit Thread unsafeStart (run :
):()=&>() Thread unsafeStart (run :
,()=&>() preventsExit :
):Bit Thread preferredThreadCount ():Int - Returns the number of threads that generally leads to best performance on the current system.
sleep (seconds :
)Real64 - Pauses execution of the current thread for a length of time.
Instance Method Summary
waitFor ()- Waits for this thread to exit.
Class Methods
@class
method start
(run :()=&>*()
):Thread
Creates and starts a new thread which executes the specified method. For example, to create a new thread which continually displays a message to the console:
Thread.start(method() {
loop {
Console.printLine("Hi, I'm a thread!")
}
})
- Parameters:
-
-run the run method to execute
- Returns:
- the newly created thread
- Parameters:
-
- value of typerun ()=&>*()
- value of typepreventsExit Bit
@class
method unsafeStart
(run :()=&>()
):Thread
- Parameters:
-
- value of typerun ()=&>()
- Parameters:
-
- value of typerun ()=&>()
- value of typepreventsExit Bit
@class
function preferredThreadCount
():Int
Returns the number of threads that generally leads to best performance on the current system. This will generally be (but is not necessarily) the number of available processor cores.
IMPLEMENTATION NOTE: this is not currently implemented and is just hardcoded to return 8.
- Returns:
- the preferred number of executing threads
@class
method sleep
(seconds :Real64
)
Pauses execution of the current thread for a length of time.
- Parameters:
-
- value of typeseconds Real64
Instance Methods
method waitFor
()
Waits for this thread to exit.