Next:
Thread Cancellation
Up:
Thread Basics: Passing Arguments,
Previous:
Thread Basics: Passing Arguments,
Contents
Passing Arguments to Threads
Passing Arguments to Threads
The
function allows the programmer to pass
one argument
to the
thread function
.
For cases where
multiple arguments
must be passed, this limitation is easily overcome by creating a
structure
.
This structure contains all of the arguments, and then a pointer is passed to that structure in the
routine.
All arguments must be passed by reference and cast to (void *).
Threads have non-deterministic start-up and scheduling.
How can you safely pass data to newly created threads?
Example:
Demonstrates how to pass a simple integer to each thread.
Figure 6.5:
Passing single argument to thread function.
Example:
Demonstrates how to pass/setup multiple arguments to thread function via a structure.
Figure 6.6:
Passing multiple arguments to thread function via a structure.
Each thread receives a
unique instance
of the structure.
Next:
Thread Cancellation
Up:
Thread Basics: Passing Arguments,
Previous:
Thread Basics: Passing Arguments,
Contents
Cem Ozdogan 2010-12-27