I have a thread, which keeps running, and this thread receives data from udp network. when there is no data coming in for lets say 3 seconds, i am setting a atomic variable[STATUS] as 0 and if data started coming again setting atomic variable[STATUS] value is 1
code below:
ProtoApiManager.hpp
------------------
// to update computer status
atomic_int STATUS = ATOMIC_VAR_INIT(0);
ProtoApiManager.cpp
------------------
_keep_running = true;
_t_bg_tasks = std::thread(&ProtoApiManager::onThreadBgTasks, this);
void main::onThreadBgTasks()
{
do
{
milliseconds currentTime = MiscUtils::getTimeNow();// getting current time
auto delta = currentTime - _last_status_msg_rx; // assigned when data received
if (delta.count() > _NAV_COMMS_TIMEOUT) // constant assigned as 3 sec
{
std::cout<<"Time out OFFLINE"<<std::endl;
MC_COMPUTER_STATUS = 0;
// prepareData()
}else{
std::cout<<"Time out ONLINE"<<std::endl;
MC_COMPUTER_STATUS = 1;
// prepareData()
}
}
while(_keep_running);
}
---------------
ProtoApiManager.process.cpp
---------------------------
ProtoApiManager::prepareData()
{
// preparing data
hardwareItemStatusMCComputer->set_hardwaretype(10);
hardwareItemStatusMCComputer->set_hardwarename("MC Computer");
hardwareItemStatusMCComputer->set_hardwarestatus(STATUS);
// transmitting to udp
}
---------------------------
Now the question is, as thread is keep running in do while, and based on time out i set atomic variable value 0 or 1, i have to call prepareData method outside thread based on atomic variable[STATUS] and set [status] while preparing data
Aucun commentaire:
Enregistrer un commentaire