braft icon indicating copy to clipboard operation
braft copied to clipboard

Discussion about the necessity of acquiring `_mutex` in `is_leader()`

Open gitccl opened this issue 7 months ago • 0 comments

Hi, I'm reading the braft source code and came across the following implementation of is_leader():

bool is_leader() {
    BAIDU_SCOPED_LOCK(_mutex);
    return _state == STATE_LEADER;
}

From the code, it seems that is_leader() only performs a simple read of the _state member. I’m wondering whether it is necessary to acquire the _mutex lock in this case. Could it be acceptable or more efficient to use atomic operations for _state instead, given that is_leader() is likely called frequently in some scenarios?

gitccl avatar Jun 09 '25 10:06 gitccl