How can I make an iteration that goes infinitely ? I don't mean make an infinite while loop. I mean the program running normally , while there is an iteration happning in the background, uninfluenced by the rest of the program.
#include <iostream>
#include <thread>
#include <chrono>
void fn ( char ch ) // the function to be executed in the background
{
while(true) // infinite loop
{
std::cout << ch << std::flush ;
std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
}
}
int main()
{
// launch four infinite iterations in the background. goes on and on
// till the program ends (note: the thread is detached as soon as it is created)
for( char c : { 'a', 'b', 'c', 'd' } ) std::thread( fn, c ).detach() ;
// rest of the program (ends on eof on standard input)
while( std::cin.get() != EOF ) std::cout << '\n' << std::flush ;
}
#include <iostream>
#include <thread>
#include <chrono>
void fn ( char ch ) // the function to be executed in the background
{
while(true) // infinite loop
{
std::cout << ch << std::flush ;
std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
}
}
int main()
{
// launch four infinite iterations in the background. goes on and on
// till the program ends (note: the thread is detached as soon as it is created)
for( char c : { 'a', 'b', 'c', 'd' } ) std::thread( fn, c ).detach() ;
// rest of the program (ends on eof on standard input)
char c ;
while( std::cin.get(c) ) std::cout << '\n' << std::flush ;
}
No, I can't. I do not know what is it that you want to do once every second, in the background.
You know that; so you can write the function that is to be executed in the background.