Next: Passing Data to Threads
Up: Threads
Previous: Threads
Contents
- Each thread in a process is identified by a thread ID, type pthread_t.
- Upon creation, each thread executes a thread function. This is just an ordinary function and contains the code that the thread should run. When the function returns, the thread exits.
- The pthread_create function creates a new thread. You provide it with the following:
- A pointer to a pthread_t variable, in which the thread ID of the new thread is stored.
- A pointer to a thread attribute object. This object controls details of how the thread interacts with the rest of the program. If you pass NULL as the thread attribute, a thread will be created with the default thread attributes.
- A pointer to the thread function. This is an ordinary function pointer, of this type:
void* (*) (void*)
- A thread argument value of type void*. Whatever you pass is simply passed as the argument to the thread function when the thread begins executing.
- A call to pthread_create returns immediately, and the original thread continues executing the instructions following the call. Meanwhile, the new thread begins executing the thread function. Linux schedules both threads asynchronously. http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/thread-create.cthread-create.c (see Fig. 5.1)
Figure 5.1:
Create a Thread
|
- Under normal circumstances, a thread exits in one of two ways;
- by returning from the thread function. The return value from the thread function is taken to be the return value of the thread.
- a thread can exit explicitly by calling pthread_exit.
Subsections
Next: Passing Data to Threads
Up: Threads
Previous: Threads
Contents
Cem Ozdogan
2007-05-16