循环在C中循环时,通用CPU非阻止方式

发布于 2025-01-18 21:32:20 字数 292 浏览 2 评论 0 原文

我会问一个非常通用的问题,使我困惑了一段时间。 我们经常不得不等待非块功能的结果,例如从文件,套接字或设备读取的字节。

为什么要浪费非阻塞功能的神灵封锁?

因此,至少在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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2025-01-25 21:32:20

我终于找到了问题的答案,感谢 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' is sched_yield(): https://man7.org/linux/man-pages/man2/sched_yield.2.html

I'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.

墨小墨 2025-01-25 21:32:20

你不需要这个。这已经就是阻塞的作用。当您的进程被阻塞时,调度程序会自动运行下一个未被阻塞的作业/进程/线程。

如果您编写这样的循环,唯一的区别是调度程序认为您的线程没有被阻塞,并且它将不断切换回您的线程,然后您的线程将不断切换回原来的线程。调度程序-浪费时间。

最好使用实际阻塞,然后调度程序就会知道您的线程被阻塞,并且在其解除阻塞之前不会运行它。

为什么要在阻塞期间浪费非阻塞函数的天赐之物?

你不知道。为了充分利用非阻塞函数,您必须以非阻塞方式编写整个程序,可能每个连接使用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.

Why waste the godsend of non-blocking functions for a blocking while?

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 like epoll to see which connections have new data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文