CPP
cpp
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
/*
In the Worker class example, ref is applied to the instance (w1, w2), because we want to update the private _val value.
In the adder function example, ref is applied to int num, because it is the value we want to update.
*/
#include <iostream>
#include <thread>
using namespace std;
class Worker{
int _val{};
public:
void operator()(int n){
_val += n;
}
int get(){ return _val;}
};
void adder(int& n){
n++;
}
int main()
{
Worker w1, w2;
cout << "before running threads:"
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run