我会问一个非常通用的问题,使我困惑了一段时间。
我们经常不得不等待非块功能的结果,例如从文件,套接字或设备读取的字节。
为什么要浪费非阻塞功能的神灵封锁?
因此,至少在POSIX API中可以做一种,而(!sothereToread())forcetopassonextonextondjobinscheduler();
?
我在任何地方都找不到有关此信息的信息,限制线程的使用会很漂亮。
我没有发现任何东西向我暗示我没有使用正确的关键字,或者没有什么类似的事实。但这将是很棒的,我希望。
I would ask a very generic question that has been perplexing me for some time.
Very, very often we have to wait for the results from non-blocking functions, for example bytes to read from a file, socket or device.
Why waste the godsend of non-blocking functions for a blocking while?
So, is it possible, at least in POSIX API, to do a sort of while(!somethingToRead()) forceToPassToNextJobInScheduler();
?
I can't find information about this anywhere, and it would be beautiful to limit the use of threads.
The fact that I have not found anything suggests to me that either I have not used the right keywords or that nothing similar is possible. But it would be wonderful, I hope.
发布评论
评论(2)
我终于找到了问题的答案,感谢 libvpoll 背后的研究人员:我正在谈论的 '
forceToPassToNextJobInScheduler
' 的确切函数是sched_yield()
: https://man7.org/linux/man-pages/man2/sched_yield.2.html我已经非常接近解决方案了,打开了一个关于 sched 函数的选项卡:https://man7.org/linux/man-pages/man7/sched.7.html
但作为一个低级 POSIX 专家,很难轻松找到正确的解决方案。
I finally found the answer to the question thanks the researcher behind libvpoll: the exactly function that I'm talking about with '
forceToPassToNextJobInScheduler
' issched_yield()
: https://man7.org/linux/man-pages/man2/sched_yield.2.htmlI'm pretty gone near to the solution, having open a tab aboust sched functions: https://man7.org/linux/man-pages/man7/sched.7.html
But as not a low-level POSIX expert, it's difficult to find easily the right solution.
你不需要这个。这已经就是阻塞的作用。当您的进程被阻塞时,调度程序会自动运行下一个未被阻塞的作业/进程/线程。
如果您编写这样的循环,唯一的区别是调度程序认为您的线程没有被阻塞,并且它将不断切换回您的线程,然后您的线程将不断切换回原来的线程。调度程序-浪费时间。
最好使用实际阻塞,然后调度程序就会知道您的线程被阻塞,并且在其解除阻塞之前不会运行它。
你不知道。为了充分利用非阻塞函数,您必须以非阻塞方式编写整个程序,可能每个连接使用
struct
而不是 em> 一个线程,以及类似epoll
查看有哪些连接新数据。You don't need this. This is already what blocking does. When your process is blocked, the scheduler automatically runs the next job/process/thread that is not blocked.
If you write a loop like this, the only difference will be that the scheduler thinks your thread isn't blocked, and it will keep switching back to your thread and then your thread will keep switching back the the scheduler - a waste of time.
Better to use actual blocking, and then the scheduler will know your thread is blocked, and won't run it until it's unblocked.
You don't. To make good use of non-blocking functions you have to write your entire program in a non-blocking way, probably using a
struct
per connection instead of a thread, and something likeepoll
to see which connections have new data.