Hi all,
I started using SDL2 a while ago and I am trying to figure out why my thread/timer starts most of the time (>95%) but once in a while it does not. I am developing on Windows 8.1 using Msdev 2017.
When the problem occurs, the ‘while (Current_Timer == 0)’ and ‘while (Low_Thread_Tick == 0)’ below never exits (even after waiting several minutes) which would mean that the timer/thread is never called. I don’t see any errors in my log.
Is there something missing/wrong in what I am doing?
Do I need to check for some other errors even if SDL_AddTimer returns a non-zero value and SDL_CreateThread returns a non-NULL pointer?
Here is the code I am using to start it:
SDL_Thread *Low_Thread;
long Low_Thread_Tick;
static int Low(void *ptr)
{
if (SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW))
{
log("Could not set thread priority, err %ld", SDL_GetError());
}
log("thread Low started");
while (1)
{
Low_Thread_Tick++;
}
}
long start_low_thread(void)
{
SDL_Thread *Low_Thread;
Low_Thread_Tick = 0;
Low_Thread = SDL_CreateThread(Low, "Low", NULL);
if (Low_Thread == NULL) {
log("Thread Low failed to start: %s\n", SDL_GetError());
return 0;
} else {
// Wait for the thread to start
while (Low_Thread_Tick == 0)
log("tick: %d", Low_Thread_Tick);
}
return 1;
}
Uint32 Timer_Proc(Uint32 interval, void *param)
{
Current_Timer++;
return interval;
}
void another_function()
{
Current_Timer = 0;
Uint32 delay = 10;
Update_Timer_ID = SDL_AddTimer(delay, (SDL_TimerCallback)Timer_Proc, NULL);
if (Update_Timer_ID == 0)
{
log("Timer did not start, error: %s", SDL_GetError());
}
while (Current_Timer == 0)
{
log(" Waiting for timer to increment (CPU time stamp %lf)", read_cpu_time_stamp());
}
}