Skip to content

Commit d7523eb

Browse files
Use std::async to handle spurious wake-ups.
1 parent f7694cc commit d7523eb

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/module.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#include "util/trash.h"
1414

1515
#include <chrono>
16-
#include <condition_variable>
16+
#include <future>
1717
#include <cstdio>
18-
#include <thread>
1918

2019
namespace zmq {
2120
class Context;
@@ -27,32 +26,27 @@ struct Terminator {
2726
assert(context != nullptr);
2827

2928
#ifdef ZMQ_BLOCKY
30-
int32_t blocky = zmq_ctx_get(context, ZMQ_BLOCKY);
29+
bool blocky = zmq_ctx_get(context, ZMQ_BLOCKY);
3130
#else
3231
/* If the option cannot be set, don't suggest to set it. */
3332
bool blocky = false;
3433
#endif
3534

36-
using namespace std::chrono_literals;
37-
std::mutex mut;
38-
std::condition_variable cv;
39-
40-
std::thread thread([&] {
35+
auto terminate = std::async(std::launch::async, [&] {
4136
auto err = zmq_ctx_term(context);
4237
assert(err == 0);
43-
cv.notify_all();
4438
});
4539

46-
std::unique_lock<std::mutex> lock(mut);
47-
if (cv.wait_for(lock, 500ms) == std::cv_status::timeout) {
40+
using namespace std::chrono_literals;
41+
if (terminate.wait_for(500ms) == std::future_status::timeout) {
4842
fprintf(stderr,
4943
"(node:%d) WARNING: Waiting for queued ZeroMQ messages to be "
5044
"delivered.%s\n",
5145
uv_os_getpid(),
5246
blocky ? " Set 'context.blocky = false' to change this behaviour." : "");
5347
}
5448

55-
thread.join();
49+
terminate.wait();
5650
}
5751
};
5852

test/unit/context-process-exit-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("context process exit", function() {
4343
this.slow(1000)
4444
const {stderr} = await createProcess(() => {
4545
zmq.context.blocky = true
46-
const socket1 = new zmq.Dealer({linger: 750})
46+
const socket1 = new zmq.Dealer({linger: 600})
4747
socket1.connect("tcp://127.0.0.1:4567")
4848
socket1.send(null)
4949
})
@@ -65,7 +65,7 @@ describe("context process exit", function() {
6565
this.slow(1000)
6666
const {stderr} = await createProcess(() => {
6767
zmq.context.blocky = false
68-
const socket1 = new zmq.Dealer({linger: 750})
68+
const socket1 = new zmq.Dealer({linger: 600})
6969
socket1.connect("tcp://127.0.0.1:4567")
7070
socket1.send(null)
7171
})
@@ -80,7 +80,7 @@ describe("context process exit", function() {
8080
this.slow(1000)
8181
const {stderr} = await createProcess(() => {
8282
zmq.context.blocky = true
83-
const socket1 = new zmq.Dealer({linger: 100})
83+
const socket1 = new zmq.Dealer({linger: 50})
8484
socket1.connect("tcp://127.0.0.1:4567")
8585
socket1.send(null)
8686
})

0 commit comments

Comments
 (0)