
Advance to master multithreaded design patterns in C/C++ using posix threads. Explore thread management, pools, barriers, monitors, and producer–consumer synchronization with practical, advanced examples.
Begin by modeling each execution unit as a threat, track its status and resources, and design a thread library with APIs to create, cancel, join, and manage threads and synchronization.
Define a thread data structure and prototype three APIs for a multithreaded library, then implement create, run, and a general or dance mode toggle to initialize and start threads.
Learn how to pause and resume pthreads using mutex and condition variables, including cancellation points and building custom apis, while avoiding invariance and resource leaks.
Examine four APIs for pausing and resuming threads, including set a flag, pause test, resume, and wake-up callback, with careful post-point usage.
Enhance the thread data structure with a TradeSports function pointer, its argument, a 32-bit flags field, a mutex, and a condition variable to enable pause and resume in pthreads.
Implement four thread APIs for setting the pause function, pausing, resuming, and testing unpause—using mutex locks, state flags, and a condition variable to control pausing and resuming threads.
Take a learning break to explore the globalist linked list as a new approach, using it as a supporting data structure for future assignments with a working implementation.
Explore how thread pools park ready threads on a condition variable, assign work without creating new threads, and wake workers via signaling for efficient task execution.
Learn how a thread pool boosts performance by reusing threads for packet processing, reducing expensive system calls, limiting thread counts, and reusing connections in network applications.
Design a thread pool where the application initializes threads, fetches work from the pool (a function plus argument), executes the work, and recycles threads with optional completion feedback.
Explore thread pool data structures and apis for implementing multithreading design patterns. Build a linked list based thread pool with mutex protection and management apis for adding or removing threads.
Begin implementing a pthreads thread pool by initializing the pool, inserting and fetching threads with a mutex-protected linked list, and starting the dispatch logic while integrating the dual thread library.
Learn to implement a pthreads thread pool API: enqueue threads into the pool, block them on a condition variable, then dispatch by creating the execution unit or signaling the thread.
Learn to implement a thread pool, using a thread execution data object that stores function and data for stage two, and guide threads through stage three with helper APIs.
Assign a function to a thread to execute states two and three in a thread pool, then trigger it with the thread run interface and reuse the thread for tasks.
Demonstrates a thread pool by creating and initializing two threads, dispatching functions that print even and odd numbers in reverse and normal order, with sleeps.
Implement step seven as an optional notification from the worker thread to the application. A boolean flag decides whether the application blocks; use zero semaphores, with condition variables as an alternative.
Implement caller thread blocking with a semaphore in step seven, letting the application block via a block color flag and resume when the worker thread signals, enforcing sequential thread-pool execution.
introduce thread barrier, a barrier synchronization data structure that blocks threads until a specified number arrive, enabling completion of parallel tasks before proceeding.
Understand barrier synchronization in multithreaded programs: a barrier threshold blocks threads at a barrier point until the required number arrives, then releases all and transitions through disposition until completion.
Understand the thread barrier data structure, its threshold count, current weight count, a mutex and a condition variable to block threads, and how the disposition flags govern blocking and signaling.
Define and implement the thread barrier API, initializing its data with a threshold and a zero current count. Initialize condition variables and set the boolean flag to true by default.
learn to implement the barrier wait api for threads using mutexes and condition variables, handling last-thread signaling, blocking, and disposition states.
Explore how a thread barrier coordinates three threads, synchronizing at multiple barrier points and verifying barrier state before program termination.
Introduce wait queues as a thread synchronization data structure that blocks threads until a routing table entry is ready, simplifying multithreaded design compared to mutexes and condition variables.
Learn how to use wait queues in multithreaded application code to synchronize threads with condition functions, illustrated by routing table readiness and disk access, aligning with producer-consumer patterns.
Explore implementing a condition function for a wait queue, detailing lock and unlock modes, mutex handling, and application-specific data, with a disk readiness example.
Define and implement the wq data structure for pthreads, including thread count, a condition variable, and a mutex pointer, plus APIs and a condition function pointer for controlled disk access.
Demonstrate the WQ test and wait API implementation, showing how a condition function and a disk mutex block threads until the disk is ready and wake threads on signal.
Compare the test_and_wait API with the standard two-state locking procedure, mapping lock mutex, predicate check, block on the condition variable, wake, recheck, enter the critical section, and unlock.
Implement and initialize wait queues in a pthreads-based multithreading design patterns course, wiring up data structures, APIs, and three-step signaling with mutexes and condition variables.
Demonstrates implementing wait queues to model a four-direction traffic light, with per-direction queues and red/yellow/green states, and shows how user input and text file logging drive thread blocking and resumption.
Design a traffic light data structure with color and direction enumerations, implement a traffic light phase, manage per-face mutex and wait queues, and expose initialization and status APIs.
Explore template programs for multithreading with pthreads, implementing a user-driven traffic light state machine. Prepare for synchronization in the next lecture.
Model moving traffic with threads approaching traffic light faces, using per-phase wait queues and mutexes, plus thread private data and state enumeration to block on red and resume on green.
Explain what recursive mutexes are, why they prevent deadlocks by allowing the same thread to lock multiple times, and how to enable and use them with POSIX mutex attributes.
Explore when to use recursive mutexes in multithreaded software, showing how nested API locks can deadlock with normal mutexes and how recursive locks enable safe re-entry.
Develop and test a custom recursive mutex by implementing five APIs that mirror the POSIX recursive mutex behavior, ensuring mutual exclusion, proper blocking, and safe unlock usage.
Design and implement a recursive mutex data structure from scratch, detailing ownership, recursion count, locking semantics, four APIs, and thread-blocking management.
Implement the recursive mutex lock api by handling three cases: free, owned by the current thread, and owned by another thread; block with a condition variable and retry until available.
Learn how to implement rec_mutex_unlock() for a recursive mutex, handling unlocked, partially locked, and ownership violation cases, including decrementing lock counts, signaling blocked threads via a condition variable, and destruction.
Explore Reed Wright locks and mutex concepts, review in-built API support, examine use cases, then write your own implementation to gain conceptual clarity.
Explore how read/write locks boost concurrency by allowing multiple readers while writers stay exclusive. Learn read and write lock semantics and the lock/unlock API for safe multithreading.
Explore POSIX pthreads read-write locks: initialize, acquire read or write locks, unlock, and destroy, with a demo and an assignment to implement your own read-write locks using normal condition variables.
Explore a six-thread test of a read-write lock, with three readers and three writers, validating exclusive writer access and concurrent readers in a critical section.
Learn to implement a read-write lock from scratch, ensuring reader and writer coordination, support for recursion, correct unlock semantics, and crash-on-invalid unlock, using only condition variables and mutexes.
Learn to implement a read-write lock using a state mutex, a condition variable, and counters to manage access and ownership by readers and writers.
Learn read lock api implementation for a read write lock in pthreads, including initialization, the destroy api, and handling of available, reader-held, and writer-held states with recursion and wait semantics.
Implement a write lock api for a read lock object, detailing cases when the lock is available, held by the same writer (recursive), or by another thread (blocked).
Implement and validate the rw lock unlock api by handling three cases: crash on unlocking when already unlocked, enforce writer and reader ownership on unlock attempts, and broadcast to threads.
Explore how monitors generalize read-write locks to coordinate threads, enabling customizable cooperation schemes and problem-specific synchronization for multithreading with pthreads.
Upgrade the rewrite lock to a monitor by applying five customizations, making the read-write lock configurable, and setting up a monitor directory for upcoming changes.
Enhance the monitor to allow unlimited readers and writers in the critical section. Mirror the read lock API for the write lock API, remove obsolete members, and ensure symmetric behavior.
Implement customization two and three by introducing max readers and max writers in the monitor, adding an API to set them, and updating read locks and write locks.
Implement read and write bias in a monitor for pthreads, using separate condition variables cv_reader and cv_writer and flags to selectively allow or block readers and writers.
Implement the replacement property in a pthreads monitor by updating the unlock API to signal waiting readers and writers, achieving symmetric read and write locking.
Testing our custom monitor by modifying the test program to set max readers and max writers, then run and verify the critical section constraints with assertions and runtime output.
This is a sequel Course to Master Class-Multithreading & Thread Synchronization - C/C++, and this course should be done only when the prequel course has been completed unless you have reasonable knowledge regarding how Mutexes and Condition Variables work.
In this course, we shall be Implementing Advanced Thread Synchronization Data Structures and Multithreading based software patterns, with some advanced thread synchronization-based problem-solving. This course uses POSIX threads, hence it is Linux only course. We shall be building upon the knowledge we gathered from the prequel course.
*** However note that the course is highly conceptual and concepts learned can be easily applied to any programming knowledge which supports Multithreading. *** So, Java, C#, Python, Go, Kotlin Programmers do not hesitate to enroll in this course, you can write your own codes based on the concept discussed. This Course is unique and only one of its kind.
The level of this course is Intermediate to Advanced.
Table of Contents
====================
1. Thread Management
2. Pausing and Resuming Threads
3. Thread Pools/Work Crew Model
4. Thread Barriers
5. Wait-Queue Model
6. Recursive Mutexes
What are Recursive Mutexes?
When to use over Normal Mutexes?
Implement own Recursive Mutex
7. Read-Write Locks
What are Read-Write locks ?
What is its benefit?
Implement your own Read-Write lock
8. Monitors
Implement Reader-Writer Monitor
Solving Bridge Problem using Monitors
Guidelines for writing Monitors
Case Study : Covid Vaccination Drive Implementation using Monitors
*** Completed Till here as of 2 Mar 2022 **
9. Assembly Line Model
10. Deadlock Detection and Prevention
11. Other Related Courses on Multi-Threading
Designing Timers
Designing Multi-Threaded TCP Servers
Asynchronous Programming
UnScheduled Topics :
=================
11. Bounded Waiting
12. Fifo (Strong) Semaphores
13. Event Pair Synchronization
14. Inter-Process Synchronization using Semaphores
15. Forking a Multi-Threaded Program
Though We use the C language to demonstrate the concepts, concepts hold good for any programming language. This course is equally valuable for C++ programmers. Other language programmers may also find this course useful as we explain Multithreading concepts close to the ground zero levels with No Abstraction.
At each stage of this Course series, you shall be writing a lot of multi-threaded Codes. So be ready to Master the Multi-threading. Along the journey, we shall cover several interview-favorite topics and Questions to prepare you alongside for interviews.