Improving my (hopefully) cross-platform Sleep function:

I was trying to think of a way to sleep for a certain amount of time in milleseconds without using the existing sleep function (Windows only). Here it is, it can be compiled in C or C++ and it works :)

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <ctime>

void halt(double);

int main() {
    std::cout << "Start time: approx. " << clock() << " ms from program execution.\n";
    double start = clock(); // Take the ticks since program execution. This can lightly be considered as milliseconds.
    halt(5000);
    double end   = clock(); // Take the amount of ticks again
    std::cout << "End time: approx. " << end << " ms from program execution.\n";
    std::cout << "Halt function lasted approx " << (end - start) << " ms.";
    std::cin.get();
}

void halt(double ms) {
    double start = clock();

    if (ms <= 0) {
        ms = 10;
    }

    while (clock() < (start + ms)); // Do nothing.
}


C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <time.h>

void halt(double);

int main() {
    char* str;
    double start = 0, end = 0;
    printf("Start time: approx. %f ms from program execution.\n", clock());
    start = clock(); // Take the ticks since program execution. This can lightly be considered as milliseconds.
    halt(5000);
    end   = clock(); // Take the amount of ticks again
    printf("End time: approx. %f ms from program execution.\n", end);
    printf("Halt function lasted approx %f ms.", (end - start));
    fgets(str, 256, stdin);
}

void halt(double ms) {
    double start = clock();

    if (ms < 0) {
        ms = 10;
    }

    while (clock() <= (start + ms)); // Do nothing.
}


I called it halt() to avoid name errors. Plus that's what it does... it halts execution...

How can I improve the function? It definately needs improvement, because it slows down all operations by alot. The actual halt function appears to work.

By the way, if my use of fgets() is laughable; I've never used it before. I'm just demonstrating that it works in C.
Last edited on
Without using external libraries, there's no other way to wait a certain amount of time.
Well the issue is that everything is slowed down. Where I called it for five seconsds in the main() function; the cout/printf statements all happened 5s from the entry of the program aswell :l
In general you will not be able to write a usable cross-platform sleep function without writing specialized
versions for every different platform. The problem with your halt function is that it is a busy loop that
consumes 100% of the CPU while it runs.

On windows there's Sleep(); on Unix there is sleep and usleep.

Your best bet probably is something like

1
2
3
4
5
6
7
8
9
10
// psuedo-code:
void halt( unsigned int msecs ) {
#if defined( WIN_32  ) // whatever the symbol is...
    Sleep( msecs );
#elif defined( UNIX ) // whatever the symbol is...
    usleep( msecs * 1000 );
#else
#error Unsupported platform.
#endif
}
Ah, well, I had a good attempt at it.

I'll have a look at how the Windows and *NIX systems accomplish it.
Topic archived. No new replies allowed.