ssize_t get_current_thread_pending_acks_impl() const {
auto it = last_ack_.find(std::this_thread::get_id());
if (it != last_ack_.end()) {
return (it->second > received_acks_) ? it->second - received_acks_ : 0;
}
return 0;
}
AckMonitor的该方法判断某线程的消息是否都有ACK,只是简单比较了已发送消息计数sent_acks_的最大值。
而这个计数的增加也有意思,仅仅让每个线程记录sent_acks_的最大值。
// Increments the number of sent acks
void increment_pending_acks() {
while (!flag_.test_and_set()) {
//save the last ack number for this thread so we only
//wait up to this number.
last_ack_[std::this_thread::get_id()] = ++sent_acks_;
flag_.clear();
break;
}
}
这样的实现是很精巧。但个人认为,如果各个线程的返回ACK不是按顺序,则有可能误判。
比如按时序:
1:某个线程的last_acks_[tid1] = 100, 但是received_acks_=99
2:之后某个时刻sent_acks=101, last_acks_[tid2]=101, 此时last_acks_[tid1] = 100不变。
3:tid2收到了应答,received_acks_=100。此时检查tid1的get_current_thread_pending_acks_impl会返回true。