不久前,我有一个密切相关的主题here。
但是,将cin.ignore()
替换为usleep(50e3)
。它不会每50毫秒报告一次。时钟报告
Time Passed: s: 0 ms: 50
Time Passed: s: 0 ms: 101
Time Passed: s: 0 ms: 152
Time Passed: s: 0 ms: 202
Time Passed: s: 0 ms: 252
Time Passed: s: 0 ms: 303
Time Passed: s: 0 ms: 353
Time Passed: s: 0 ms: 403
Time Passed: s: 0 ms: 454
Time Passed: s: 0 ms: 504
Time Passed: s: 0 ms: 554
Time Passed: s: 0 ms: 605
Time Passed: s: 0 ms: 657
Time Passed: s: 0 ms: 708
Time Passed: s: 0 ms: 758
Time Passed: s: 0 ms: 808
Time Passed: s: 0 ms: 862
Time Passed: s: 0 ms: 915
Time Passed: s: 0 ms: 965
Time Passed: s: 1 ms: 15
Time Passed: s: 1 ms: 66
Time Passed: s: 1 ms: 116
Time Passed: s: 1 ms: 169
Time Passed: s: 1 ms: 221
Time Passed: s: 1 ms: 271
Time Passed: s: 1 ms: 322
Time Passed: s: 1 ms: 372
Time Passed: s: 1 ms: 423
Time Passed: s: 1 ms: 473
Time Passed: s: 1 ms: 524
Time Passed: s: 1 ms: 574
Time Passed: s: 1 ms: 625
Time Passed: s: 1 ms: 676
Time Passed: s: 1 ms: 727
Time Passed: s: 1 ms: 778
Time Passed: s: 1 ms: 833
Time Passed: s: 1 ms: 883
Time Passed: s: 1 ms: 935
Time Passed: s: 1 ms: 986
Time Passed: s: 2 ms: 37
Time Passed: s: 2 ms: 91
Time Passed: s: 2 ms: 142
Time Passed: s: 2 ms: 192
Time Passed: s: 2 ms: 243
Time Passed: s: 2 ms: 294
Time Passed: s: 2 ms: 346
Time Passed: s: 2 ms: 396
Time Passed: s: 2 ms: 450
Time Passed: s: 2 ms: 501
Time Passed: s: 2 ms: 552
Time Passed: s: 2 ms: 602
Time Passed: s: 2 ms: 652
Time Passed: s: 2 ms: 703
Time Passed: s: 2 ms: 753
Time Passed: s: 2 ms: 804
Time Passed: s: 2 ms: 855
Time Passed: s: 2 ms: 906
Time Passed: s: 2 ms: 956
Time Passed: s: 3 ms: 7
Time Passed: s: 3 ms: 57
Time Passed: s: 3 ms: 107
Time Passed: s: 3 ms: 158
Time Passed: s: 3 ms: 208
Time Passed: s: 3 ms: 258
Time Passed: s: 3 ms: 315
Time Passed: s: 3 ms: 365
Time Passed: s: 3 ms: 416
Time Passed: s: 3 ms: 466
Time Passed: s: 3 ms: 517
Time Passed: s: 3 ms: 567
Time Passed: s: 3 ms: 618
Time Passed: s: 3 ms: 668
Time Passed: s: 3 ms: 719
Time Passed: s: 3 ms: 769
Time Passed: s: 3 ms: 824
Time Passed: s: 3 ms: 875
Time Passed: s: 3 ms: 926
Time Passed: s: 3 ms: 976
Time Passed: s: 4 ms: 27
Time Passed: s: 4 ms: 79
Time Passed: s: 4 ms: 129
Time Passed: s: 4 ms: 182
Time Passed: s: 4 ms: 233
Time Passed: s: 4 ms: 283
Time Passed: s: 4 ms: 334
Time Passed: s: 4 ms: 384
Time Passed: s: 4 ms: 436
Time Passed: s: 4 ms: 486
Time Passed: s: 4 ms: 537
Time Passed: s: 4 ms: 587
Time Passed: s: 4 ms: 638
Time Passed: s: 4 ms: 688
Time Passed: s: 4 ms: 739
Time Passed: s: 4 ms: 789
Time Passed: s: 4 ms: 844
Time Passed: s: 4 ms: 895
Time Passed: s: 4 ms: 946
Time Passed: s: 4 ms: 997
Time Passed: s: 5 ms: 47
Time Passed: s: 5 ms: 101
Time Passed: s: 5 ms: 151
Time Passed: s: 5 ms: 202
Time Passed: s: 5 ms: 252
Time Passed: s: 5 ms: 303
Time Passed: s: 5 ms: 354
Time Passed: s: 5 ms: 404
Time Passed: s: 5 ms: 455
如您所见(很抱歉,但可能会输出很长的时间,但以防万一),始终存在不准确之处。
为什么会这样,对它有什么影响?如何解决?
最佳答案
当您的过程通过调用sleep
函数进入睡眠状态时,系统调度程序将其延迟唤醒,直到花费了所请求的时间。之后,它可能会唤醒该过程,或者可能需要更长的时间。
许多操作系统不保证在睡眠时间结束后立即唤醒进程。