在c中运行一段时间的无限循环

发布于 2024-09-29 04:56:31 字数 174 浏览 7 评论 0原文

我想无限循环运行一段时间。基本上,我想要这样的东西

//do something

while(1){
  //do some work
}

//do some other thing

,但我希望循环的运行时间是固定的,例如,循环可以运行 5 秒。 有人有主意吗?

I want to run an infinite loop for a while. Basically, i want to have something like this

//do something

while(1){
  //do some work
}

//do some other thing

but i want the running time of the loop to be fixed, example, the loop could be running for 5 seconds.
Do somebody have an idea?

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

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

发布评论

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

评论(6

水溶 2024-10-06 04:56:31

只需执行 sleep(5) (包括 unistd.h)。您可以这样使用它:

// do some work here
someFunction();    

// have a rest
sleep(5);

// do some more work
anotherFunction();

如果您在循环内工作,您可以执行以下操作(包括 time.h):

// set the end time to the current time plus 5 seconds
time_t endTime = time(NULL) + 5;

while (time(NULL) < endTime)
{
    // do work here.
}

Just do sleep(5) (include unistd.h). You can use it like this:

// do some work here
someFunction();    

// have a rest
sleep(5);

// do some more work
anotherFunction();

If you're doing work inside the loop, you can do (include time.h):

// set the end time to the current time plus 5 seconds
time_t endTime = time(NULL) + 5;

while (time(NULL) < endTime)
{
    // do work here.
}
何必那么矫情 2024-10-06 04:56:31

尝试使用时钟()。

#include <time.h>

clock_t start = clock();

while (1)
{
    clock_t now = clock();
    if ((now - start)/CLOCKS_PER_SEC > 5)
        break;

    // Do something
}

Try using clock().

#include <time.h>

clock_t start = clock();

while (1)
{
    clock_t now = clock();
    if ((now - start)/CLOCKS_PER_SEC > 5)
        break;

    // Do something
}
瑾夏年华 2024-10-06 04:56:31

首先,如果可能的话,考虑使用 sleep 函数。如果您必须在指定的时间段内完成实际工作(我认为这不太可能),则以下丑陋的解决方案将起作用:

#include <signal.h>
int alarmed = 0;
void sigh(int signum) {
    alarmed = 1;
}
int main(void){
    /* ... */
    signal(SIGALRM, &sigh);
    alarm(5); // Alarm in 5 seconds
    while(!alarmed) {
        /* Do work */
    }
    /* ... */
}

使用 time.h 的解决方案也是可能的,并且可能更简单和/或更多种准确,取决于上下文:

#include <time.h>
int main(void){
    /* ... */
    clock_t start = clock();
    while(clock() - start < 5 * CLOCKS_PER_SEC) {
        /* Do work */
    }
    /* ... */
}

First of all, consider using the sleep function if possible. If you have to do actual work for a specified time period, which I find unlikely, the following ugly solution would work:

#include <signal.h>
int alarmed = 0;
void sigh(int signum) {
    alarmed = 1;
}
int main(void){
    /* ... */
    signal(SIGALRM, &sigh);
    alarm(5); // Alarm in 5 seconds
    while(!alarmed) {
        /* Do work */
    }
    /* ... */
}

A solution using time.h would also be possible, and perhaps simpler and/or more accurate, depending on context:

#include <time.h>
int main(void){
    /* ... */
    clock_t start = clock();
    while(clock() - start < 5 * CLOCKS_PER_SEC) {
        /* Do work */
    }
    /* ... */
}
只怪假的太真实 2024-10-06 04:56:31

伪代码:

starttime = ...;

while(currentTime - startTime < 5){

}

Pseudo-code:

starttime = ...;

while(currentTime - startTime < 5){

}
溺渁∝ 2024-10-06 04:56:31

如果您不想每次通过循环调用时间获取函数并且在具有警报的系统上(POSIX,如 Unix、Linux、BSD...),您可以执行

以下操作 :整数超时= 0;

void handle_alrm(int sig) {
     timeout = 1;
}

int main(void) {
    signal(SIGALRM, handle_alrm);
    ...
    timeout = 0;
    alarm(5);
    while (!timeout) {
       do_work();
    }
    alarm(0); // If the signal didn't fire yet we can turn it off now.
    ...

信号可能会产生其他副作用(例如将您踢出系统调用)。在依赖它们之前,您应该先研究一下它们。

If you don't want to call a time getting function each time through the loop and are on a system that has alarm (POSIXes like Unix, Linux, BSD...) you can do:

static volatile int timeout = 0;

void handle_alrm(int sig) {
     timeout = 1;
}

int main(void) {
    signal(SIGALRM, handle_alrm);
    ...
    timeout = 0;
    alarm(5);
    while (!timeout) {
       do_work();
    }
    alarm(0); // If the signal didn't fire yet we can turn it off now.
    ...

Signals can have other side effects (like kicking you out of system calls). You should look into these before relying on them.

巷子口的你 2024-10-06 04:56:31

未经测试;分辨率非常粗糙。

#include <time.h>
#define RUNTIME 5.0 /* seconds */

double runtime = 0;
double start = clock(); /* automatically convert clock_t to double */
while (runtime < RUNTIME / CLOCKS_PER_SEC) {
    /* work */
    runtime = clock() - start;
}

如果 /* work */ 花费超过 5 秒,则循环将花费超过 5 秒。

如果 /* work */ 需要 1.2 秒,则循环将执行大约 5 次,总共 6 秒

Not tested; resolution is very coarse.

#include <time.h>
#define RUNTIME 5.0 /* seconds */

double runtime = 0;
double start = clock(); /* automatically convert clock_t to double */
while (runtime < RUNTIME / CLOCKS_PER_SEC) {
    /* work */
    runtime = clock() - start;
}

If /* work */ takes more than 5 seconds, the loop will take more than 5 seconds.

If /* work */ takes 1.2 seconds, the loop will execute approximately 5 times for a total of 6 seconds

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