Lecture 4

clarification - interrupts:

clarification - context switching:

example of reason to create threads even if only one core: webserver

multitasking: quickly switching core from one task to another

different kinds of threads: kernel threads and user threads

thread project example:

#include<iostream>
#include<thread>

void Greet(std::string name) {
    std::cout << "Hello from " << name << std::endl;
}

int main() {
    std::thread t1{ Greet, "t1" };
    std::thread t2{ Greet, "t2" };
    std::cout << "Hello from original" << std::endl;
}

sample output:

Hello from original
Hello from t2
Hello from t1

output does not necessarily come in same order:

another possible output:

Hello from original
Hello from Hello from t1
t2

above, cout is shared between threads - caused a race condition

next time - will discuss interprocess communication and synchronization