What are trade offs for “busy wait” vs “sleep”?

前端 未结 6 1603
轻奢々
轻奢々 2020-12-02 10:15

This is an extension to my previous question

How does blocking mode in unix/linux sockets works?

What I gather from Internet now, all the process invoking bl

6条回答
  •  无人及你
    2020-12-02 10:38

    If in the use case of your application, context switching would be more expensive than eating a few CPU cycles because your condition would be guaranteed to get satisfied within a short time, then busy waiting may be good for you.

    Otherwise, you can forcefully relinquish the CPU by sleeping or cond_wait()ing.

    Another scenario that I can think of for forceful context switching out is as follows:

    while(condition)
        sleep(0);
    

提交回复
热议问题