-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharingData.cpp
More file actions
47 lines (37 loc) · 1.05 KB
/
SharingData.cpp
File metadata and controls
47 lines (37 loc) · 1.05 KB
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
37
38
39
40
41
42
43
44
45
46
47
#include <chrono>
#include <iostream>
#include <thread>
#include "ExampleRegistry.h"
namespace {
int glo_var = 99;
void f1(int arg) {
std::cout << "arg: " << arg << '\n';
glo_var = 100;
}
void f2(int& arg) {
// thread_local int thread_loc_var = 1;
std::cout << "arg: " << arg << '\n';
arg = 100;
}
void run() {
int loc_var = 10;
std::cout << "loc_var: " << loc_var << '\n';
std::cout << "glo_var: " << glo_var << '\n';
std::thread t1(f1, loc_var);
t1.join();
std::cout << "loc_var: " << loc_var << '\n';
std::cout << "glo_var: " << glo_var << '\n';
std::thread t2(f2, std::ref(loc_var));
t2.join();
std::cout << "loc_var: " << loc_var << '\n';
}
} // namespace
class SharingData : public IExample {
std::string group() const override { return "core/concurrency"; }
std::string name() const override { return "SharingData"; }
std::string description() const override {
return "The examples for <thread> sharing data";
}
void execute() override { run(); }
};
REGISTER_EXAMPLE(SharingData, "core/concurrency", "SharingData");