• ### Popular Now

• 14
• 12
• 9
• 10
• 9

This topic is 3955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The Intel Thread Checker doesn't seem to like my threading practices. It displays these nice red errors every time it detects a write-read race. For example: One thread is executing this code:
time_elapsed = Timer.GetTimeElapsed();
g_time += time_elapsed;  //time incremented each millisecond
Sleep(1);
float mytime = g_time;
//grab time each frame; every ~17ms

##### Share on other sites
You are claiming these are atomic operations when in fact they are not.

g_time+= time_elapsed;

Memory is probably copied to registers, then registers are added, then flushed back to memory. The thread could be preempted at any point during those instructions.

Look up InterlockedIncrement and InterlockedExchange, which could help you with the case you described.

In terms of accessing variables from objects you have there are several options, the one you described might be overkill, another option would be to double buffer your game objects and synch them on each frame.

##### Share on other sites
Another option would be to use volatile for g_time, I belive on VS 2005 volatile adds memory barries which would also solve your problem.