Threads Module User's Guide : PART II Concurrency Packages : Chapter 3 The Threading Package : Thread Attributes : Initializing Threaded Runnables
Initializing Threaded Runnables
Every threaded runnable instance contains a thread attribute instance that defines the thread attributes to use when creating a new thread during a call to start(). The Threading package allows you to change or query the thread attribute instances associated with any threaded runnable.
Supplying RWThreadAttribute Instances To Threaded Runnables
RWThreadAttribute instances can be supplied to threaded runnable objects at one of two times:
When the runnable objects are constructed (see “Supplying RWThreadAttribute Instances At Construction”).
After construction, using accessor functions (see “Supplying RWThreadAttribute Instances After Construction”).
RWThreadAttribute objects can be supplied to and shared between any number of threaded runnable objects. Changes made to the values of a shared thread attribute instance can be seen by all runnables that have been given a handle to that attribute instance.
The thread attribute instance referenced by each threaded runnable is only used or evaluated at the time the thread is created. A threaded runnable makes a local copy of its current thread attribute instance each time start() is called. This prevents the attribute values that were present when a thread was created from being lost as a result of future changes in a runnable’s thread attribute instance or the attribute’s values.
Changing a runnable’s thread attribute instance or values of that attribute instance does not affect the active thread, if any, possessed by the runnable object. Attribute changes can only affect the next thread created as result of a call to start().
Supplying RWThreadAttribute Instances At Construction
RWThreadAttribute instances can be supplied to threaded runnable objects if the runnables are constructed using the appropriate static make() functions. If a thread attribute instance is not specified when a threaded runnable is constructed, then that runnable is initialized with its own RWThreadAttribute instance initialized with default values.
The following list identifies the make() functions for each threaded runnable class that accepts an RWThreadAttribute instance:
RWRunnableServer
static RWRunnableServer make(const RWThreadAttribute&)— Constructs an RWRunnableServer instance initialized with the specified thread attribute instance.
RWServerPool
static RWServerPool make(const RWThreadAttribute&,size_t) — Constructs an RWServerPool instance whose main thread is created using the specified thread attribute instance and whose pool threads are created using default thread attributes.
static RWServerPool make(size_t,const RWThreadAttribute&)— Constructs an RWServerPool instance whose main thread is created with default thread attributes and whose pool threads are created using the specified thread attribute instance.
static RWServerPool make(const RWThreadAttribute&
serverThreadAttr, size_t numThreads,
const RWThreadAttribute& poolThreadsAttr,
size_t maxCapacity);
Constructs an RWServerPool instance whose main thread is created using the first thread attribute instance and whose pool threads are created using the second thread attribute instance.
RWThreadFunction
static RWThreadFunction make(RWTFunctor<void()>&, const RWThreadAttribute&)— Constructs an RWThreadFunction instance initialized with the specified thread attribute instance.
static RWThreadFunction make(const RWThreadAttribute&)— Constructs an RWThreadFunction instance initialized with the specified thread attribute instance.
RWTThreadIOUFunction
static RWTThreadIOUFunction<R> make(const RWTFunctor<R()>&, const RWThreadAttribute&)— Constructs an RWTThreadIOUFunction instance initialized with the specified thread attribute instance.
static RWTThreadIOUFunction<R> make (const RWTIOUEscrow <R>&, const RWTFunctor<R()>&, const RWThreadAttribute&)— Constructs an RWTThreadIOUFunction instance initialized with the specified thread attribute instance.
Supplying RWThreadAttribute Instances After Construction
To set the thread attributes for threaded runnables already constructed, you must use the following accessor functions:
RWThread and RWThreadSelf
void setAttribute(const RWThreadAttribute&)— Replaces the threaded runnable's current thread attribute object with another. Changing a threaded runnable's attribute object after the runnable has already been started does not affect its current thread—it can only affect the thread created the next time the runnable is started.
RWServerPool
void setPoolAttribute(const RWThreadAttribute&)— Changes the thread attribute instance used to initialize new runnable server objects as they are created in the thread pool. Changing the pool thread attribute after the server has started does not affect new threads started by the pool server until the server pool instance is restarted.