Separate program tasks

***Edited***

I wanted to make a timer or some number (int) that would decrease in the "background" while the quiz is going on and the user is answering some questions. How can it be done ?




Last edited on
Hmm, Don't really get your question. What do you want to do ?
You probably want multimpe threads of execution.
Sample program — two threads are decrementing same variable and loop which brings it down to 0 wins:
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
#include <iostream>
#include <atomic>
#include <thread>
#include <mutex>

std::atomic<int> value(90000);

std::mutex output;

void loop(std::string name)
{
    int i;
    while((i = value.fetch_sub(1)) > 0)
        ;
    std::lock_guard<std::mutex> lock(output);
    if(i == 0)
        std::cout << name << " is a winner!\n";
    else
        std::cout << name << " lost :(\n";
}

int main()
{
    std::thread t1(loop, "loop1");
    std::thread t2(loop, "loop2");
    t1.join();
    t2.join();
}
http://coliru.stacked-crooked.com/a/f132b51fb25d2381

If you run it on your machine enough times you will notice that winning loop and order of win/lose reporting changes

EDIT: I read your revised questing, and answer is the same: create a background thread with timer.
However if you want to interrupt user when time is up, then it is harder. You will need to study how your OS handles input and interact with it directly, as C++ standard streams will not help you here.
Last edited on
Thank you very much.
Topic archived. No new replies allowed.