-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.cpp
147 lines (134 loc) · 3.95 KB
/
Example.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Example.cpp
*
* Created on: 23.08.2012
* Author: doriangrey
*/
#include "Channel/Channel.hpp"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
/**
Example 1: Create a channel-object from scratch, handle it over to two threads, and go ahead.
*/
void Example1(std::uint32_t numRuns)
{
Channel<float> chan;
std::mutex coutmutex;
std::thread t1([&chan, numRuns, &coutmutex]() -> void
{
std::uint32_t runs = numRuns;
float msgBase = 5.1f;
while (runs-- > 0)
{
chan << msgBase;
msgBase += 1.0f;
std::lock_guard<std::mutex> guard(coutmutex);
std::cout << "Task 1 put a msg." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
std::thread t2([&chan, numRuns, &coutmutex]() -> void
{
float res = 0;
std::uint32_t runs = numRuns;
while (runs-- > 0)
{
chan >> res;
std::lock_guard<std::mutex> guard(coutmutex);
std::cout << "Task 2 took a msg: " << res << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
t1.join();
t2.join();
}
/**
Example 2: Create a shared-ptr-channel, handle it over to two threads, and go ahead.
Note that it appears rather similar to the version above, since the channel is handled
over as reference from enclosing scope.
*/
void Example2(std::uint32_t numRuns)
{
auto chan = make_chan<int>();
std::mutex coutmutex;
std::thread t1([&chan, numRuns, &coutmutex]() -> void
{
std::uint32_t runs = numRuns;
int msgBase = 5;
while (runs-- > 0)
{
chan << msgBase;
msgBase++;
std::lock_guard<std::mutex> guard(coutmutex);
std::cout << "Task 1 put a msg." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
std::thread t2([&chan, numRuns, &coutmutex]() -> void
{
int res = 0;
std::uint32_t runs = numRuns;
while (runs-- > 0)
{
chan >> res;
std::lock_guard<std::mutex> guard(coutmutex);
std::cout << "Task 2 took a msg: " << res << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
t1.join();
t2.join();
}
/**
Example 3: Create a shared-ptr-channel, handle it over to two threads, and go ahead.
*/
static std::mutex example3_mutex;
void Example3_Task1(Chan<double> channel, std::uint32_t numRuns)
{
double msgBase = 5.3;
while (numRuns-- > 0)
{
channel << msgBase;
msgBase += 1.0;
std::lock_guard<std::mutex> guard(example3_mutex);
std::cout << "Task 1 put a msg." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void Example3_Task2(Chan<double> channel, std::uint32_t numRuns)
{
double res = 0.0;
while (numRuns-- > 0)
{
channel >> res;
std::lock_guard<std::mutex> guard(example3_mutex);
std::cout << "Task 2 took a msg: " << res << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void Example3(std::uint32_t numRuns)
{
auto chan = make_chan<double>();
std::thread t1(Example3_Task1, chan, numRuns);
std::thread t2(Example3_Task2, chan, numRuns);
t1.join();
t2.join();
}
int main()
{
// Running test 1
std::cout << "[Test 1] Directly instantiating channel... " << std::endl;
Example1(10);
std::cout << "[Test 1] Completed. " << std::endl;
// Running test 2
std::cout << "[Test 2] Instantiating channel using make_chan and wrapper-struct... " << std::endl;
Example2(10);
std::cout << "[Test 2] Completed. " << std::endl;
// Running test 3
std::cout << "[Test 3] Instantiating channel and copy by default... " << std::endl;
Example3(10);
std::cout << "[Test 3] Completed. " << std::endl;
return 0;
}