开发者

What's the principle of blocking mode?

开发者 https://www.devze.com 2023-01-11 07:19 出处:网络
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\'

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 are read 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 via SIGALARM 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消