The SimpleCxxLib package


#include "thread.h"

This file exports a simple, platform-independent thread abstraction, along with simple tools for concurrency control.
Classes
Lock This class represents a simple lock used to control concurrency.
Thread This class encapsulates a lightweight process running in the same address space as the creator.
Statement
synchronized(lock) Defines a critical section protected by the specified lock.
Functions
fork(fn)
fork(fn, data) 
Creates a child thread that calls fn in an address space shared with the current thread.
getCurrentThread() Returns the currently executing thread.
join(thread) Waits for the specified thread to finish before proceeding.
yield() Yields the processor to allow another thread to run.

Statement detail


synchronized (lock) ...
Defines a critical section protected by the specified lock. The general strategy for using this facility is shown in the following paradigmatic pattern:

   synchronized (lock) {
      ... statements in the critical section ...
   }

Function detail


Thread fork(void (*fn)());
Thread fork(void (*fn)(ClientType & data), ClientType & data);
Creates a child thread that calls fn in an address space shared with the current thread. The second form makes it possible to pass an argument to fn, which may be of any type.

Usage:

Thread child = fork(fn);
Thread child = fork(fn, data);

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

Usage:

join(thread);

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

Usage:

yield();

Thread getCurrentThread();
Returns the currently executing thread.

Usage:

Thread self = getCurrentThread();