Stop the program an amount of time.

Ok, so I have to implement a round-robin simulator. The problem is that simulator should stop(as if the process would be being executed by the CPU). And I have no idea how to do that.
I didn't find anything about how to do that on the references and the forum. I thought about sleep(), but it seems it is only for threads...
Any idea is welcome.
The easiest way to make a sleep function would be to simply have a timer, and then a busy loop that continues until the timer has reached a certain point. Here is a quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <chrono>

void mySleep(double seconds) {
    auto start = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> timeSoFar = std::chrono::duration<double>(0.0);

    while (timeSoFar < std::chrono::duration<double>(15.0)) {
        auto current = std::chrono::high_resolution_clock::now();
        timeSoFar = std::chrono::duration_cast<std::chrono::duration<double>> (start - current);
    }
	
    return;
}

int main() {
    std::cout << "Starting" << std::endl;
    mySleep(15);
    std::cout << "Finished" << std::endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.