#include <iostream>
#include <cstdbool>
#include <unistd.h>
#include <pthread.h>

class Timer{
private:
    bool run;

public:
    void start();
    void stop();
    void print();

};

void Timer::start(){
    this->run = true;
    this->print();
    return;
}

void Timer::stop(){
    this->run = false;
    return;
}

void Timer::print(){

    int counter = 0;

    while(this->run == true){

        std::cout << counter << std::endl;
        counter++;

        usleep(500000);
    }

    return;
}

void *handler(void *argument){

    ((Timer *) argument)->start();

    return argument;
}

int main(void){

    Timer *timer = new Timer();
    pthread_t timer_thread;
    int mainCounter = 0;

    pthread_create(&timer_thread, NULL, handler, (void *) &timer);

    while(true){

        if(mainCounter == 100){
            std::cout << "Stopping..." << std::endl;
            timer->stop();
        }

        std::cout << " => " << mainCounter << std::endl;
        mainCounter++;

        usleep(50000);
    }

    return 0;
}

