Simple Multithread Timer This is a very basic timer that can support multithreading with std::thread and std::chrono.

The timer has the classic functions: start() and stop().

The start() method creates an independent thread (if multithread support is enabled), then sleep the thread for a given Interval, then execute Timeout function. This method sets the running flag to true.

If singleShot flag is not enabled the sleepThenTimeout process is called while a running flag is true.

If multithread support is not enabled, the current thread is sleep.

The stop() method just sets the running flag to false and join the thread.

My doubt is about threat-safety.  I've just started to learn how multithreading works, so I'm not sure if I must use mutexes or something like that.

Any other type of feedback are welcome!

Timer.h

#ifndef TIMER_H
#define TIMER_H

#include &lt;thread&gt;
#include &lt;chrono&gt;

class Timer
{
public:
    typedef std::chrono::milliseconds Interval;
    typedef std::function&lt;void(void)&gt; Timeout;

    Timer(const Timeout &amp;timeout);
    Timer(const Timeout &amp;timeout,
          const Interval &amp;interval,
          bool singleShot = true);

    void start(bool multiThread = false);
    void stop();

    bool running() const;

    void setSingleShot(bool singleShot);
    bool isSingleShot() const;

    void setInterval(const Interval &amp;interval);
    const Interval &amp;interval() const;

    void setTimeout(const Timeout &amp;timeout);
    const Timeout &amp;timeout() const;

private:
    std::thread _thread;

    bool _running = false;
    bool _isSingleShot = true;

    Interval _interval = Interval(0);
    Timeout _timeout = nullptr;

    void _temporize();
    void _sleepThenTimeout();
};

#endif // TIMER_H


Timer.cpp

#include "Timer.h"

Timer::Timer(const Timeout &amp;timeout)
    : _timeout(timeout)
{
}

Timer::Timer(const Timer::Timeout &amp;timeout,
             const Timer::Interval &amp;interval,
             bool singleShot)
    : _isSingleShot(singleShot),
      _interval(interval),
      _timeout(timeout)
{
}

void Timer::start(bool multiThread)
{
    if (this-&gt;running() == true)
        return;

    _running = true;

    if (multiThread == true) {
        _thread = std::thread(
                    &amp;Timer::_temporize, this);
    }
    else{
        this-&gt;_temporize();
    }
}

void Timer::stop()
{
    _running = false;
    _thread.join();
}

bool Timer::running() const
{
    return _running;
}

void Timer::setSingleShot(bool singleShot)
{
    if (this-&gt;running() == true)
       return;

    _isSingleShot = singleShot;
}

bool Timer::isSingleShot() const
{
    return _isSingleShot;
}

void Timer::setInterval(const Timer::Interval &amp;interval)
{
    if (this-&gt;running() == true)
       return;

    _interval = interval;
}

const Timer::Interval &amp;Timer::interval() const
{
    return _interval;
}

void Timer::setTimeout(const Timeout &amp;timeout)
{
    if (this-&gt;running() == true)
       return;

    _timeout = timeout;
}

const Timer::Timeout &amp;Timer::timeout() const
{
    return _timeout;
}

void Timer::_temporize()
{
    if (_isSingleShot == true) {
        this-&gt;_sleepThenTimeout();
    }
    else {
        while (this-&gt;running() == true) {
            this-&gt;_sleepThenTimeout();
        }
    }
}

void Timer::_sleepThenTimeout()
{
    std::this_thread::sleep_for(_interval);

    if (this-&gt;running() == true)
        this-&gt;timeout()();
}


main.cpp

#include &lt;iostream&gt;

#include "Timer.h"

using namespace std;

int main(void)
{
    Timer tHello([]()
    {
        cout &lt;&lt; "Hello!" &lt;&lt; endl;
    });

    tHello.setSingleShot(false);
    tHello.setInterval(Timer::Interval(1000));
    tHello.start(true);

    Timer tStop([&amp;]()
    {
        tHello.stop();
    });

    tStop.setSingleShot(true);
    tStop.setInterval(Timer::Interval(3000));
    tStop.start();

    return 0;
}
Basically you are working with these class members inside the thread functions _temporize and _sleepThenTimeout.


timeout, _isSingleShort and _interval, these three cannot be changed after the start function is called, so it is safe to use them.
_running on the other hand can be read/write by both threads. In reality it might not cause any problem as assignment to bool is atomic (on most architectures), but to be 100% safe you can use std::atomic&lt;bool&gt;.


Another important thing to keep in mind is that while timer class itself is thread safe, it is responsibility of the user of this class to make sure that the timer call back function (_timeout) is thread-safe by it self, as it will be executed in a separate thread.