Lecture 06 - Threads Flashcards
What does each thread get its own copy of?
Automatic variables (function variables)
What data do threads share?
- Open file handles and sockets
- Global variables
- Memory allocated with malloc
Give four reasons threads are useful
- Increased responsiveness
- Ease of communication between threads
- Scalability
- Handling asynchronous events
How do threads increase the responsiveness of a program?
There can be separate threads for IO while others do computation.
What library implements POSIX threads?
pthreads
What is the function used to create a new thread in C?
pthread_create()
What is the function prototype of pthread_create?
int pthread_create( pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void*), void *restrict arg);
What are two ways for a thread to exit?
- It returns from its start routine
- It calls pthread_exit
What is the function prototype for pthread_exit?
void pthread_exit(void *value_ptr);
What happens to threads when a process terminates?
They also terminate.
What function is used to get the current thread’s ID?
pthread_self
What is the function prototype for pthread_self?
pthread_t pthread_self(void);
What makes a function thread-safe?
When the correct result is produced in the face of simultaneous execution.
What gcc parameter is used to link the pthread library?
-pthread
What function is used to wait for a thread to terminate?
pthread_join