Added timeout to ConditionVariableNotifier::waitForNotification() function
This commit is contained in:
@@ -8,11 +8,18 @@ namespace Bloom
|
|||||||
this->conditionalVariable.notify_all();
|
this->conditionalVariable.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConditionVariableNotifier::waitForNotification() {
|
void ConditionVariableNotifier::waitForNotification(std::optional<std::chrono::milliseconds> timeout) {
|
||||||
auto lock = std::unique_lock(this->mutex);
|
const auto predicate = [this] {
|
||||||
this->conditionalVariable.wait(lock, [this] {
|
|
||||||
return this->notified;
|
return this->notified;
|
||||||
});
|
};
|
||||||
|
auto lock = std::unique_lock(this->mutex);
|
||||||
|
|
||||||
|
if (timeout.has_value()) {
|
||||||
|
this->conditionalVariable.wait_for(lock, timeout.value(), predicate);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this->conditionalVariable.wait(lock, predicate);
|
||||||
|
}
|
||||||
|
|
||||||
this->notified = false;
|
this->notified = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <chrono>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "NotifierInterface.hpp"
|
#include "NotifierInterface.hpp"
|
||||||
|
|
||||||
@@ -34,7 +36,7 @@ namespace Bloom
|
|||||||
/**
|
/**
|
||||||
* Blocks until the contained std::conditional_variable is notified.
|
* Blocks until the contained std::conditional_variable is notified.
|
||||||
*/
|
*/
|
||||||
void waitForNotification();
|
void waitForNotification(std::optional<std::chrono::milliseconds> timeout = std::nullopt);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
|
|||||||
Reference in New Issue
Block a user