Infinite iteration

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 ;
}
it says that eof is not declared
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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 ;
}
so how can i turn this into a program that takes an integer i=0 and make it iterate every second in the background ?
Write a function that takes an integer input, and iterates once every second, without an end.

Launch the function in a detached thread; it would run in the background as long as the program is running.
I can't figure it out. Can you write it for me ?
> Can you write it for me ?

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.
I just need an int variable that grows from 0 every second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>

void incr_every_second( std::atomic<int>& var )
{
    while(true)
    {
         std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
         ++var ;
    }
}

int main()
{
    std::atomic<int> var { 0 } ;
    std::thread( incr_every_second, std::ref(var) ).detach() ;

    // rest of the program (ends on eof on standard input)
    char c ;
    while( std::cin.get(c) ) std::cout << "var == " << var << '\n' << std::flush ;
}
Thank you very much. I really needed this.
Topic archived. No new replies allowed.