简体   繁体   English

POSIX C/C++ sleep() 和 usleep() 不工作? (树莓派)

[英]POSIX C/C++ sleep() and usleep() not working? (Raspberry PI)

I wrote a small programm on my RasPI and have trouble with the sleep() and usleep() functions.我在我的 RasPI 上写了一个小程序,但在使用sleep()usleep()函数时遇到了问题。 both of them don't work.他们两个都不工作。 When I use usleep() with a number below 1000000 (below 1 second) it works, whenever i try to use a number that should let the program sleep for 1 second or more, it doesn't work.当我使用usleep()和一个低于 1000000(低于 1 秒)的数字时它起作用,每当我尝试使用一个应该让程序休眠 1 秒或更长时间的数字时,它不起作用。 I've been working on making the Digital pin HIGH for a given time.我一直在努力让数字引脚在给定时间内保持高电平。

I've tried to shrink the program to printf() and to sleep only:我试图将程序缩小到printf()并只睡觉:

#include <stdio.h>
#include <unistd.h>

int main()
{

    while (true)
    {
        sleep(1);
        printf("%.2f", 10.1);
    }
}

works after flushing the output buffer刷新 output 缓冲区后工作

#include <stdio.h>
#include <unistd.h>
#include <chrono>
#include <thread>


int main()
{

    while (true)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        printf("%.2f\n", 10.1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM