-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_local_cpp11.cpp
More file actions
36 lines (27 loc) · 891 Bytes
/
thread_local_cpp11.cpp
File metadata and controls
36 lines (27 loc) · 891 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
#include <thread>
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
//// thread_local
//// - gives each thread its own independent copy of the variable.
////
//// Key notes:
//// - lifetime — tied to thread, not program
//// - mental model: what static is to programs, thread_local is to threads
thread_local int counter = 10;
void work(int thread_id) {
thread_id == 1 ? ++counter : --counter;
std::cout << "Thread " << thread_id << ": counter = " << counter << "\n";
}
TEST_CASE("thr-loc-1") {
std::thread t1 {work, 1};
std::thread t2 {work, -1};
t1.join();
t2.join();
std::cout << "Thread main: counter = " << counter << "\n";
REQUIRE(counter == 10);
}
// Thread -1: counter = 9
// Thread 1: counter = 11
// Thread main: counter = 10