hi,

i am having a project where i must implement a method that takes measurements(random numbers) on specific time intervals.This means that i should read (for example) a number every 5minites...I ve been told for a sleep function but i can't find any info about it....Any thoughts on how i should implement this method?:confused:

also is there any way to read the system time through c++?{to know what time appears in the system clock}

thanks in advance for your help....:)
Nicolas

Dani AI

Generated

A concise, modern pattern for “take a measurement every N minutes” and also record the system clock.

As pointed out, operating systems expose sleep/timer primitives; for portable C++ code (post‑C++11) prefer the standard library’s timing and threading facilities. Use a monotonic clock (std::chrono::steady_clock) to schedule the next sample and std::chrono::system_clock to stamp measurements for human-readable logs. Scheduling the next wake-up with steady_clock + sleep_until avoids cumulative drift that happens if each iteration simply calls sleep_for(period).

// portable C++11+ pattern (replace sample() with real work)
#include <iostream>
#include <chrono>
#include <thread>
#include <ctime>

int main()
{
    using namespace std::chrono;
    const minutes period(5);
    auto next = steady_clock::now() + period;

    while (true) {
        int value = /* sample measurement */;
        auto ts = system_clock::to_time_t(system_clock::now());
        std::cout << "Measured at " << std::ctime(&ts) << " value=" << value;

        std::this_thread::sleep_until(next);
        next += period;
    }
}

Notes and practical cautions: run this loop in a background thread if the program must remain responsive; use an atomic/condition variable to stop gracefully. If the sampling task can sometimes exceed the period, decide whether to skip, run back-to-back, or log the delay. Use system timers or real‑time facilities (OS APIs or Boost) only when sub-millisecond accuracy or guaranteed scheduling under heavy load is required. Also be aware that system suspend/clock adjustments affect system_clock timestamps; steady_clock remains monotonic. This expands on ’s original question and the earlier replies by offering a portable, drift-robust approach.

Recommended Answers

All 5 Replies

I ve been told for a sleep function but i can't find any info about it....Any thoughts on how i should implement this method?

If you are using Linux, then this for sleep and if you are using windows then .

also is there any way to read the system time through c++?{to know what time appears in the system clock}

Yes there is, use the header file #include <ctime> if using C++ or #include <time.h> if using C. For the header reference see and here.

thanks in advance for your help....Nicolas

Glad we could help.

so i shoud implement that function as thread?{In order to call the sleep function on that thread....}

No need to complicate matters, here is a simple way of using the function..

#include <iostream>
#include <windows.h>
using namespace std;
 
int main(int argc, char** argv)
{
    cout << "Hello" << endl ;
    cout << "Wait for 3 seconds..." << endl ;
    Sleep(5000) ; // takes the time in milliseconds ( 1 second = 1000 milliseconds)
    cout << "World" ;
    getchar( ) ;
    return 0 ;
}

thanks alot!

Thanks I just found this through google great help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.