Skip to content

Commit cba7672

Browse files
committed
1. fix handler remove bug
1 parent 6a71a44 commit cba7672

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

examples/handler_tutorial.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ int main(int argc, char **argv) {
2828

2929
hdlr.postDelay([]() { cout << "POST call back" << endl; }, 230);
3030

31-
32-
std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::seconds(12));
31+
std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::seconds(3));
32+
hdlr.removeMessages(6);
33+
std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::seconds(1));
34+
hdlr.removeAlls();
3335
// hdlr.stop();
3436
cout << "Program exit !" << endl;
3537
return 1;

handler/handler.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ bool Handler::sendEmptyMessageDelay(int what, long delayMillis) {
5959

6060
bool Handler::post(std::function<void()> &&f) { return postDelay(std::move(f), 0); }
6161
bool Handler::postDelay(std::function<void()> &&f, long delayMillis) {
62-
6362
if (f == nullptr || delayMillis < 0) {
6463
return false;
6564
}
@@ -76,12 +75,12 @@ void Handler::removeMessages(int what) {
7675
if (what < 0) return;
7776

7877
std::unique_lock<std::mutex> lock(queue_mutex);
79-
msg_list.remove_if([what](const Message &m) { return m.what == what; });
78+
if (!msg_list.empty()) msg_list.remove_if([what](const Message &m) { return m.what == what; });
8079
}
8180

8281
void Handler::removeAlls() {
8382
std::unique_lock<std::mutex> lock(queue_mutex);
84-
msg_list.clear();
83+
if (!msg_list.empty()) msg_list.clear();
8584
}
8685

8786
void Handler::stop() {

0 commit comments

Comments
 (0)