jeudi 24 juin 2021

How to ensure UINT64 does not overflow

I currently have something like this in my code

std::atomic<UINT64> counter;

I am doing this in a certain section of the code.

counter++

The portion of the code where this is done is accessed by multiple threads. My understanding is that if the program runs constantly at one point counter++ will overflow. Is there any option for me to make it go back to 0 or not overflow/invalidate by itself. or do I have to do something like this

if(counter == std::numeric_limits<UINT64>::max()-1) {
   counter = 0;
} 

Aucun commentaire:

Enregistrer un commentaire