/
thread-rope.cpp
75 lines (61 loc) · 2.6 KB
/
thread-rope.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <thread>
#include <sstream>
// A rope is made up of many threads. Each thread is a strand of the rope.
class Rope
{
int value;
public:
Rope() : value(0) { std::cout << "Rope() ctor\n"; }
~Rope() { std::cout << "~Rope() dtor: value=" << value << '\n'; }
void Set(int v) { value=v; }
void Print()
{ std::stringstream ss; ss << "Print " << value << "\n"; std::cout << ss.str(); }
void Compute()
{ std::stringstream ss; ss << "Compute " << value << "\n"; std::cout << ss.str(); }
void ComputeArg(int arg)
{ std::stringstream ss; ss << "ComputeArg " << arg << "\n"; std::cout << ss.str(); }
};
int main()
{
const int NUM_THREADS = 3;
Rope strands[NUM_THREADS];
std::vector<std::thread> threadVector;
std::thread threadTable[NUM_THREADS];
int arg;
int index;
for(auto& r : strands) r.Print();
arg = 1000;
for(auto& r : strands) r.Set(arg++);
for(auto& r : strands) r.Print();
std::cout << "\nmethod 1 - std::bind class member function\n";
for(auto& r : strands) threadVector.push_back(std::thread(std::bind(&Rope::Compute, &r))); // class member function
for(auto& th : threadVector) th.join();
threadVector.clear();
std::cout << "\nmethod 2 - lambda\n";
for(auto& r : strands) threadVector.push_back(std::thread( [&]() { r.Compute(); } )); // lambda
for(auto& th : threadVector) th.join();
threadVector.clear();
std::cout << "\nmethod 3 - std::bind class member function with arg\n";
arg = 100;
for(auto& r : strands) threadVector.push_back(std::thread(std::bind(&Rope::ComputeArg, &r, arg++))); // class member function
for(auto& th : threadVector) th.join();
threadVector.clear();
std::cout << "\nmethod 4 -lambda - call class member function with arg\n";
arg = 200;
for(auto& r : strands) threadVector.push_back(std::thread([&]() { r.ComputeArg(arg++); })); // lambda
for(auto& th : threadVector) th.join();
threadVector.clear();
std::cout << "\nmethod 5 - threadTable without vector\n";
std::cout << "create Compute threads\n";
index = 0;
for(auto& th : threadTable) th = std::thread( std::bind(&Rope::Compute, &strands[index++] ) );
for(auto& th : threadTable) th.join();
std::cout << "create ComputeArg threads\n";
index = 0;
arg = 300;
for(auto& th : threadTable) th = std::thread( std::bind(&Rope::ComputeArg, &strands[index++], arg++ ) );
for(auto& th : threadTable) th.join();
std::cout << "\nmain exiting\n";
}