Lecture 3

review - last time, discussed the following about processes:


MPI: message passing interface

example: process creation for efficiency

note on MPI:


threads

processes are "heavy" - lots of information associated with program code - turn to threads

threads:

side note: context switching:

difference between process and thread:

to create a new thread on the same process:

in general: threads are lighter, easier, and cheaper, but not always possible

shared vs. not shared in threads in the same process:

example:

int glob = 123;

int foo(int var) {
    float value;

    // using global variable (shared)
    // available to both t1 and t2
    // all threads access the same var
    // can cause race conditions
    cout << glob++;
}

int main() {
    // need to do #include <thread>
    // or something similar
    std::thread t1(foo, 3);
    std::thread t2(foo, 24);
    // now, this process has 3 threads -
    // original thread plus two newly created

    // the original thread proceeds and prints "Hi"
    cout << "Hi";

    // then, threads t1 and t2 begin running foo
    // with different parameters, indepedently
    // the threads can have different parameters
    // because they do not share the stack
}