I cant seem to find anything useful on the web except adding outside libraries for this one
help is required.

I wrote a program that you can say builds a database of users and their passwords.
the program loads the users from an existing file already into a hashtable.
now if i run the program for 2 days it wont update the file and if the pc crash
i will lose the whole data inside the program.

i wrote a function save() that opens the output file and copies the hashtable to it
now i want to call this function every 5 minutes .
is there any way of doing this?

workspace is vs2008 windows.

thanks , Despairy.

6 Years
Discussion Span
Last Post by Caligulaminus

> I cant seem to find anything useful on the web except adding outside libraries for this one

With a conforming implementation, nothing more than the standard C++ library is needed. Something like this:

#include <thread>
#include <atomic>
#include <chrono>

void save_file( const std::string& path, int minutes,
                std::atomic<bool>& keep_saving /* , hashtable */ )
    const std::chrono::duration<int> duration( minutes * 60 ) ; // seconds
        std::this_thread::sleep_for(duration) ;
        // TODO: save hashtable to the file
        // NOTE: synchronized read access to the hashtable would be required.
    while(keep_saving) ;

int main()
    std::string path_to_file = "whatever" ;
    std::atomic<bool> keep_saving_file(true) ;
    std::thread thread_to_save_file( save_file, path_to_file, 5, keep_saving
                                     /* , hashtable */ ) ;

    // do other stuff
    // ...

    // quit program now
    keep_saving_file.store(false) ;
    thread_to_save_file.join() ;


> workspace is vs2008 windows

With VS2008, there is no support for C++11 features; an external library would be required. Boost.Threads is an option; this would give the closest possible mapping to standard C++ threads. http://www.boost.org/doc/libs/1_48_0/doc/html/thread.html


You may also want to write in a mutex so that the thread that saves the file doesn't try to do so when the in-memory data is being modified, and vice-versa.


Thanks for answering. i asked my teacher and she said they run automated
testings on vs 2008 without any additional libraries.
so looking for another idea if you got any :( thanks


You can use a timer with an interrupt handler for the timer expiring event (ETIME). This was the old way to do it, and should work with VS 2008 just fine.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.