Threading newbie with running thread issue

Well, you understand that I will explain the problem: I use a library called ClanLIB (not my choice), this library, SEEMLY (I'm not sure even reading sourcE), creates a stream that processes the sound.

This stream, when the buffer is empty, tries to get more data, usually it is perplexing when the data generation library is too slow to provide more data before the sound card reaches the end of the buffer.

So, I added my own stream, which continues to generate sound in the background.

This worked fine, except that my own thread sometimes took up too much CPU time and slowed down everything else. To fix this, I added conditional wait.

Conditional wait occurs when the buffer is full, and when ClanLIB requests more data, the wait signaling is signaled, so the buffer write stream resumes (until it is full again).

My problem is that since I added this conditional wait, ClanLIB sound stream and my own music stream, SOMETIMES get a runaway playing music while the rest of the application freezes.

What strange condition can cause this?

pseudo code:

//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()
}
+3
source share
4 answers

Actually, I don’t know what the problem is, it was fixed by accident ...

, ... ( - ), . ! (, , )

0

- , cond1. , , - , - , - condition_wait mutex, ?

" " , , - , , , , .

+1

clanlib , OpenAL . , , ..

, . . ;)

+1

SDK ClanLib, , SDK. , , , , .

, , , , , added my own thread, that keeps generating sound on the background. , , , , ClanLib, , .

, , . .

, , , , - , ClanLib forum ?


Edit: what makes you sure it's a subversion? I still find it hard to believe, but if you cna find this in your program, then why not sleep(), and not generate sound? (how long to sleep? how do you know how much sound you need to generate?) and does the sound created by the sound affect the “real” sound?)

0
source

Source: https://habr.com/ru/post/1759236/


All Articles