Ok, to you understand I will explain the problem: I am using a library called ClanLIB (not my choice), that library, SEEMLY (I am not certain, even reading the sourcE), creates a thread that handles sound.
This thread, when the buffer is empty, tries to fetch more data, usually this cause a underrun when the data generating library is too slow to give more data before the soundcard reaches the buffer end.
So I added my own thread, that keeps generating sound on the background.
This worked fine, except that my own thread sometimes hijacked too much CPU time and froze everything else. To fix it, I added a conditional wait.
The conditional wait happens when the buffer is full, and when ClanLIB ask for more data, the wait is signaled, thus the buffer write thread resumes (until it is full again).
My issue is that since I added this conditional wait, the ClanLIB sound thread, and my own music thread, SOMETIMES get "runaway", playing music while the rest of the application freezes.
What sort of strange condition would cause that?
pseudocode:
//main thread (that get frozen)
start_sound_thread();
do_lots_of_stuff();
quit();
//Sound Thread:
While(true)
{
play(buffer);
if(buffer_empty)
{
mutex.lock()
buffer = buffer2;
if(buffer2_full)
{
signal(cond1);
buffer2_full = false;
}
mutex.unlock()
}
}
//Music Library Thread:
while(true)
{
mutex.lock()
if( check_free_space(buffer2) == 0)
{
buffer2_full = true;
condition_wait(cond1);
}
write_music(buffer2);
mutex.unlock()
}