23. Concurrency Processing - C++
23. Concurrency Processing - C++
Concurrency Processing
Prerequisites
1
C++
1. Concurrency Processing
It is very similar before posting about concurrency, But usually python support many libraries. So It is also meaningful if I build a concurrency environments with C++
There’re some elements.
- Worker functions (it is main worker each thread)
- Capturing data from cam independently
- Create lock and unlock when input or output memory should be protected from other that it can be revising
2. Concurrency Processing Code
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <opencv2/opencv.hpp>
#include "CImageViewer.cpp"
#include "COperation.h"
#include <chrono>
#include <filesystem>
#include <string>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <string>
struct SSharedFrame
{
cv::Mat imgFrameGray;
std::mutex mtx;
std::atomic<int> version{0};
};
struct SWorkerResult
{
cv::Mat dst;
double timeMs = 0.0;
bool success = false;
int processedVersion = -1;
std::mutex mtx;
};
void ConvolutionWorker(SSharedFrame& sFrameInput, SWorkerResult& sResult, int i32Type, std::atomic<bool>& bRun)
{
COperation algmOper;
int i32LastVersion = -1;
while (bRun.load())
{
cv::Mat imgFrameSrc;
int i32CurrentVersion = -1;
{
std::lock_guard<std::mutex> lock(sFrameInput.mtx);
i32CurrentVersion = sFrameInput.version.load();
if (!(sFrameInput.imgFrameGray.empty() || i32CurrentVersion == i32LastVersion))
imgFrameSrc = sFrameInput.imgFrameGray.clone();
}
if (imgFrameSrc.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
cv::Mat imgFramDst(imgFrameSrc.rows, imgFrameSrc.cols, CV_8UC1, cv::Scalar(0));
auto start = std::chrono::high_resolution_clock::now();
bool bResult = algmOper.Convolution5x5(imgFrameSrc, imgFramDst, i32Type);
auto end = std::chrono::high_resolution_clock::now();
double elapsedMs = std::chrono::duration<double, std::milli>(end - start).count();
std::string strText = "Type " + std::to_string(i32Type) + " | " + std::to_string(elapsedMs) + " ms | " + (bResult ? "OK" : "FAIL");
cv::putText(imgFramDst,strText,cv::Point(10, 30),cv::FONT_HERSHEY_SIMPLEX,0.7,cv::Scalar(255),2,cv::LINE_AA);
{
std::lock_guard<std::mutex> lock(sResult.mtx);
sResult.dst = imgFramDst.clone();
sResult.timeMs = elapsedMs;
sResult.success = bResult;
sResult.processedVersion = i32CurrentVersion;
}
i32LastVersion = i32CurrentVersion;
}
}
int main()
{
bool bResult = false;
do
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
break;
cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
cv::Mat imgFrame;
cap.read(imgFrame);
if (imgFrame.empty())
break;
cv::Mat imgFrameGray;
cv::cvtColor(imgFrame, imgFrameGray, cv::COLOR_BGR2GRAY);
SSharedFrame SSharedFrame;
{
std::lock_guard<std::mutex> lock(SSharedFrame.mtx);
SSharedFrame.imgFrameGray = imgFrameGray.clone();
SSharedFrame.version = 1;
}
constexpr int CONV_COUNT = 7;
std::vector<std::thread> vctThread;
std::vector<SWorkerResult> vctResults(CONV_COUNT);
std::atomic<bool> bRun{true};
for (int i32Type = 1; i32Type <= CONV_COUNT; ++i32Type)
vctThread.emplace_back(ConvolutionWorker,std::ref(SSharedFrame), std::ref(vctResults[i32Type - 1]), i32Type, std::ref(bRun));
std::vector<cv::Mat> images(CONV_COUNT + 1, cv::Mat(imgFrameGray.rows, imgFrameGray.cols, CV_8UC1, cv::Scalar(0)));
CImageViewer viewer("Convolution Compare - Concurrent", images);
int frameVersion = 1;
while (true)
{
cap.read(imgFrame);
if (imgFrame.empty())
break;
cv::cvtColor(imgFrame, imgFrameGray, cv::COLOR_BGR2GRAY);
{
std::lock_guard<std::mutex> lock(SSharedFrame.mtx);
SSharedFrame.imgFrameGray = imgFrameGray.clone();
SSharedFrame.version = ++frameVersion;
}
images.clear();
cv::Mat imgOrg = imgFrameGray.clone();
cv::putText(imgOrg,"Original",cv::Point(10, 30),cv::FONT_HERSHEY_SIMPLEX,0.8,cv::Scalar(255),2,cv::LINE_AA);
images.push_back(imgOrg);
for (int i = 0; i < CONV_COUNT; ++i)
{
cv::Mat imgDisplay;
{
std::lock_guard<std::mutex> lock(vctResults[i].mtx);
if (!vctResults[i].dst.empty())
imgDisplay = vctResults[i].dst.clone();
}
if (imgDisplay.empty())
{
imgDisplay = cv::Mat(imgFrameGray.rows, imgFrameGray.cols, CV_8UC1, cv::Scalar(0));
cv::putText(imgDisplay,"Type " + std::to_string(i + 1) + " | Waiting...",cv::Point(10, 30),cv::FONT_HERSHEY_SIMPLEX,0.7,cv::Scalar(255),2,cv::LINE_AA);
}
images.push_back(imgDisplay);
}
viewer.UpdateImages(images);
if (!viewer.ShowOnce(1))
break;
}
bRun = false;
for (auto& worker : vctThread)
{
if (worker.joinable())
worker.join();
}
cap.release();
cv::destroyAllWindows();
bResult = true;
}
while(false);
return 0;
}
Result: Concureency processing
This post is licensed under CC BY 4.0 by the author.
