samedi 30 juillet 2016

How to modify an atomic variable

I have two atomic variables and two threads plus the main thread. One thread continuously receives data from UDP port and stores data into std::atomic<double> m_udpData. The second thread gets m_udpData and stores the data into std::atomic<double> m_delayedData. In the main thread, I need to retrieve m_delayedData. The two threads and atomic variables belong to one class Slave. It seems that I can't assign a value to an atomic variable. Is there a workaround for this problem in a safe manner?

// First thread
void Slave::updateUDPData()
{
    while (true){
        server->recieve(m_udpData);
    }
}

// Second thread
void Slave::updateData()
{
    while (true){
        std::this_thread::sleep_for(m_delay);
        m_delayedData = m_udpData; // <- yields an error. 
    }
}

Aucun commentaire:

Enregistrer un commentaire