C ++-Sleep()和cin如何工作? [英] C++ - how does Sleep() and cin work?

查看:125
本文介绍了C ++-Sleep()和cin如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很好奇.函数Sleep()实际上是如何工作的(在windows.h中声明)?也许不仅是该实现,而且任何人都可以.我的意思是-如何实施?如何使代码在特定时间停止"?也对cin >>和实际上 的工作方式感到好奇.他们到底在做什么?

Just curious. How does actually the function Sleep() work (declared in windows.h)? Maybe not just that implementation, but anyone. With that I mean - how is it implemented? How can it make the code "stop" for a specific time? Also curious about how cin >> and those actually work. What do they do exactly?

我知道如何阻止"某事物继续运行的唯一方法是使用while循环,但是考虑到与调用标准输入法进行读取时所发生的情况相比,这需要大量的处理能力(只需将一会儿(真)与从stdin读取的内容进行比较),我想那不是他们所做的.

The only way I know how to "block" something from continuing to run is with a while loop, but considering that that takes a huge amount of processing power in comparison to what's happening when you're invoking methods to read from stdin (just compare a while (true) to a read from stdin), I'm guessing that isn't what they do.

推荐答案

操作系统使用一种称为调度程序的机制将其管理的所有线程或进程保持良好的状态.

The OS uses a mechanism called a scheduler to keep all of the threads or processes it's managing behaving nicely together.

每秒几次,计算机的硬件时钟会中断CPU,这会导致操作系统的调度程序被激活.然后,调度程序将查看所有尝试运行的进程,并决定在下一个时间段运行哪个进程.

several times per second, the computer's hardware clock interrupts the CPU, which causes the OS's scheduler to become activated. The scheduler will then look at all the processes that are trying to run and decides which one gets to run for the next time slice.

它用来决定的不同事物取决于每个进程的状态,以及之前有多少时间.因此,如果当前进程一直在大量使用CPU,从而阻止了其他进程的进行,它将使当前进程处于等待状态并在另一个进程中进行交换,以便可以执行一些工作.

The different things it uses to decide depend on each processes state, and how much time it has had before. So if the current process has been using the CPU heavily, preventing other processes from making progress, it will make the current process wait and swaps in another process so that it can do some work.

不过,大多数情况下,大多数进程将处于等待状态.例如,如果某个进程正在等待来自控制台的输入,则OS可以查看进程信息并查看其等待的io端口.它可以检查那些端口,以查看它们是否有任何数据可用于该进程.如果这样做,它可以再次启动该过程,但是如果没有数据,则该过程将跳过当前的时间片.

More often, though, most processes are going to be in a wait state. For instance, if a process is waiting for input from the console, the OS can look at the processes information and see which io ports its waiting for. It can check those ports to see if they have any data for the process to work on. If they do, it can start the process up again, but if there is no data, then that process gets skipped over for the current timeslice.

任何进程都可以通知OS它想要等待一段时间.然后,即使在发生硬件中断之前,调度程序也将被激活(当进程试图从没有准备好读取数据的流中进行阻塞读取时,也会发生这种情况),并且OS会记录该进程是什么等待.要入睡,该过程正在等待警报响起,或者每次重启时它都会再次发出声音,直到计时器计时到来.

as for sleep(), any process can notify the OS that it would like to wait for a while. The scheduler will then be activated even before a hardware interrupt (which is also what happens when a process tries to do a blocking read from a stream that has no data ready to be read,) and the OS makes a note of what the process is waiting for. For a sleep, the process is waiting for an alarm to go off, or it may just yield again each time it's restarted until the timer is up.

由于OS仅在某种原因导致操作系统抢占正在运行的进程(例如我提到的进程屈服或硬件计时器中断)之后才恢复进程,因此sleep()不太准确,因此准确度取决于OS或硬件,但是通常约为一毫秒或更多毫秒.

Since the OS only resumes processes after something causes it to preempt a running process, such as the process yielding or the hardware timer interrupt i mentioned, sleep() is not very accurate, how accurate depends on the OS or hardware, but it's usually on the order of one or more milliseconds.

如果需要更高的准确性或等待时间很短,唯一的选择是使用您提到的busy loop构造.

If more accuracy is needed, or very short waits, the only option is to use the busy loop construct you mentioned.

这篇关于C ++-Sleep()和cin如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆