thread.h

This interface exports a platform-independent thread abstraction, along with simple functions for concurrency control.
Types
Thread The Thread type is used to represent a thread, which is a lightweight process running in the same address space as the creator.
Lock The Lock type is used to manage concurrent access to some data structure within an application.
Functions
forkThread(fn, data) Forks a new thread to invokes the specified function, passing data as an argument.
joinThread(thread) Waits for the specified thread to finish before proceeding.
yield() Yields the processor to allow another thread to run.
getCurrentThread() Returns the currently executing thread.
setThreadName(thread, name) Sets the name of a thread to the given string.
getThreadName(thread) Returns the name of the specified thread.
newLock() Creates a new Lock object.
waitThread(lock) Waits for some other thread to issue a call to signalThread on this lock.
signalThread(lock) Signals all threads waiting on the lock so that they wake up and recheck the corresponding condition.

Type detail


typedef struct ThreadCDT *Thread;
The Thread type is used to represent a thread, which is a lightweight process running in the same address space as the creator.

typedef struct LockCDT *Lock;
The Lock type is used to manage concurrent access to some data structure within an application. Only one thread can hold a lock at any point in time; other threads seeking to gain access queue on the lock until it is released by the thread that originally obtained it. The general strategy for using a lock is to use the synchronized statement to protect a critical region of code, as illustrated in the discussion of synchronized later in this file.

Function detail


Thread forkThread(proc fn, void *data);
Forks a new thread to invokes the specified function, passing data as an argument. Threads created by forkThread become dormant on completion and wait for the client to synchronize with them using a joinThread operation.

Usage:

thread = forkThread(fn, data);

void joinThread(Thread thread);
Waits for the specified thread to finish before proceeding.

Usage:

joinThread(thread);

void yield(void);
Yields the processor to allow another thread to run.

Usage:

yield();

Thread getCurrentThread(void);
Returns the currently executing thread.

Usage:

self = getCurrentThread();

void setThreadName(Thread thread, string name);
Sets the name of a thread to the given string. This name is used primarily for debugging purposes.

Usage:

setThreadName(thread, name);

string getThreadName(Thread thread);
Returns the name of the specified thread. If no name has been set for the thread, this function returns a string in the form Thread<xxx>, where xxx is an integer uniquely identifying the thread.

Usage:

name = getThreadName(thread);

Lock newLock(void);
Creates a new Lock object.

Usage:

lock = newLock();

void waitThread(Lock lock);
Waits for some other thread to issue a call to signalThread on this lock. This call requires that the lock be owned by the calling thread. The effect of this function is to release the lock and then wait until the desired signalThread operation occurs, at which point the lock is reacquired and control passes to the statement following the waitThread.

The waitThread function is useful only if the call is embedded inside a while loop that checks a condition before proceeding. That while statement must itself be embedded inside a synchronized statement that acquires the lock. Thus, the standard paradigm for using the waitThread function looks like this:

   synchronized (lock) {
       while (conditional test) {
           waitThread(lock);
       }
       . . . code to manipulate the locked resource . . .
   }

Usage:

waitThread(lock);

void signalThread(Lock lock);
Signals all threads waiting on the lock so that they wake up and recheck the corresponding condition.

Usage:

signalThread(lock);