CS202 Review Session 3 Notes from [Xiangyu Gao](https://xiangyug.github.io/), TA from fall 2021 Edited by Khanh Nguyen, TA Spring 2022 1. Process and thread 2. What is concurrency? 3. Concurrency Commandment 4. Sequential Consistency 5. C++ primer 6. Lab 3 overview 7. Q&A 8. Resources --------------------------------------------------------------------- 1. Process and thread - Process: an instance of a program - Thread: can think of as execution path of a process. A single process can be referred to as a single-threaded process. A process can have many threads, referred to as multi-thread. Thread have their own registers and stacks but share everything else. 2. What is concurrency? - "It's when there are mutliple threads?" - "It's when things execute at the same time" It's true. That's what concurrency is: multiple things happening at the same time, competing for shared resources. Why concurrency? - Because it's ubiquitous. An analogy, if you are in the office hours, you are competing for the TA’s attention to debug your code. The TA is the shared resources and the student is a thread. - We also care because it mimics the real world, in which lots of different things happening at the same time. But there are limited resources so we have to come up with some ways to access these shared resources. - In the programming world, we came up concurrency models and primitives. - There are different concurrency models: monitors in Java, message passing in Erlang and Haskell. We also have different concurrency primitives. We have mutex, conditional variables and semaphore (Dijkstra invented semaphore :o). - In cs202, we will use a monitor, which is a combination of mutex and conditional variables for CS202. **If you want to learn more about these, take Programming Languages, which is CSCI490 with Edward Yang, CSCI480 with Thomas Wies or the grad course.** 3. Concurrency Commandment - Rule1: acquire/release at beginning/end of methods - Rule2: hold lock when doing condition variable operations - Rule3: a thread in wait() must be prepared to be restarted at any time, not just when another thread calls signal - Cannot replace broadcast() with signal(). But can replace signal() with broadcast() 4. Sequential Consistency - "Informally, sequential consistency implies that operations appear to take place in some total order, and that that order is consistent with the order of operations on each individual process” (https://jepsen.io/consistency/models/sequential) Mental Model: T1: ---------A1------------A2------ T2: ------B1-------------B2------- In sequential consistency model, we can think of this as a game where you have to provide an ordering of all the events across all the threads. But the catch is that you can't re-order event within single thread. However, order across different thread can be re-ordered For instance, here are some valid order: - B1-A1-A2-B2 - B1-A1-B2-A2 - A1-B1-A2-B2 And here is some invalid order: - B1-A2-A1-B2 // A2 happens before A1 but both in T1 - B2-B1-A1-A2 // B2 happens before B1 but both in T2 5.C++ primer a. Destructor: - Syntax: classname::~classname(). E.g: TaskQueue::~TaskQueue() - Opposite of constructor - Used to clean up memory, destroy mutex and conditional variables b. Freeing dynamically allocated memory - Syntax in C++ is delete. E.g: delete POINTER_GO_HERE c. Printing - printf(...) - std::cout << ""... 6. Lab 3 overview Producer - Consumer architecture [See whiteboard] Lab 3 architecture [See whiteboard] Coarse-grained lock: - The whole store/object shares one lock and conditional variable Fine-grained lock: - Each item in the store has its own lock Some notable files: - sthread.cpp: our thin wrapper around pthread. Use this as your threading library - TaskQueue.cpp: the task queue that suppliers and consumers dequeue to operate on. - estoresim.cpp: main entry point for the code, creating threads - EStore.cpp: the shared object that suppliers and consumers are operating on - RequestHandlers.cpp: handler so that worker thread knows what to do Some common pitfalls: - Forgot to use destructor to free/destroy resources - Forgot to release the mutex - Has code of the form: int condition = some_condition while(condition) { .... } -> This is wrong because condition might rely on shared variables so need to be recomputed 7. Q&A 8. Resources: - JS Event Loop visualizer: https://www.jsv9000.app/ - Jepsen: https://jepsen.io/consistency/models/sequential