I'm using threads for the first time , and im making use of SDL's threading functions which are easy and i've got everything working perfectly...or at least that's what i believe , which is what gave me the motivation to ask if im doing everything properly.
This is a sample unit test which simulates the behaviour of the task by 99,9% :
SomeDynamicBlock** data;
int data_ptr,data_len;
int WorkWorkWork(void* data)
{
unsigned int oldTime = 0;
while (true)
{
if (SDL_GetTicks() - oldTime > 1000)
{
for (int i = 0,j = data_ptr;i < j;i++)
{
if (data)
data->Update(SDL_GetTicks());
}
oldTime = SDL_GetTicks();
}
}
return 0;
}
bool SetupWorker(int size)
{
//Only happens once!
data = new SomeDynamicBlock*[size];
data_ptr = 0;
data_len = size;
for (int i = 0;i < data_len;i++)
data = 0;
sdl_work_thread = CreateThread(WorkWorkWork);
return true;
}
void KillWorker()
{
SDL_KillThread(sdl_work_thread);
delete data;
data = 0;
}
bool AddRef(SomeDynamicBlock* shared)
{
if (data_ptr < data_len)
{
data[data_ptr++] = shared;
return true;
}
for (int i = 0;i < data_ptr;i++)
{
if (!data)
{
data = shared;
return true;
}
}
return false;
}
And these are my questions :
A.What do you think of AddRef ? Shouldn't i suspend the thread at that time ? If so how would i do it ?
B.Is SDL_GetTicks() thread safe ?
Thanks for any help.