6
\$\begingroup\$

This is a very basic timer that can support multithreading withstd::thread andstd::chrono.

The timer has the classic functions:start() andstop().

Thestart() method creates an independent thread (if multithread support is enabled), then sleep the thread for a givenInterval, then executeTimeout function. This method sets therunning flag to true.

IfsingleShot flag isnot enabled thesleepThenTimeout process is called while arunning flag istrue.

If multithread support is not enabled, thecurrent thread is sleep.

Thestop() method just sets therunning flag to false andjoin 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 <thread>#include <chrono>class Timer{public:    typedef std::chrono::milliseconds Interval;    typedef std::function<void(void)> Timeout;    Timer(const Timeout &timeout);    Timer(const Timeout &timeout,          const Interval &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 &interval);    const Interval &interval() const;    void setTimeout(const Timeout &timeout);    const Timeout &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 &timeout)    : _timeout(timeout){}Timer::Timer(const Timer::Timeout &timeout,             const Timer::Interval &interval,             bool singleShot)    : _isSingleShot(singleShot),      _interval(interval),      _timeout(timeout){}void Timer::start(bool multiThread){    if (this->running() == true)        return;    _running = true;    if (multiThread == true) {        _thread = std::thread(                    &Timer::_temporize, this);    }    else{        this->_temporize();    }}void Timer::stop(){    _running = false;    _thread.join();}bool Timer::running() const{    return _running;}void Timer::setSingleShot(bool singleShot){    if (this->running() == true)       return;    _isSingleShot = singleShot;}bool Timer::isSingleShot() const{    return _isSingleShot;}void Timer::setInterval(const Timer::Interval &interval){    if (this->running() == true)       return;    _interval = interval;}const Timer::Interval &Timer::interval() const{    return _interval;}void Timer::setTimeout(const Timeout &timeout){    if (this->running() == true)       return;    _timeout = timeout;}const Timer::Timeout &Timer::timeout() const{    return _timeout;}void Timer::_temporize(){    if (_isSingleShot == true) {        this->_sleepThenTimeout();    }    else {        while (this->running() == true) {            this->_sleepThenTimeout();        }    }}void Timer::_sleepThenTimeout(){    std::this_thread::sleep_for(_interval);    if (this->running() == true)        this->timeout()();}

main.cpp

#include <iostream>#include "Timer.h"using namespace std;int main(void){    Timer tHello([]()    {        cout << "Hello!" << endl;    });    tHello.setSingleShot(false);    tHello.setInterval(Timer::Interval(1000));    tHello.start(true);    Timer tStop([&]()    {        tHello.stop();    });    tStop.setSingleShot(true);    tStop.setInterval(Timer::Interval(3000));    tStop.start();    return 0;}
Jamal's user avatar
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
askedFeb 5, 2014 at 4:09
Mario Frescas's user avatar
\$\endgroup\$
4
  • \$\begingroup\$Why would you use up all the resources required for a thread when you can set SIGALARM?\$\endgroup\$CommentedFeb 5, 2014 at 5:00
  • \$\begingroup\$As far I know SIGALRM is not cross-plataform.\$\endgroup\$CommentedFeb 6, 2014 at 3:20
  • \$\begingroup\$What platform are you thinking about that does not have sig alarm but does have a threading model?\$\endgroup\$CommentedFeb 6, 2014 at 5:34
  • \$\begingroup\$Sorry, my bad! I've chosen to use threads because I want to learn about threads. I've also could use Qt's Timer but that is not the way.\$\endgroup\$CommentedFeb 9, 2014 at 3:55

2 Answers2

3
\$\begingroup\$

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 thestart 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 tobool is atomic (on most architectures), but to be 100% safe you can usestd::atomic<bool>.

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.

answeredFeb 6, 2014 at 6:35
Ammar's user avatar
\$\endgroup\$
2
  • \$\begingroup\$What do you think about replaceif (this->running() == true) return; with an assert?\$\endgroup\$CommentedFeb 8, 2014 at 23:32
  • 1
    \$\begingroup\$Yes, assert will be better suited here, as it is better to let the caller know of the problem, rather than failing silently. Butassert or theif statement won't affect the thread safety.\$\endgroup\$CommentedFeb 9, 2014 at 8:08
2
\$\begingroup\$

I changed the testing part in main to

int cntHello = 0;int cntStop = 0;Timer tHello([&](){    cntHello++;});tHello.setSingleShot(false);tHello.setInterval(Timer::Interval(1000));tHello.start(true);Timer tStop([&](){    ++cntStop;    tHello.stop();});tStop.setSingleShot(true);tStop.setInterval(Timer::Interval(3001));tStop.start(true);// TODO why could join tHello at this point?// tHello.join();tStop.join();tHello.join();

First change is the during creating the Timer for tHello. I add '&' between '[]' otherwise I cout not use cntHello inside the function. Why?(The counters are used later for checking how often the functions are called.)

Second I use tStop allthough as multithread and wait after starting it until the threads have been finshed.(by using join) For that I wrote methods join() and joinable() for the class Timer with only the the methods for _thread. This works if I join for tStop first. If I join tHello first I got an exception. Can you explain why?

answeredJun 24, 2016 at 7:27
Rainer's user avatar
\$\endgroup\$

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.