Skip to content

2. Threads and Processes

Thread versus process

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Threads that waste CPU cycles
#include <chrono>
#include <thread>
#include <unistd.h>

// a simple function that wastes CPU cycles "forever"
void cpu_waster() {
  printf("CPU Waster Process ID: %d\n", getpid());
  printf("CPU Waster Thread ID %d\n", std::this_thread::get_id());
  while (true) continue;
}

int main() {
  printf("Main Process ID: %d\n", getpid());
  printf("Main Thread ID: %d\n", std::this_thread::get_id());
  std::thread thread1(cpu_waster);
  std::thread thread2(cpu_waster);

  while (true) {  // keep the main thread alive "forever"
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
}

Execution scheduling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Two threads chopping vegetables
#include <chrono>
#include <thread>

bool chopping = true;

void vegetable_chopper(const char* name) {
  unsigned int vegetable_count = 0;
  while (chopping) {
    ++vegetable_count;
  }
  printf("%s chopped %u vegetables.\n", name, vegetable_count);
}

int main() {
  std::thread olivia(vegetable_chopper, "Olivia");
  std::thread barron(vegetable_chopper, "Barron");

  printf("Barron and Olivia are chopping vegetables...\n");
  std::this_thread::sleep_for(std::chrono::seconds(1));
  chopping = false;
  barron.join();
  olivia.join();
}

Thread life cycle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Two threads cooking soup
#include <chrono>
#include <thread>

void chef_olivia() {
  printf("Olivia started & waiting for sausage to thaw...\n");
  std::this_thread::sleep_for(std::chrono::seconds(3));
  printf("Olivia is done cutting sausage.\n");
}

int main() {
  printf("Barron requests Olivia's help.\n");
  std::thread olivia(chef_olivia);

  printf("Barron continues cooking soup.\n");
  std::this_thread::sleep_for(std::chrono::seconds(1));

  printf("Barron patiently waits for Olivia to finish and join...\n");
  olivia.join();

  printf("Barron and Olivia are both done!\n");
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Two threads cooking soup
#include <chrono>
#include <thread>

void chef_olivia() {
  printf("Olivia started & waiting for sausage to thaw...\n");
  std::this_thread::sleep_for(std::chrono::seconds(3));
  printf("Olivia is done cutting sausage.\n");
}

int main() {
  printf("Barron requests Olivia's help.\n");
  std::thread olivia(chef_olivia);
  printf("  Olivia is joinable? %s\n", olivia.joinable() ? "true" : "false");

  printf("Barron continues cooking soup.\n");
  std::this_thread::sleep_for(std::chrono::seconds(1));
  printf("  Olivia is joinable? %s\n", olivia.joinable() ? "true" : "false");

  printf("Barron patiently waits for Olivia to finish and join...\n");
  olivia.join();
  printf("  Olivia is joinable? %s\n", olivia.joinable() ? "true" : "false");

  printf("Barron and Olivia are both done!\n");
}

Detached thread

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Barron finishes cooking while Olivia cleans
#include <chrono>
#include <thread>

void kitchen_cleaner() {
  while (true) {
    printf("Olivia cleaned the kitchen.\n");
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
}

int main() {
  std::thread olivia(kitchen_cleaner);
  for (int i = 0; i < 3; ++i) {
    printf("Barron is cooking...\n");
    std::this_thread::sleep_for(std::chrono::milliseconds(600));
  }
  printf("Barron is done!\n");
  olivia.join();
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Barron finishes cooking while Olivia cleans
#include <chrono>
#include <thread>

void kitchen_cleaner() {
  while (true) {
    printf("Olivia cleaned the kitchen.\n");
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
}

int main() {
  std::thread olivia(kitchen_cleaner);
  olivia.detach();
  for (int i = 0; i < 3; ++i) {
    printf("Barron is cooking...\n");
    std::this_thread::sleep_for(std::chrono::milliseconds(600));
  }
  printf("Barron is done!\n");
}