So recently I started thinking about a way of improving speed (vs cpu usage) in a program which is extensible by DLL’s which you can built with the provided SDK (Software Development Kit) in C++.
I started doing research on threading data and then writing to global variables, where – if needed – the main thread would just call a variable, and not execute a whole function (the thread does the stuff).
I was actually surprised by my results, because I expected the thread to crash the application – however it didn’t.
I started a discussion on the application’s developer forum – we got to the point that stated:
“The rule for threads is: If it crashes, it’s wrong, if it doesn’t crash, it’s probably still wrong.” and:
“Actually, that code probably won’t crash, but it can corrupt variables. I’ve heard stories of multi-threaded programs running for months with no issues before a race-hazard occurred.”
Well, can ‘GETS’ actually cause race conditions?
The main application (SA-MP , San Andreas Multiplayer) is single threaded, and compiled like that.
The code I used for my tests:
[C++]
#ifdef OS_WINDOWS
void Thread::BackgroundCalculator( void *unused )
#else
void *Thread::BackgroundCalculator( void *unused )
#endif
{
float X;
float Y;
float Z;
while( true )
{
if(ENABLED == false)
{
continue;
}
for(int i = 0; i < MAX_PLAYERS; ++i)
{
if(IsPlayerConnected(i) == false)
{
speed[i] = -1.0f;
continue;
}
if(IsPlayerInAnyVehicle(i) == true)
{
GetVehicleVelocity(GetPlayerVehicleID(i),&X,&Y,&Z);//Is actually used by MAIN APPLICATION thread too... so should cause race condition?
}
else
{
GetPlayerVelocity(i,&X,&Y,&Z);//Is actually used by MAIN APPLICATION thread too... so should cause race condition?
}
speed[i] = sqrt(X*X+Y*Y+Z*Z);//called from my code.. thread
}
SLEEP(30);
}
EXIT_THREAD();//should be never reached..
}
static cell AMX_NATIVE_CALL n_GetSpeed( AMX* amx, cell* params )
{
return amx_ftoc(speed[params[1]]);//returning to main thread, ftoc = FloatToCell
}
and when calling VERY MANY TIMES “n_GetSpeed” from the virtual machine (.amx file). It doesn’t crash.
I also tried calling GetPlayerVelocity/GetVehicleVelocity from the main thread and my thread as much as possible at the same time. and still doesn’t crash. totally not what I’d expect.
So, when we got to the point where somebody said a race-condition will occur, I would like to know how to cause one (force a race condition?). (in C/C++ ofcourse)
__
The code I am making is always open-source and available from my page, this too:
http://gpb.googlecode.com/files/ThreadTest_R100.zip
Just if you need the whole code 😉
__
Extra note: the thing is.. That I only access variables, not change, the only thing I change is the speed variable and it’s ONLY done in the second thread.
Without knowing much about the application you are modifying. It could potentially cause a crash.
You say that you only read, so everything should be fine. BUT, the main thread (the original program) will most definitely change the variables. One thing that could happen is that after you call IsPlayerInAnyVehicle and it returns TRUE, it changes to FALSE, or the vehicle changes, or the player dies, or the player quits, … Your call to GetVehicleVelocity, GetPlayerVehicleID or GetPlayerVelocity might therefore be called under erroneous circumstances. Will this lead to a crash? Who knows. But at least, the code is not 100% healthy.
EDIT: It is impossible to know whether this code is thread-safe without knowing how the methods you call are implemented. Put weak memory ordering into the game and all bets are off, without proper memory barriers.