Tut 05
Tut 05
LI Jianqiang
[email protected]
Oct 3, 2024
Reminder
Assignment 1
Due at 18:00:00 p.m., Mon, Oct 7th
Assignment 2
Will release after tutorial, Oct 3th
Due at 18:00:00 p.m., Mon, Nov 4th
2
Overview
3
Review: Pthread Library part 1
The Pthread (POSIX threads) library is a powerful tool for creating and
managing threads in C. It provides a standardized API to facilitate multi-
threading, which is essential for developing concurrent applications.
Key Interfaces:
pthread_create pthread_mutex_init
pthread_exit pthread_mutex_lock
pthread_join pthread_mutex_unlo
ck
pthread_mutex_dest
roy
4
Pthread Library part 2
The Pthread (POSIX threads) library is a powerful tool for creating and
managing threads in C. It provides a standardized API to facilitate multi-
threading, which is essential for developing concurrent applications.
Key Interfaces:
pthread_create pthread_mutex_init pthread_cond_init
pthread_exit pthread_mutex_lock pthread_cond_wait
pthread_join pthread_mutex_unlo pthread_cond_signal
ck pthread_cond_broad
pthread_mutex_dest cast
roy pthread_cond_destro
y 5
1. pthread_cond_init
Example:
pthread_cond_t cond;
pthread_cond_init(&cond, NULL);
6
2. pthread_cond_wait
Parameters:
• cond: pointer to the condition variable.
• mutex: pointer to an associated mutex (must be locked before calling).
Example:
pthread_mutex_lock(&mutex);
while (condition_is_false) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
7
2. pthread_cond_wait
8
3. pthread_cond_signal
Parameters:
• cond: pointer to the condition variable.
Example:
pthread_mutex_lock(&mutex);
// Change the condition
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
9
4. pthread_cond_broadcast
Parameters:
• cond: pointer to the condition variable.
Example:
pthread_mutex_lock(&mutex);
// Change the condition
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
1
5. pthread_cond_destroy
Parameters:
• cond: pointer to the condition variable.
Example:
pthread_cond_destroy(&cond);
1
Example 1: Multiple Producers and Consumers
1
Example 1: Multiple Producers and Consumers
Requirements:
void* consumer(void* arg) { consumer wait when
• Producers wait if the buffer is full
while (1) {
empty
• Consumers wait if it's empty pthread_mutex_lock(&mutex);
#include <pthread.h> example while (count == 0) {
#include <stdio.h> pthread_cond_wait(&cond_consume, &mutex);
#include <stdlib.h>
1.c }
#define BUFFER_SIZE 10 int item = buffer[--count]; // Consume item
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; printf("Consumed, count: %d\n", count);
pthread_cond_t cond_produce = PTHREAD_COND_INITIALIZ pthread_cond_signal(&cond_produce);
ER; pthread_mutex_unlock(&mutex);
pthread_cond_t cond_consume = PTHREAD_COND_INITIALIZ } signal producer when not
ER; return NULL;
producer wait when full
int buffer[BUFFER_SIZE]; }
int count = 0; full int main() {
void* producer(void* arg) { pthread_t producers[2], consumers[2];
while (1) { for (int i = 0; i < 2; i++) {
pthread_mutex_lock(&mutex); pthread_create(&producers[i], NULL, producer, N
while (count == BUFFER_SIZE) { ULL);
pthread_cond_wait(&cond_produce, &mutex) pthread_create(&consumers[i], NULL, consumer, N
; ULL);
} }
buffer[count+ for (int i = 0; i < 2; i++) {
+] = rand() % 100; // Produce item
signal consumer when not pthread_join(producers[i], NULL);
printf("Produced, count: %d\n", count); pthread_join(consumers[i], NULL);
empty
pthread_cond_signal(&cond_consume); } 1
pthread_mutex_unlock(&mutex); return 0;
Example 2: Barrier Synchronization
1
Example 2: Barrier Synchronization
Requirements:
1
Q&A