Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

POSIX Threads (`pthread` | C Programming Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 41 of 50

·c

POSIX Threads (`pthread`) – Advanced

18 mins

Course progress82%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • `pthread_create` needs a `void *(*)(void *)` function pointer.
  • Always unlock a mutex, even on error paths – consider `pthread_cleanup_push` or `pthread_mutex_unlock` in a `finally`‑like block.
  • `pthread_cond_wait` atomically releases the mutex and waits.
  • Detach a thread (`pthread_detach`) when you don’t need to `join` it.

On Unix‑like systems the POSIX thread library (pthread) provides a richer API.

Key components:

  • Thread creation – pthread_create(&tid, NULL, func, arg);
  • Joining – pthread_join(tid, NULL);
  • Mutexes – pthread_mutex_t, init with pthread_mutex_init.
  • Condition variables – pthread_cond_t for signalling.
  • Read‑write locks – pthread_rwlock_t.
  • Thread attributes – stack size, detach state.

📚 Example: Producer‑Consumer with Condition Variable

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#define BUF_SIZE 5
int buffer[BUF_SIZE];
int count = 0; // items in buffer
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t not_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t not_empty = PTHREAD_COND_INITIALIZER;

void *producer(void *arg) {
    for (int i = 0; i < 20; ++i) {
        pthread_mutex_lock(&mtx);
        while (count == BUF_SIZE) pthread_cond_wait(&not_full, &mtx);
        buffer[count++] = i;
        printf("produced %d (count=%d)
", i, count);
        pthread_cond_signal(&not_empty);
        pthread_mutex_unlock(&mtx);
        usleep(100000);
    }
    return NULL;
}

void *consumer(void *arg) {
    for (int i = 0; i < 20; ++i) {
        pthread_mutex_lock(&mtx);
        while (count == 0) pthread_cond_wait(&not_empty, &mtx);
        int val = buffer[--count];
        printf("consumed %d (count=%d)
", val, count);
        pthread_cond_signal(&not_full);
        pthread_mutex_unlock(&mtx);
        usleep(150000);
    }
    return NULL;
}

int main(void) {
    pthread_t prod, cons;
    pthread_create(&prod, NULL, producer, NULL);
    pthread_create(&cons, NULL, consumer, NULL);
    pthread_join(prod, NULL);
    pthread_join(cons, NULL);
    return 0;
}
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from C Programming FundamentalsBrowse activities
Previous lessonMultithreading with `<threads.h>` (C11)Continue to lesson 42Signal Handling (`signal.h`)Back to C Programming Fundamentals curriculum