Next: Joining Threads
Up: Thread Creation
Previous: Thread Creation
Contents
- Use the thread argument to pass a pointer to some structure or array of data.
- Define a structure for each thread function, which contains the parameters that the thread function expects.
- Using the thread argument, it is easy to reuse the same thread function for many threads. All these threads execute the same code, but on different data.
- In the following program (see Fig. 5.2), the same thread function, char_print, is used by both threads, but each is configured differently using struct char_print_parms. http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/thread-create2.cthread-create2.c
Figure 5.2:
Create Two Threads
|
- A serious bug in it. The main thread creates the thread parameter structures as local variables, and then passes pointers.
- if main finishes executing before either of the other two threads are done, the memory containing the thread parameter structures will be deallocated while the other two threads are still accessing it.
Next: Joining Threads
Up: Thread Creation
Previous: Thread Creation
Contents
Cem Ozdogan
2007-05-16