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:
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);
}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:
What critical section code does:
-unlocks the mutex
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:
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; }
What is the purpose of pthread conditional variables?
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: