PThreads Flashcards
What does this code do?
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; /* mutex lock for buffer /
pthread_cond_t c_cons = PTHREAD_COND_INITIALIZER; / consumer waits on this cond var /
pthread_cond_t c_prod = PTHREAD_COND_INITIALIZER; / producer waits on this cond var */
Initializes the following:
- pthread mutex
- pthread conditional variable c_cons
- pthread conditional variable c_prod
What does this block of code do?
pthread_t tid1, tid2; /* thread identifiers */ int i; if(pthread_create(&tid1, NULL, producer, NULL) != 0) { fprintf(stderr, "Unable to create producer thread\n"); exit(1); } if(pthread_create(&tid2, NULL, consumer, NULL) != 0) { fprintf(stderr, "Unable to create consumer thread\n"); exit(1); }
The code creates 2 threads
one thread is identified as tid1
one thread is identified as tid2
one thread runs the function producer
one thread runs the function consumer
What does this code do?
pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("Parent quiting\n");
The code waits for the created threads to exit
The code actually joins the threads. “Joining” is one way to accomplish synchronization between threads.
What does the consumer code do?
int i; while(1) { pthread_mutex_lock (&m); if (num < 0) { exit(1); } /* underflow */ while (num == 0) { /* block if buffer empty */ pthread_cond_wait (&c_cons, &m); }
/* if executing here, buffer not empty so remove element */ i = buffer[rem]; rem = (rem+1) % BUF_SIZE; num--; pthread_mutex_unlock (&m);
pthread_cond_signal (&c_prod); printf ("Consume value %d\n", i); fflush(stdout); }
- it runs the while loop forever or until the num var is less than 0
- Inside the while loop:
1. Acquires the mutex lock (mutex m)
- Blocks if the buffer is empty by performing a blocking wait for a signal: pthread_cond_wait
- The critical section of the mutex performs the following:
- waits for the signal on c_cons variable
- remove the next global buffer element
- decrement the num variable - Unlock the mutex
- call pthread_cond_signal to send a signal on the c_prod variable that the buffer is not full (waiting in the producer variable)
What does this producer code do?
int i; for (i=1; i<=20; i++) { pthread_mutex_lock (&m); if (num > BUF_SIZE) { exit(1); /* overflow */ } while (num == BUF_SIZE) { /* block if buffer is full */ pthread_cond_wait (&c_prod, &m); } buffer[add] = i; add = (add+1) % BUF_SIZE; num++; pthread_mutex_unlock (&m); pthread_cond_signal (&c_cons); printf ("producer: inserted %d\n", i); fflush (stdout); }
the producer code does the following:
- loops from 1 to 20
- acquires the mutex lock
What critical section code does:
- exits if the num is greater than the BUF_SIZE var
- blocks the buffer is full –> pthread_cond_wait on c_prod var
- adds an element to the global buffer
- increases the num var
-unlocks the mutex
- sends signal to consumer that buffer is not empty
pthread_cond_signal on var c_cons
repeats loop
How are threads terminated?
pthread_exit()
pthread_cancel() (cancelled by another thread)
Calls to exec() or exit() which kills entire process and kills the threads
If main() finishes first
How do you detach a thread?>
pthread_detach()
What is the pthread struct?
pthread_t
What is the struct for the pthread attribute? How do you initialize it? What does it specify?
pthread_attr_t
pthread_attr_t attr;
pthread_attr_init(&attr);
Specifies the following:
- defines the features of a new thread
such as stack size, joinable, priority, inheritance, scheduling policy, system/process scope
Default are joinable threads
Difference between joinable and detached threads:
Joinable:
- parent creates children threads
- children threads rejoin parent
if the parent exits earlier than the children threads, there are zombie threads
Detached:
- parent thread creates children threads
- the children threads do work and then are detached
- this indicates storage for the thread can be reclaimed when that thread terminates
What does the following code do?
#include #include
void *foo (void arg) { / thread main */
printf(“Foobar!\n”);
pthread_exit(NULL);
}
int main (void) {
int i; pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); // Required!!! pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&tid, &attr, foo, NULL); return 0; }
- includes the pthread header to include pthread functionality
- foo function is the routine for each of the threads which prints Foobar
- Inside main:
- thread var is initialized
- attr var is initialized
- pthread_attr_init is called to initialize attributes
- pthread_attr_setdetachstate creates threads as detached
- sets the system scope
- creates the thread with the routine of function foo and with the attributes set
What is the purpose of pthread conditional variables?
- condition variables are synchronization constructs which allow blocked threads to be notified once a specific condition occurs
- Wait, signal, and broadcast that can be performed on condition variables
- PThreads has pthread condition signal and pthread condition broadcast, that we can use to either notify one thread that’s waiting on a condition variable using the signal operation, or to notify all threads that are waiting on a condition variable using the pthread condition broadcast operation.
What does the following do?
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);
Destroys the following:
- pthread attribute
- count_mutex
- conditional variable count_threshold_cv
- exits the pthread_exit