How to make a long time periods if/while statement

Hi everyone, I started a new project and I have some little problems
I'm making a program that would run 24/7 and that could be run on multiple computers that are all running Windows (the program would be in the startup folder of the computer).

So I'm searching for 2 functions, one that would check if the program has been already launched on this computer and another one that would do a save every 24 hours. So for my first problem I have some solutions, but for the second one I don't know what I should do because I think that a loop with sleep() would take too much power for the cpu, so if someone has an idea that would be great, thank you and sorry for my english
Last edited on
Sleep() for a whole minute at a time?
How to you make able understand

Read that and tell me it's not incredibly irritating.
sorry for my english


How to you make able understand

Read that and tell me it's not incredibly irritating.


Maybe English isn't his first language?

Also, it wasn't too hard to understand basically he wants to know if there is a better method than sleep to make his program save a file every 24 hours. Which would mean you are probably going to need to set up some sort of time and when it reaches 24 hours write the file and reset the timer.
Last edited on
> I think that a loop with sleep() would take too much power for the cpu

No, it won't. No processor time time is used by a sleeping thread (It is not in a 'runnable' state).

See: http://www.cplusplus.com/forum/general/122006/

Or the Win32 api Sleep():
causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval ... After the sleep interval has passed, the thread is ready to run.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

SetWaitableTimer() is another way to accomplish this.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686289(v=vs.85).aspx



> Read that and tell me it's not incredibly irritating.

'That' is not in the least bit irritating; it is just some one making an honest effort to seek help.

'This', on the other hand, is irritating. Though not 'incredibly' so.
It is annoying to chance upon a crass boor on the internet, but it does happen once in a while.
I'd like to thank giblit and JLBorges and yes actually english is my third language.
So I read the 2 links, and I have a last question, the sleep function will stop the program during 24 hours, but I'd like to know if there is a way for the program to continue working and executing the script during the sleep function?

So in resume, the program would continue working and doing all the stuff it had to do and separately the sleep function would wake up every 24 hours, so I know I could just put the sleep function in another file so I'd got 2 programs but I'd like to know if there is another way to do it, thank you everyone.
> I'd like to know if there is a way for the program to continue working
> and executing the script during the sleep function?

Run the function that sleeps for some time, wakes up, does some work, sleeps again and so on in a separate thread of execution.

This would be the general idea:

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
27
28
29
30
31
#include <ctime>
#include <iostream>
#include <chrono>
#include <thread>

void background_thread_function() 
{
    for( int i=0 ; i<5 ; ++i )
    {
        std::cout << "background_thread does some work\n" << std::flush ;
        std::this_thread::sleep_for( std::chrono::seconds(2) ) ; // sleeps for two seconds
    }
}

int main()
{
    std::cout << "creating background thread\n" << std::flush ;
    std::thread bkthread( background_thread_function ) ;
    
    for( int i = 0 ; i < 2000 ; ++i )
    {
        std::cout << '.' << std::flush ;
        if( i%200 == 199 ) 
        {
            std::cout << '\n' << std::flush ;
            std::this_thread::sleep_for( std::chrono::seconds(1) ) ; // sleeps for one seconds
        }
    }
    
    bkthread.join() ;
}

http://coliru.stacked-crooked.com/a/106373af2b88f7a8
Thank you a lot, that's perfect, very helpful.
Topic archived. No new replies allowed.