2

I want to know if there is anyway to make the current thread to sleep for a particular interval of time in C. I have lots of files in the project and want only one function in the file to sleep for particular time while can access other functions in the same file. Something like below i want to do -

void otherfunction() <-- other programs should be able to access otherfunction() when somefunction() is sleeping.

   void somefunction()
   {
       //sleep for 2sec.
   }
user7070
  • 21
  • 3
  • 2
    possible duplicate of [pthread sleep linux](http://stackoverflow.com/questions/3633089/pthread-sleep-linux) –  Jun 10 '14 at 11:54

2 Answers2

2

The POSIX function for sleeping is called sleep(). You call it with the number of seconds as a parameter.

sth
  • 222,467
  • 53
  • 283
  • 367
0

This is a microseconds sleep function , for cross-platform sleeping:

void MicroSleep(int n) {
    #ifdef WIN32
        ::Sleep(n);
    #else
        struct timeval tm;
        unsigned long seconds = n/1000;
        unsigned long useconds = n%1000;
        if (useconds > 1000) { useconds -= 1000; seconds++; }
        useconds*=1000; // using usec
        tm.tv_sec = seconds;
        tm.tv_usec = useconds;
        select(0, 0, 0, 0, &tm);
    #endif
}
Leonardo Bernardini
  • 1,076
  • 13
  • 23