
PPT Attachedon 12 May 2024
Set up a posix-compliant environment on Linux or macOS, using Ubuntu in a VMware VM, fork the repository on GitHub, and use Visual Studio Code for code access.
Explain synchronous versus asynchronous programming through a cake-making example, highlighting how parallel steps avoid waiting. Show how signals notify completion and prevent blocking so tasks run independently.
Explore the difference between asynchronous and multithreading, learn how to implement single-threaded and multi-threaded asynchronous designs, and manage tasks like network packets, timer callbacks, and user requests.
Explore four models of synchronous and asynchronous computation by solving sum and multiplication tasks on a 10-element integer array, including asynchronous single-threaded and asynchronous multi-threaded designs with task subdivision.
Adopt a fast-track, hands-on course on asynchronous design patterns by building an event loop (dispatch queue) and applying language-agnostic concepts, with libuv as an example.
Explore the design and implementation of an event loop, a multipurpose asynchronous data structure that schedules computations, and build an event loop library for various async models.
set up a posix-compliant linux environment, such as ubuntu 20.04 in a vm, fork the course repository on github, and use gcc for c/c++ demonstrations.
Design and implement an event loop mini library by defining task structures as function pointers with arguments, using a linked-list queue, mutexes, a condition variable, and a POSIX thread.
Implement init and run APIs for an event loop, initialize members, mutex, and condition variable, and spawn a detached thread to run the loop; demonstrate with a demo app.
Implement three APIs for the final event loop: detach the head task, append a new task to the end, and check for a paused task using linked list logic.
Refer to Appendix Section A if you want to refresh how a typical producer - consumer pattern is implemented before going through this lecture video.
Demonstrates single-threaded concurrency by using an event loop to run upload and download tasks in parallel, illustrating task scheduling, context switching, and that tasks remain invisible to the operating system.
Use locking to preserve data integrity across arbitrary context switches, preventing inconsistent states during multi-threaded updates, especially when operations on shared structures like linked lists are interrupted.
Understand why event loops are lock free: a single event loop thread executes all tasks, and developer-controlled task switching ensures data structures stay consistent without locks.
Introduce the design pattern to serialize multithreaded flows, and discuss reducing a multithreaded program to a single-threaded form, plus criteria for choosing between multi- and single-threaded approaches.
Copy pasted from Multi-threading Course.
Integrate an event loop in a c/c++ project by creating the loop object, packaging routing table updates as a single job, and updating via a callback.
Discover how timers enable asynchronous programming with POSIX timer APIs, supporting one-shot and periodic scheduling, while learning to manage unwanted concurrency with an event loop for a lock-free, single-threaded design.
Explore asynchronous programming design patterns in C/C++ by adding expiration timers to routing table entries, using timer setup and callbacks for millisecond expiration to delete expired entries and prevent leaks.
This lecture shows how timer threads on routing table entries create concurrency in STP, and how a one-line fix hands deletion to the event loop to restore single-threaded behavior.
Explore how the event loop schedules asynchronous computations, dequeueing tasks from its queue to enable concurrency in a single threaded program while avoiding forced multithreading.
By the end of this section, the Src code snapshot would be pointed to by this git hashcode : 03a29fa1b0bbf83ec7219e6f1bd65a3b2be35ccd
How to setup this commit hashcode, pls follow the below steps :
Download git repo :
https://github.com/sachinites/AsyncProgramming
Set the git head to above hash code
git reset --hard 03a29fa1b0bbf83ec7219e6f1bd65a3b2be35ccd
now, you can browse the code of this repo when this section was completed.
To come back to latest commit :
git pull
Learn to implement preemption in an event loop by returning continue or finish from the callback, adjust prototypes and demo code, and reschedule tasks accordingly.
Demonstrate preemption in the SDP process by saving and restoring task state so the event loop resumes from the next instruction after each ten routing table entries.
To see the solution, visit the link :
https://github.com/sachinites/AsyncProgramming/commit/d09e4de5e25e38353377701dffffb1b6805dad16
Explore how task prioritization in asynchronous C/C++ design patterns improves user response time by making show command highest priority and lowering delete tasks to prevent premature deletions in packet processing.
Explore how task priority in an event loop affects server responsiveness, showing how elevating http request packet processing above a long-running algorithm reduces delays and premature deletion issues.
Understand premature deletion in asynchronous programming, where deleting a data object with pending tasks creates a dangling pointer and may crash the program; learn a graceful method to handle it.
Postpone destroying data object d until all tasks on the event loop finish, preventing premature deletion. Implement a separate delete task with the lowest priority so other tasks run first.
Implement asynchronous single routing table entry deletion in the STP process, using low priority tasks to prevent premature deletion, detach from the routing table, and safely release resources.
Learn how to safely delete an entire routing table in an asynchronous environment by isolating the routing table, launching a low-priority delete task, and deleting a linked list without interference.
Showcases asynchronous deletion of a routing table (a doubly linked list) via delete tasks scheduled at the lowest event-loop priority, deleting ten entries per preempted batch.
Explore why concurrency remains essential in multithreaded systems by examining how 100 threads share 16 CPUs, achieving a hybrid of concurrency and parallelism for better performance.
Concurrency enables a multi-threaded process to wait for input/output, send periodic network packets, and process user input simultaneously despite limited CPUs.
Design a multi-threaded process when work can be split into independent chunks, using worker threads to service each client and delegating work for isolation of server and client communications.
Create and start two UDP listener threads on localhost, ports 3000 and 3001, passing received packets to the main application via a packet receive function.
Walks through implementing a UDP listener thread by creating a thread, packing ip address, port, and receive callback into a state structure, binding a UDP socket, and notifying on packets.
Welcome to the World of Asynchronous Programming!!
Asynchronous programming is used anywhere where your application interacts with other external entities - be it other threads, other processes. This world by its nature is Asynchronous. Asynchronous literally means - anything can happen anytime - unpredictable. How do you write software through Asynchronous programming? That's what we shall be going to learn in this course. Big software projects of reasonable sizes are Asynchronous. Software projects leave their synchronous boundaries the moment it starts integrating and communicating with external entities or go multithreaded.
Asynchronous programming is widely used in Distributed Systems, Networking Protocols, Distributes Databases, etc.
The prerequisite of this Course :
Know Thread Synchronization Concepts - Mutexes and Condition Variables ( any thread library, but we will be using pthreads )
C or C++ in preferable, but if your fav language is any other mainstream language then also it is ok. Borrow the concepts and implement it in your fav language.
Comfortable with basic Data structures such as Linked-List.
Zeal to learn and excel - beyond what Crowd is doing ( DS/ALGO/CP )
The end product of this Course shall be a mini library yet very powerful library which allows you do develop software through Asynchronous programming.
Course Difficulty Level :
The Course is level is Intermediate to Advanced. Very beginners pls refrain from enrolling into the Course.
Software Designing Problems to Address in this Course :
Problem 1. Simulating Concurrency in Single-Threaded Programs
problem 2. Reducing Multi-threaded Programs to Single Threaded
Problem 3. Work Deferrals
Problem 4. Asynchronous Communication
Problem 5. Queuing and Processing Incoming Network Packets
Good luck ! Happy Learning.
After doing this course You would understand :
Problems with Synchronous programming
Design and Implement Asynchronous Application/Softwares
What type of applications/softwares must not be multi-threaded
What type of applications/softwares must be asynchronous
Say good bye to locking and thread synchronization for forever.
The Concepts you would learn in this course are portable to other programming languages of your choice