Skip to content

Commit 9291241

Browse files
committed
Added condition variable
1 parent 2dc84ed commit 9291241

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

ext/or-tools/constraint.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <atomic>
2+
#include <condition_variable>
23
#include <mutex>
34
#include <queue>
45
#include <string>
@@ -434,6 +435,7 @@ void init_constraint(Rice::Module& m) {
434435
std::atomic<bool> done{false};
435436
std::queue<CpSolverResponse> queue;
436437
std::mutex queue_lock;
438+
std::condition_variable cv;
437439
Rice::Object ruby_thread;
438440
std::optional<Rice::Exception> exception;
439441

@@ -451,8 +453,10 @@ void init_constraint(Rice::Module& m) {
451453
while (true) {
452454
CpSolverResponse r;
453455
{
454-
std::lock_guard<std::mutex> guard(queue_lock);
455-
if (queue.empty()) {
456+
std::unique_lock<std::mutex> lock(queue_lock);
457+
// TODO increase when GVL unlocked
458+
auto time = std::chrono::system_clock::now() + std::chrono::milliseconds(1);
459+
if (!cv.wait_until(lock, time, [&] { return !queue.empty(); })) {
456460
break;
457461
}
458462
r = std::move(queue.front());
@@ -491,6 +495,7 @@ void init_constraint(Rice::Module& m) {
491495
[&](const CpSolverResponse& r) {
492496
std::lock_guard<std::mutex> guard(queue_lock);
493497
queue.push(r);
498+
cv.notify_one();
494499
})
495500
);
496501
}

0 commit comments

Comments
 (0)