My application runs on a pSOS operating system. The code is compiled with Diab C compiler.
The application defines a number of counters which have been declared as
unsigned int call_count;
As there are chances of some of these overflowing in a small time frame, I have decided to declare the counters as
unsigned long long int call_count;
This I believe would not overflow at least during my lifetime.
My question is this conversion harmless? Are there any overhead that I need to concerned with. When the application is under stress the call_count would be incremented incessantly. Can performance take a hit ? A SNMP manager would be querying these counters every 15 seconds as well.
Is your code assuming that incrementing a 32-bit variable is an atomic operation? Incrementing a 64-bit variable on a 32-bit CPU probably won’t be atomic unless you go out of your way to make it so.
Example:
call_countequals0x00000005FFFFFFFFwhen a call comes in.call_countis incremented:call_countgets set to0x000000500000000and the CPU’s carry bit gets set to 1.call_countis incremented by the carry bit:call_countgets set to0x0000000600000000.If another thread or an interrupt handler reads the value of
call_countbetween steps 2 and 3, it will get the wrong result (0x000000500000000instead of0x000000600000000). The solution is to synchronize access tocall_count. A few possibilities:InterlockedIncrement()on Windows)