在C睡觉,我应该同时使用同一个时钟,或系统调用? [英] To sleep in C, should I use while with a clock, or a system call?

查看:174
本文介绍了在C睡觉,我应该同时使用同一个时钟,或系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被检查出时钟()上cplusplus.com 。其例子包括使过程等待一个第二,然后输出一条线,在一个循环中,直到10秒钟ellapsed。我需要做在我工作的家庭作业类似的东西。我想知道是这样的:如果我只是把我的程序进入一个循环,这不就是利用系统资源就冒我身边?在这种情况下,也不会在系统调用更好?

I was checking out clock() on cplusplus.com. Their example involves making the process wait for a second and then output a line, in a loop until 10 seconds have ellapsed. I need to do something similar in the homework assignment I'm working on. What I was wondering was this: if I just send my program into a loop, isn't that using system resources to just spin me around? In this case, wouldn't a system call be better?

感谢您的帮助!

推荐答案

是的,使用系统调用库函数如睡眠()要好得多。这时钟()例子只是为了只是显示如何正确地调用函数和跨preT它的返回值,而不是说明,使程序的最佳方式等待。

Yes, using a system call or library function like sleep() is much better. That clock() example is just meant to just show how to correctly call the function and interpret its return value, not to illustrate the best way to make a program wait.

这篇关于在C睡觉,我应该同时使用同一个时钟,或系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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