Skip to content

Threads

Razvan Deaconescu edited this page Apr 14, 2019 · 3 revisions

Threads

Modern computing systems have multiple CPUs/cores. This allows multiple processes to trully run in parallel. Additionally it provides the opportunity for a given action to run in parallel on multiple CPUs. Multithread programs running on multiple cores increase throughput. Even on a single core system, threads allow a simpler programming interface where one thread runs when another thread is blocked waiting for an I/O device.

In this chapter we show use cases of threads and the advantages and disadvantages of using threads and processes. We use the POSIX thread API (pthreads) to demo working with threads. We dicuss caveats of using threads and the requirements (and issues) for using synchronization primitives.

goals:

  • use cases of threads, caveats of using threads
  • using POSIX thread API
  • understanding synchronization concepts and synchronization API

Contents

multi processor systems, multiple actions or one action on multiple cores multiple process vs multiple threads sharing memory, separate stacks, per-thread data (TLS) POSIX threads API, inspecting threads race conditions, thread safety, reentrancy, synchronization

demos:

Trace thread creation vs process creation

Create multiple threads, show they fill all CPUs.

Show that creating a thread alters the process address space.

Create process vs creating threads: time taken for activation

Process vs threads: modify variables

Show modification of variable on stack

Show use of TLS and then of __thread.

Show issue with ctime vs ctime_r.

Show problem when synchronization does not happen.

Run ffmpeg with libx264 and with libvorbis, show core usage.

Tasks

find limitation of number of threads

Alter stack size to create more threads

See difference between using mutexes and spinlocks and atomic variables

Clone this wiki locally