-
Notifications
You must be signed in to change notification settings - Fork 1
Threads
Modern computing systems have multiple CPUs/cores. This allows multiple processes to truly 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 discuss caveats of using threads and the requirements (and issues) for using synchronization primitives.
You will know what are uses cases of using threads, when does it make sense and when it doesn't make sense to use threads; you will know caveats and common issues in using threads. You will become accustomed to the POSIX thread API (pthreads
), commonly used on flavours of UNIX. You will get a better understanding of synchronization concepts and you will improve your skills in using synchronization mechanisms.
multiprocessor 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 CPU cores usage.
Find limitation of number of threads.
Alter stack size to create more threads.
See difference between using mutexes and spinlocks and atomic variables.