Modern C++ Threads
Kurs om hur du programmerar flertrådade (multi-threaded) applikationer med Modern C++
Buggar i fler-trådade program anses ofta vara det besvärligaste man kan råka ut för som programmerare. Faktum är att det generella rådet är att inte stoppa in dem från början. Om man tycker det rådet är aningen orealistiskt, så är det näst bästa att programmera enligt ett antal väl beprövade programmerings-idiom som undviker problemen. I denna kurs, fokuserar vi på just den strategin.
Snabbfakta
Namn
Modern C++ ThreadsÄmne
C++ och CURI
cxx/cxx-threads
Längd
3 dagarNivå
AdvancedMålgrupp
Rutinerade C++ programmerareFörkunskaper
Kunna programmera i C++Programvara & Verktyg
Här ser du vilka kursdatum som är tillgängliga. Klicka på en av datumknapparna för att anmäla dig till ett kurstillfälle. På kursen pratar läraren svenska, medan vårt kursmaterial alltid är författat på engelska.
Missa inte vår samfaktureringsrabatt! Är ni fler personer från samma företag/organisation som går på samma kurs, rabatteras tillkommande personer med 25%
. Ni anmäler er till kursen en och en, men uppger samma företag, så ordnar vi resten. Samtliga deltagare från samma företag ingår på samma faktura, den första till fullt pris och resterande till rabatterat pris.
Du sitter bekvämt i ett av våra klassrum, vilka finns centralt placerade i Stockholms innerstad.
I priset ingår tryckt kursmaterial (och som PDF), samt kaffe/te med smörgås på förmiddagen och kaffe/te med bulle på eftermiddagen. Pris: 21 000 kr + moms Du sitter bekvämt framför datorn och deltar i kursen via internet. Vi använder programvaran Zoom för alla våra fjärrkurser. I priset ingår kursmaterial som PDF. Pris: 16 500 kr + moms
Om ni är tre eller fler personer från samma företag eller organisation, kan ni beställa en företagsanpassad kurs. Då håller vi kursen på ett datum som passar er. Antingen på plats i era lokaler eller som en fjärrkurs. Vi kan också hålla den muntliga framställningen på engelska. Klicka på knappen nedan för att be om en offert.
Kursdatum
Kurs i Klassrum
Kurs via Zoom
Kurs hos Er
Här är ett sammandrag i punktform av vad du får lära dig på kursen. Eftersom kursmaterialet är författat på engelska, så återger vi sammandraget också på engelska.Detta får du lära dig
Eftersom kursmaterialet är författat på engelska, så återger vi innehållet också på engelska.Kursinnehåll
Threads
Introduction to Threads and Concurrency
- Concurrent vs. parallel
- Scheduling
- Thread types
- Synchronization
- Virtual adress space organization
- Overview of how a function-call is performed and why it’s relevant to threads
- Where do all the thread lives?
Creating a thread in C++20
- How to create a C++20 thread
- Sample hello thread
- Explicit join
- Obtaining the current thread
- Sleeping
- How to provide the thread body
- Lambda, function, functor, method pointer
std::thread
vs.std::jthread
AutoJoiner
helper class for std::thread- Passing arguments to a thread
- The problem of mixed output and how to fix it
- Moving threads
- Swapping threads
- Thread base class
- Obtain the number of processing unints
- Thread local storage
Cooperative Interruption
- How to stop a thread
- Implicit requesting stop
- Explicit requesting stop
- Register a stop call-back
- Using a stop source/token
Concurrency Problems
The Critical Section Problem
- Understanding the problem and its solution
- The ATM problem
- Account class
- ATM class
- Bank program
- Failed execution
- Critical (code) section
- What is wrong?
- Three conditions for the critical-section problem
- How to prevent the critical-section problem
- Using a mutex lock
- C++ mutex type
- Thread-safe account class
- Using a lock guard
- How to prevent self-deadlock
- Using a recursive lock
- The problem of partial mutable operations
- How to make
getBalance
const, but retainmutex::lock
- Using lock with time-out
The Race-Condition Problem
- Understanding the problem and its solution
- The SWIFT problem
- Moneybox class
- SWIFT class
- Sender thread
- Receiver thread
- Failed execution
- What is wrong?
- Condition synchronization
- C++ condition variables
- Thread-safe monyebox
- The concept monitor
- Monitor semantics
The Deadlock Problem
- The concept resource
- The Kitchen problem
- Resource class
- Cook thread
- Failed execution
- The Dining Philosophers problem
- Ed Coffmans conditions for deadlock
- How to handle deadlocks
- Fixing the Kitchen problem
- The Account Transfer problem
- C++ scoped locks
- Implementation of the Account Transfer problem
More Synchronization
Read-Write Locks
- What is a read-write lock?
- Using a
std::shared_mutex
- Sample demo program
Latches & Barriers
- What is latch?
- Latch operations
- Usage of a latch
- What is barrier?
- Barrier operations
- Usage of a barrier
Semaphores
- What is a semaphore?
- Counting semaphore
- C++ semaphores
- C++ binary semaphore
- Using a semaphore as a mutex
- Implementing a queue with semaphores
- Problems with semaphores
- Semaphore can be more versatile than locks
- POSIX has two types of semaphores
Promises & Futures
- What are promises and futures?
- C++ promises and futures
- Sample usage
- Dealing with exceptions
- Asynchronous tasks
- Usage os
std::async
- Execution policies
- Using s shared future
- Example; scanning the file system using async tasks
Message Queues
Thread-Safe Queue
- Logical design of a queue
- Handling queue capacity
- Implementation advice for an unbounded queue
- Class
MessageQueueUnbounded
- Sample execution
- Implementation advice for an bounded queue
- Class
MessageQueueBounded
- Sample execution using a bounded queue
- Dropping messages when queue full
Unidirectional Message Passing
- What is message-passing
- Handling pointer based messages
- Implementing interface Receivable
- Implementing a threads pipeline
Bidirectional Message Passing
- What is bidirectional message-passing?
- What is rendezvous?
- Communication patterns
- Implementation of rendezvous
- Rendezvous mesage class
- RPC server
- Sample client
- Execution
- Some optimizations
- Inspecting thread stack frames
Thread Pools
- What is a thread pool?
- The simplest thread pool implementation
- Tasks that return a result
- Task type
- The submit function
- The worker loop
- Pool creation and shutdown
- Sample test program
Misc. Library Parts
Parallel STL Algorithms
- What is a parallel STL algorithm
- Parallelized algorithms
- Execution policies
- Sequenced policy
- Parallel policy
- Parallel unsequenced policy
- Unsequenced policy
- Chosing a policy
- Changes to iterator requirements
- Changes in exception behaviour
- Replaced algorithms
- Do we get any speed-ups? For which work loads?
- The benchmark program
Atomic Variables
- What is an atomic type/variable?
- Using
std::atomic<T>
- Operations
- The ATM problem using an atomic int
Under the Hood
User-Defined Memory Allocation
- Limitations of using system heap for messages
- Allocation functions of the system heap
- How the system heap is implemented
- Why the heap is a large critical section
- Heap arenas in GNU glibc; multiple locks
- What is a private heap?
- Choosing storage for messages
- Possible implementation of a consumer's private heap
- Allocation strategies
- Allocation-only storage
- Monotonic allocation
- Circular allocation
- Block-pool storage
- What is
std::pmr
in C++17? - Classes and functions in
std::pmr
- What is a memory resource?
- Special allocators
- Global allocators
-
Using a simple
std::monotonic_buffer_resource
- Container support for std::pmr
- Usage of a std::pmt::vector
- Using an
std::pmr::unsynchronized_pool_resource
- Understanding consumer stack space
- A message thread with a private std::pmr heap
POSIX Threads
- Rudimentary C API
- Creating a thread with
pthread_create
- Passing arguments
- Thread configuration
- Setting the stack size
- PThread mutex
- The ATM problem as a C program, with pthreads
- PThread Condition
- PThread Read-Write locks
- PThread Barriers
Implementation of C++ Threads
- Wrapping pthreads
- Simple
thread
class - Usage of the class
- Pasing arguments
- Class
ThreadArgs
- Usage of the modified class
- Class Mutex
- Usage of the mutex class
- Class Condition
- Usage of the condition class
Shared Memory
POSIX Processes
- What is a process?
- Understanding the system call
fork
- Using
exit
andwait
- Understanding the system call
exec
- Loading new program code
POSIX Shared Memory
- What is shared memroy, really?
- How is it working
- Shared memory C API
- Using
mmap
to create memory segments - Class
SharedMemory
- Usage of the class
-
Understanding the placement variant of operator
new
- How to configure POSIX mutex and condition to work in shared memory
- Sample program; Producer-Consumer via shared memory