What's the principle of blocking mode?
Like blocks until the file is done playing, what's the principle and how to implement thi开发者_JS百科s?
"blocking" means that the operation will not return control to its caller until whatever it's "blocking until" is true.
This can be implemented in several ways:
- Delegate the responsibility for blocking to someone else. For example, call
pthread_mutex_lock
, which may block. This makes your function block too. Other functions doing this areread
and any other system call which says it may block. - Spin. In other words, have some code that looks like
while (!condition) {}
. This will eat an entire CPU core, so it's not a good practice if you're going to be blocking for any significant amount of time. - Use a signal handler. Call
sleep(5000)
or some such, and terminate the sleep viaSIGALARM
or another asynchronous method.
In the case of a media player, "blocking until the file is done playing" just means "waits until the media file is done playing before returning".
let a thread wait for an event which will be fired by another thread when file is done playing.
精彩评论