Introduction to Programming Threads
Basic Thread Routines
Creation
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
- Analogous to a combined fork and exec routine
- Returns a thread id in thread .
- When attr is NULL the default thread attributes are used.
Termination
void pthread_exit(void * return_value);
- Analogous to exit
- The exit routine kills all threads and exits the process
- If the currecnt thread is the last thread then the process terminates
- Returning from the start_routine is equivalent to calling
pthread_exit
- Returning from the inital thread main is the equivalent to
calling exit
Detach and Join
int pthread_detach(pthread_t thread);
int pthread_join(pthread_t thread, void ** status);
- Analogous to wait
- Must specify thread. There is no wait any.
- Current thread blocks until thread terminates
- The return value of thread is returned in status
- All threads must be detached or joined with.
Self and Equal
pthread_t pthread_self(void);
int pthread_equal(pthread_1 t1, pthread_t t2);
Prepared by
Chris Provenzano (proven@mit.edu)