简体   繁体   中英

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. 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. 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:

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

int main()
{

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

works after flushing the output buffer

#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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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