-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
111 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,71 @@ | ||
#ifndef BLOCKING_QUEUE_ | ||
#define BLOCKING_QUEUE_ | ||
|
||
#include <deque> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <algorithm> | ||
|
||
template <typename T> | ||
template<typename T> | ||
class BlockingQueue { | ||
public: | ||
void put(T elem) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
backingQueue.push_back(elem); | ||
waitObj.notify_all(); | ||
} | ||
void put(T elem) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
backingQueue.push_back(elem); | ||
waitObj.notify_all(); | ||
} | ||
|
||
bool empty() { | ||
return size() == 0; | ||
} | ||
|
||
bool empty() { | ||
return size() == 0; | ||
} | ||
bool swapToFrontIf(std::function<bool(T)> func) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
auto pos = std::find_if(backingQueue.begin(), backingQueue.end(), func); | ||
if (pos != backingQueue.begin() && pos != backingQueue.end()) { | ||
std::iter_swap(pos, backingQueue.begin()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool swapToFrontIf(std::function<bool(T)> func) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
auto pos = std::find_if(backingQueue.begin(), backingQueue.end(), func); | ||
if (pos != backingQueue.begin() && pos != backingQueue.end()) { | ||
std::iter_swap(pos, backingQueue.begin()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
bool removeIf(std::function<bool(T)> func) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
auto it = std::remove_if(backingQueue.begin(), backingQueue.end(), func); | ||
bool removed = it != backingQueue.end(); | ||
backingQueue.erase(it, backingQueue.end()); | ||
return removed; | ||
} | ||
|
||
bool removeIf(std::function<bool(T)> func) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
auto pos = std::find_if(backingQueue.begin(), backingQueue.end(), func); | ||
if (pos != backingQueue.begin() && pos != backingQueue.end()) { | ||
backingQueue.erase(pos); | ||
return true; | ||
} | ||
return false; | ||
} | ||
void remove(T elem) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
backingQueue.erase(std::remove(backingQueue.begin(), backingQueue.end(), elem), backingQueue.end()); | ||
} | ||
|
||
void remove(T elem) { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
backingQueue.erase(std::remove(backingQueue.begin(), backingQueue.end(), elem), backingQueue.end()); | ||
} | ||
size_t size() { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
return backingQueue.size(); | ||
} | ||
|
||
size_t size() { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
return backingQueue.size(); | ||
} | ||
T take() { | ||
std::unique_lock<std::recursive_mutex> lock(mutex); | ||
while (size() == 0) waitObj.wait(lock); | ||
auto front = backingQueue.front(); | ||
backingQueue.pop_front(); | ||
return front; | ||
} | ||
|
||
T take() { | ||
std::unique_lock<std::recursive_mutex> lock(mutex); | ||
while (size() == 0) waitObj.wait(lock); | ||
auto front = backingQueue.front(); | ||
backingQueue.pop_front(); | ||
return front; | ||
} | ||
std::deque<T> clear() { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
std::deque<T> returnQueue = backingQueue; | ||
backingQueue.clear(); | ||
return returnQueue; | ||
} | ||
|
||
std::deque<T> clear() { | ||
std::lock_guard<std::recursive_mutex> lock(mutex); | ||
std::deque<T> returnQueue = backingQueue; | ||
backingQueue.clear(); | ||
return returnQueue; | ||
} | ||
private: | ||
std::deque<T> backingQueue; | ||
std::recursive_mutex mutex; | ||
std::condition_variable_any waitObj; | ||
std::deque<T> backingQueue{}; | ||
std::recursive_mutex mutex{}; | ||
std::condition_variable_any waitObj{}; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.