I'm looking for a way to have an integer be constantly incremented every 10 seconds or so. I know how to get the integer to increment, but I don't know how to get it to continue to increment no matter what else is currently occurring in the rest of the program.
C++ - constantly increment an integer
1.3k Views Asked by Musicrafter12 At
        	2
        	
        There are 2 best solutions below
0
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                Using C++11 style:
#include <atomic>
#include <iostream>
#include <thread>
int main()
{
    std::atomic<int> i{0};
    std::thread thread_time([&]() { while (true) { ++i; std::this_thread::sleep_for(std::chrono::seconds(10)); } });
    while (true) {
        std::cout << i.load() << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(10));
    }
    thread_time.join();
    return 0;
}
Use
std::threadfor this.Create a function
Now from
main: