Skip to content

Discussion about the necessity of acquiring _mutex in is_leader() #496

@gitccl

Description

@gitccl

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions