Skip to content

Commit

Permalink
Provide multi-threaded processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
srogatch committed Sep 1, 2018
1 parent d8df4af commit c38b6f9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions TextMatching/MatchText/BlockingQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

template <typename T> class BlockingQueue {
std::condition_variable _cvCanPop;
std::mutex _sync;
std::queue<T> _qu;
bool _bShutdown = false;

public:
void Push(const T& item)
{
{
std::unique_lock<std::mutex> lock(_sync);
_qu.push(item);
}
_cvCanPop.notify_one();
}

void RequestShutdown() {
{
std::unique_lock<std::mutex> lock(_sync);
_bShutdown = true;
}
_cvCanPop.notify_all();
}

bool Pop(T &item) {
std::unique_lock<std::mutex> lock(_sync);
for (;;) {
if (_qu.empty()) {
if (_bShutdown) {
return false;
}
}
else {
break;
}
_cvCanPop.wait(lock);
}
item = std::move(_qu.front());
_qu.pop();
return true;
}
};
Binary file modified TextMatching/MatchText/MatchText.cpp
Binary file not shown.
1 change: 1 addition & 0 deletions TextMatching/MatchText/MatchText.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="BlockingQueue.h" />
<ClInclude Include="Statistics.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
Expand Down
3 changes: 3 additions & 0 deletions TextMatching/MatchText/MatchText.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<ClInclude Include="Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BlockingQueue.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down
Binary file modified TextMatching/MatchText/stdafx.h
Binary file not shown.

0 comments on commit c38b6f9

Please sign in to comment.