- The solution is to force main to wait until the other two threads are done.
- That function is pthread_join, which takes two arguments;
- the thread ID of the thread to wait for.
- a pointer to a void* variable that will receive the finished thread s return value.
- If you don't care about the thread return value, pass NULL as the second argument.
- In the following code (see Fig. 3), main does not exit until threads are no longer using the argument structures.
Figure 3:
Revised Main Function for thread-create2.c
|
- Make sure that any data you pass to a thread by reference is not deallocated;
- for local variables, which are deallocated when they go out of scope.
- for heap-allocated variables, which you deallocate by calling free.
2006-03-24