Hi,
Our code scanner has reported a data race at workLoop
method of HeartbeatClient
bool HeartbeatClient::start() {
int err = pthread_create(&_thread, NULL, &HeartbeatClient::workLoop, this);
if (err != 0) {
LOG(ERROR) << "Cannot start thread for heartbeat client";
return false;
}
_running = true;
LOG(INFO) << "Start sending heartbeats to '" << _remoteHost << ":" << _remotePort << "'";
return true;
}
_running
initialized with false
,
Could there is a chance that HeartbeatClient::workLoop access the _running
before the main thread has assigned true
into, the heart beat thread might just stopped before the main thread has updated the value of _running
.
May be we could move the assignment into thread body to avoid this issue.
void* HeartbeatClient::workLoop(void *arg) {
......
// client->_running = true; // <====== move here instead
while (client->_running) {
Regards,
Alex, SourceBrella Inc.