Call function periodically without using threads and sleep() method in c

前端 未结 5 1656
离开以前
离开以前 2021-01-22 11:53

I want to call a function, lets say every 10 or 20 seconds. When I searched, I came up with threads and sleep() method everywhere.

I also checked for time

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 12:23

    Simple:

    #include 
    #include 
    
    int main(int argc, const char** argv)
    {
        while(1)
        {
            usleep(20000) ;
            printf("tick!\n") ;
        }
    }
    

    Note that usleep() will of course block :)

提交回复
热议问题