Getting ready

In this recipe, we will work with the following example code (example.cpp):

#include <chrono>#include <iostream>#include <thread>static const int num_threads = 16;void increase(int i, int &s) {  std::this_thread::sleep_for(std::chrono::seconds(1));  std::cout << "thread " << i << " increases " << s++ << std::endl;}int main() {  std::thread t[num_threads];  int s = 0;  // start threads  for (auto i = 0; i < num_threads; i++) {    t[i] = std::thread(increase, i, std::ref(s));  }  // join threads with main thread  for (auto i = 0; i < num_threads; i++) {    t[i].join();  }  std::cout << "final s: " << s << std::endl;  return 0;}

In this example code, we start 16 threads, and each of these threads calls the increase function. The increase function ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.