20

I am using C++ in Ubuntu with codeBlocks, boost 1.46 in GCC 4.7 [ yield_k.hpp ]

I get this compile time error:

error : Sleep was not declared in this scope

Code:

#include <iostream>
using namespace std;
int main() { 
  cout << "nitrate";
  cout << flush;
  sleep(1000);
  cout << "firtilizers";
  return 0;
}

How do I resolve this error? I want the program to hang for 1 second.

12
  • 12
    You have to write #include <unistd.h>, with the angled brackets.
    – Kerrek SB
    Jun 11, 2012 at 7:53
  • @KerrekSB shouldn't he get an error on the #include line if those were missing?
    – daramarak
    Jun 11, 2012 at 7:57
  • @daramarak, The only sure thing is "nothing worked". There could have been an error on both lines. Not exactly sure how boost plays into this either. It's just a mention, not a boost-related question.
    – chris
    Jun 11, 2012 at 7:58
  • 1. Shouldn't sleep() be with a lowercase s? 2. I guess the expected initialized before 'sleep' probably means that you forgot a semicolon in one of the lines before sleep()...
    – Eitan T
    Jun 11, 2012 at 8:06
  • I included #include <unistd.h> dint work, and I also changed Sleep() to sleep()
    – Mahika
    Jun 11, 2012 at 8:08

5 Answers 5

28

Sleep is a Windows function.

For Unix, look into using nanosleep (POSIX) or usleep (BSD; deprecated).

A nanosleep example:

void my_sleep(unsigned msec) {
    struct timespec req, rem;
    int err;
    req.tv_sec = msec / 1000;
    req.tv_nsec = (msec % 1000) * 1000000;
    while ((req.tv_sec != 0) || (req.tv_nsec != 0)) {
        if (nanosleep(&req, &rem) == 0)
            break;
        err = errno;
        // Interrupted; continue
        if (err == EINTR) {
            req.tv_sec = rem.tv_sec;
            req.tv_nsec = rem.tv_nsec;
        }
        // Unhandleable error (EFAULT (bad pointer), EINVAL (bad timeval in tv_nsec), or ENOSYS (function not supported))
        break;
    }
}

You will need <time.h> and <errno.h>, available in C++ as <ctime> and <cerrno>.

usleep is simpler to use (just multiply by 1000, so make it an inline function). However, it's impossible to guarantee that that sleeping will occur for a given amount of time, it's deprecated, and you need to extern "C" { }-include <unistd.h>.

A third choice is to use select and struct timeval, as seen in http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204 (this is how wine emulates Sleep, which itself is just a wrapper for SleepEx).

Note: sleep (lowercase 's'), whose declaration is in <unistd.h>, is not an acceptable substitute, since its granularity is seconds, coarser than that of Windows' Sleep (uppercase 's'), which has a granularity of milliseconds.

Regarding your second error, ___XXXcall is a MSVC++-specific token (as are __dllXXX, __naked, __inline, etc.). If you really need stdcall, use __attribute__((stdcall)) or similar to emulate it in gcc.

Note: unless your compile target is a Windows binary and you're using Win32 APIs, use of or a requirement for stdcall is A Bad Sign™.

4
  • Thanks moshbear, I fixed it by changing 'Sleep' to 'sleep' and since I needed to use stdcall, is used your solution __attribute__((stdcall)). Thanks
    – Mahika
    Jun 11, 2012 at 9:24
  • 1
    sleep does seconds, not milliseconds; you'd be better off writing a select loop as is done in wine (select is also supported by Winsock) or copy pasting the my_sleep. nanosleep is a bit more elegant than select, IMO. Don't forget the #ifndef _WINDOWS or equivalent.
    – moshbear
    Jun 11, 2012 at 10:06
  • 1
    @timothy: You shouldn't need __attribute__((stdcall)) to call POSIX functions. Jul 4, 2013 at 6:22
  • That winehq source code, the line number might be out of date, try this:
    – pilkch
    Jun 6, 2017 at 3:14
17

How to use usleep in a C++ program on linux:

Put this in a file called s.cpp

#include <iostream>
#include <unistd.h>
using namespace std;
int main() { 
  cout << "nitrate";
  cout << flush;
  usleep(1000000);
  cout << "firtilizers";
  return 0;
}

Compile it and run it:

el@defiant ~/foo4/40_usleep $ g++ -o s s.cpp
el@defiant ~/foo4/40_usleep $ ./s
nitratefirtilizers

It printed 'nitrate', waited 1 second, then printed 'firtilizers'

1
  • 4
    #include <unistd.h> fixed the problems I was having, thanks!
    – seth10
    Jun 13, 2017 at 13:38
3
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
    const long a=1000000;
    long j;
    cin >> j;
    usleep(a*j);
    puts("exit");
}

use usleep() Insted of sleep and Don't forget to Include unistd.h (Not cunistd)

1

In my case it helped to write Sleep and NOT sleep - very strange, but worked!

1
  • 1
    What environment are you on? windows or linux?
    – hungryWolf
    Sep 13, 2017 at 6:52
0

use std::this_thread::sleep_for()

#include <chrono>
#include <tread>


int main(int argc , char *argv[])
{       
    std::this_thread::sleep_for(std::chrono::seconds(2));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.