Basics

Why Threads?

fork() is so beautiful

  • New process

  • New address space

  • ⟶ no race conditions

  • ⟶ simple is beautiful!

But …

  • Process creation is expensive

  • Separate address space ⟶ communication is cumbersome

  • Portability: Windows has no idea

Typical Uses

  • Use of multiple processors for compute-intensive calculation

  • One is force to use a library that blocks

    • A no-go in a GUI application for example

    • Push it in a thread, call it there, and communicate with the thread however you feel best

    • Communication ⟶ later

  • Blocking I/O

    • Like the blocking library: push it in a dedicated thread

    • But there are better anti-naive solutions (Unix is not Windows)

Overview

  • Creating threads

  • Synchronisation: Mutex

  • Communication: Condition variable

  • Thread specific data (a.k.a. thread local storage)

  • One-time initialization

POSIX Thread API

  • POSIX thread API is not implemented in the kernel

    • User space library

    • man 3 ...

    • strace is of limited use

  • errno is thread spezific ⟶ “semi-global”

  • No PThread function sets errno

    • They generally return what otherwise would be -errno

    • Thank you!

  • gcc -pthread

    • Defines macro _REENTRANT

    • Links -lpthread

    • C++: thread safe initialization of local static