123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #ifndef CERES_INTERNAL_CONCURRENT_QUEUE_H_
- #define CERES_INTERNAL_CONCURRENT_QUEUE_H_
- #include <condition_variable>
- #include <mutex>
- #include <queue>
- #include <thread>
- #include "glog/logging.h"
- namespace ceres::internal {
- template <typename T>
- class ConcurrentQueue {
- public:
-
- ConcurrentQueue() = default;
-
-
- void Push(const T& value) {
- std::lock_guard<std::mutex> lock(mutex_);
- queue_.push(value);
- work_pending_condition_.notify_one();
- }
-
-
- bool Pop(T* value) {
- CHECK(value != nullptr);
- std::lock_guard<std::mutex> lock(mutex_);
- return PopUnlocked(value);
- }
-
-
-
- bool Wait(T* value) {
- CHECK(value != nullptr);
- std::unique_lock<std::mutex> lock(mutex_);
- work_pending_condition_.wait(lock,
- [&]() { return !(wait_ && queue_.empty()); });
- return PopUnlocked(value);
- }
-
-
-
- void StopWaiters() {
- std::lock_guard<std::mutex> lock(mutex_);
- wait_ = false;
- work_pending_condition_.notify_all();
- }
-
- void EnableWaiters() {
- std::lock_guard<std::mutex> lock(mutex_);
- wait_ = true;
- }
- private:
-
-
-
- bool PopUnlocked(T* value) {
- if (queue_.empty()) {
- return false;
- }
- *value = queue_.front();
- queue_.pop();
- return true;
- }
-
-
-
- std::mutex mutex_;
- std::condition_variable work_pending_condition_;
- std::queue<T> queue_;
-
-
- bool wait_{true};
- };
- }
- #endif
|