Something not sleep() but just as simple

Someone told me that code that contains "sleep()" can only run on Windows. Having a knowledge of C++ that barely goes beyond 3 weeks in a 2 semester course (The other time was spent in a program called Scratch, it was fun to use, but not practical and it angered me because it was supposed to be a C++ class.) I really cannot judge what I'm told by someone who is better than me in C++. If that is a lie, I'd say I have an easy solution to this problem. But if its not, the research that I've done has only turned up sleep() - which is really not what I'm looking for - and some complex pieces (for me at least) of code that I do not understand.

Thanks in advance
-UDBV

PS What I do know is pretty much only functions, loops (for, while, do, and if), cout and cin, and goto
It depends. What are you trying to do with this function?
These delay functions are not part of C++, but provided by the OS. If the OS supports it, you can call it from C++. You will need to use correct include file.

The Windows function is Sleep().
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx
e.g.
1
2
3
4
5
6
7
8
9
#include <windows.h>
#include <iostream>

int main()
{
    std::cout << "Sleeping for 10 seconds ..." << std::endl;
    Sleep(10 * 1000);
    return 0;
}


The POSIX function is sleep().
http://pubs.opengroup.org/onlinepubs/007908799/xsh/sleep.html
e.g.
1
2
3
4
5
6
7
8
9
#include <unistd.h>
#include <iostream>

int main()
{
    std::cout << "Sleeping for 10 seconds ..." << std::endl;
    sleep(10);
    return 0;
}

Yes, Sleep is windows-only. Anyways you can recreate the function for yourself. Remember that Sleep(0) does NOT wait 0 seconds, but just idles so that your CPU is more free, and that is hardly recreable.
Besides that case, this should be the non-os-depending code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <ctime>
bool Wait(const unsigned long &Time)
{
    clock_t Tick = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
    if(Tick < 0) // if clock() fails, it returns -1
        return 0;
    clock_t Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
    if(Now < 0)
        return 0;
    while( (Now - Tick) < Time )
    {
        Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
        if(Now < 0)
            return 0;
    }
    return 1;
}


Then in your code you can use it like:

1
2
if(!Wait(1000)) // Wait 1 second (1000 ms)
    { /* Error */ }
That code doesn't really sleep as it uses the CPU.

It's not easy to write a portable sleep function that releases the CPU while waiting.
kbw wrote:
That code doesn't really sleep as it uses the CPU.

It's not easy to write a portable sleep function that releases the CPU while waiting.


True.

I still didn't manage to know how to do it os-indipendently.
I guess Sleep() for Windows uses WaitForMessage, or a Timer (SetTimer/KillTimer).
Last edited on
In C++11 you can use sleep_for
http://en.cppreference.com/w/cpp/thread/sleep_for
Thanks, I didn't know that.
Yes, Sleep is windows-only. Anyways you can recreate the function for yourself. Remember that Sleep(0) does NOT wait 0 seconds, but just idles so that your CPU is more free, and that is hardly recreable.
Besides that case, this should be the non-os-depending code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <ctime>
bool Wait(const unsigned long &Time)
{
    clock_t Tick = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
    if(Tick < 0) // if clock() fails, it returns -1
        return 0;
    clock_t Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
    if(Now < 0)
        return 0;
    while( (Now - Tick) < Time )
    {
        Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f);
        if(Now < 0)
            return 0;
    }
    return 1;
}



Then in your code you can use it like:

1
2
if(!Wait(1000)) // Wait 1 second (1000 ms)
    { /* Error */ }[/


I am quite new to C++ so even though I'm glad that you have provided an alternative to me, I would still like to know what exactly I'm doing. I mean I've never seen this stuff before. So if you don't mind explaining it, I would appreciate it if you could. Otherwise I'll be on my way. Thanks again.
Well, that's a Sleep-like function.
It simply waits some time.
1
2
3
4
5
6
7
8
#include <ctime>

void wait(int sec)
{
        clock_t endwait;
        endwait = clock() + sec * CLK_TCK;
        while (clock() < endwait) {}
}


That should work

Just use it where you want it to be with 'wait(1);' for it to wait for a second
These functions don't really "sleep", they are looping-looping-looping until they aren't supposed to anymore.

A "true" sleep actually makes your program sleep, it is using 0% of the CPU.
These functions don't really "sleep", they are looping-looping-looping until they aren't supposed to anymore.


These will work fine for what I'm trying to achieve, thank you guys :3
Topic archived. No new replies allowed.