Chapter 4 - Pthreads Flashcards
What is a shared memory, from the programmers point of view?
All cores can access all the memory locations.
What is a critical section?
The code that updates a shared resource, that can only be updated by one thread at a time.
What are threads?
An instance of a program running on a processor. (In MPI this is called a process)
Draw a shared-memory system.
Why is a thread a “lighter-weight” process?
It contains just an instance of the running program, not the additional components (memory, descriptors, etc.).
They do have their own stack and local variables
What does a process consist of?
A process is an instance of a running program.
Stack- and heap memory
Descriptors of resources allocated for process (stdin, stdout)
Security info
State info (ready, waiting, register contents, …)
What are POSIX threads?
Standard for unix-like OS’es
It specifies an API for multithreaded programming.
How are pthread files compiled?
Like normal C files
Might need to link pthread library
gcc -o hello hello.c -lpthread
What is the pthread header file?
include <pthread.h></pthread.h>
What are global variables in thread-programs?
Variables that are shared between all threads.
How are threads started in pthread programs?
Threads are started by the program executable - meaning code needs tostart the threads itself.
What is the datastructure of threads?
pthread_t
What function is used to start threads?
pthread_create(
pthread_t* thread,
const pthread_attr_r* attr,
void* (start_routine)(void),
void* args_p
)
attr: Can pass NULL
start_routine: The function to be run by the thread
args_p: arguments to pass to the function being run, thread rank is often passed here.
How should functions being run by threads be declared?
void* thread_function(void* args_p)
Give an example of how threads can be created and how their rank is passed to them
int rank=0;
pthread_t thread = malloc(sizeof pthread_t);
pthread_create(&thread, NULL, &func, (void*) rank)
What kind of parallelism is most often used with pthreads?
SIMD, but there is no technical reason for threads only to run the same programs. It just is most often done.
Threads still do thread-branching within a thread.
What is a main thread?
Thread that runs the main-function
Does threads need to return something?
They do not need to return something useful, but because pthread_create have the return type(void*), the thread should return something. This can be NULL
How is threads stopped?
pthread_join(
thread_t* t,
void** ret_val
)
When join is called by main thread, the program will wait until the corresponding thread has completed.
Join also frees the resources associated with the thread.
ret_val: Used to retrieve return value
What are zombie threads?
Threads that are never joined, and their resources never freed. These wastes resources
What does pthread_detach do?
Used when a program does not need to wait for a thread to finish.
The function call indicates to the htread that its resources should be freed upon termination.
How can error checking be done using pthreads?
The pthread_create function have several return values that indicate different errors
What is a thread pool?
The main threads start all threads it think it will be needing, at the beginning of the program.
The threads sit idle when they finish their work, instead of terminating. When a new requests comes, an idle thread can respond to it.
This reduces thread-creation overhead
What is a race condition?
When multiple threads access the same shared memory, and at least one thread execute an update, the result may be unpredictable and we can get errors.
How can we ensure that a critical section is only entered by one thread at the time?
Flag + busy waiting: Can e.g. keep value of rank that is allowed in critical section
mutexes
semaphores
What is busy-waiting?
A thread repeatedly tests a condition, but does not do any useful work until the condition has been met.
How can busy-waiting be coded?
while(flag != my_rank);
No loop body
Is there a situation where using while loops to busy work might not work?
If a compiler optimizes the code and changes the order of instructions in a way that the critical section is executed before the while-loop checks the condition.
If while loops are used to busy wait, compiler optimizations should be turned off. The compiler does not know about multiple threads being used.
What is a spinlock?
When a thread is spinning on the while-loop condition, eating up CPU resources.
This can happen if the thread that is currently accessing the critical section gets preempted by the CPU and thereby delayed.
Why is busy-waiting not optimal for performance?
Can get spinlocks
Turning off compiler optimizations can hurt performance alot.
Howcan serial execution be faster than using multiple threads and busy-waiting?
Thread creation/joining has overhead. This is not the main factor though.
When using busy waiting, all the threads are alternating between execution and waiting. This can increase the run time alot.
Why does critical sections reduce performance?
Because execution must effectively be serialized in critical sections.
To increase performance, we should try to minimize the number of times a critical section is entered.
What is a mutex?
Mutual exclusion
Guarantees exclusive access to critical section.
A data type consisting of a variable and a couple functions used to achieve this.
pthread_mutex_t
How is a mutex created?
int pthread_mutex_init(
pthread_mutex_t* m,
const pthread_mutexattr_t* attr
)
Attr: Can be NULL
What is a static mutex initialization?
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
easier initialization method, fine in many cases