thread.h
Types | |
The Thread type is used to represent a thread, which is a lightweight process running in the same address space as the creator. | |
The Lock type is used to manage concurrent access to some data structure within an application. | |
Functions | |
Forks a new thread to invokes the specified function, passing data as an argument. | |
Waits for the specified thread to finish before proceeding. | |
Yields the processor to allow another thread to run. | |
Returns the currently executing thread. | |
Sets the name of a thread to the given string. | |
Returns the name of the specified thread. | |
Creates a new Lock object. | |
Waits for some other thread to issue a call to signalThread on this lock. | |
Signals all threads waiting on the lock so that they wake up and recheck the corresponding condition. |
typedef struct ThreadCDT *Thread;
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;
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.
Thread forkThread(proc fn, void *data);
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);
Usage:
joinThread(thread);
void yield(void);
Usage:
yield();
Thread getCurrentThread(void);
Usage:
self = getCurrentThread();
void setThreadName(Thread thread, string name);
Usage:
setThreadName(thread, name);
string getThreadName(Thread thread);
Thread<xxx>
, where xxx
is an
integer uniquely identifying the thread.
Usage:
name = getThreadName(thread);
Lock newLock(void);
Lock
object.
Usage:
lock = newLock();
void waitThread(Lock lock);
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);
Usage:
signalThread(lock);