Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:58:56+00:00 2026-06-15T12:58:56+00:00

I wrote a timer class for my project. I used standard C++ function calls

  • 0

I wrote a timer class for my project. I used standard C++ function calls for it.

I want to create a timer (start, stop, pause, continue, reset) it and also pass it new timings.

If the timer fires before I stop it, I want to get a qnx pulse message on a given channel.

This is the header:

#ifndef TIMER_H_
#define TIMER_H_

#include <time.h>
#include <sys/neutrino.h>
#include "HWaccess.h"
#include "address.h"

#define MILLISECONDS_NANOSECONDS_CONV   1000000
#define SECONDS_MILLISECONDS_CONV       1000

class Timer {
public:
    /**
     * Constructor creates a new timer, attaches to the given
     * channel.
     * Does not start timer!
     *
     * @param chid
     * @param seconds
     * @param milliseconds
     * @param msg
     */
    Timer(int chid, int sec, int msec, int msg);

    /**
     * Deletes timer after detach from channel
     */
    virtual ~Timer();

    /**
     * Starts the timer
     */
    void start();

    /**
     * Stops timer and resets it to initial values
     */
    void stop();

    /**
     * Pauses the timer
     */
    void pause();

    /**
     * Continues the timer
     */
    void cont();

    /**
     * Resets the timer to initial values
     */
    void reset();

    /**
     * Changes timer values to new given values and
     * resets it
     * Does not start the timer!
     *
     * @param seconds
     * @param milliseconds
     */
    void changeTime(int sec, int msec);

private:
    /**
     * Timer ID
     */
    timer_t timerid;

    /**
     * Timerstruct for running timer
     */
    struct itimerspec timer;

    /**
     * Timerstruct for backing up the running timer
     */
    struct itimerspec backupTimer;

    /**
     * timer value: seconds
     */
    int seconds;

    /**
     * timer value: milliseconds
     */
    int miliSeconds;

    /**
     * Connection ID for timeout pulse
     */
    int coid;

    /**
     * Event structure for timer if it fires
     */
    struct sigevent event;
};

#endif /* TIMER_H_ */

…and the implementation:

#include "Timer.h"

Timer::Timer(int chid, int sec, int msec, int msg) {
    if ((coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0)) == -1) {
        printf("Timer: Error in ConnectAttach\n");
    }

    SIGEV_PULSE_INIT(&event, coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_TIMER, msg/*Timer abgelaufen*/);

    if (timer_create(CLOCK_REALTIME, &event, &timerid) == -1) {
        printf("Timer: Error in timer_create()\n");
    }

    seconds = sec;
    miliSeconds = msec;

    reset();
}

Timer::~Timer() {
    if (ConnectDetach(coid) == -1) {
        printf("Timer: Error in ConnectDetach\n");
    }

    if (timer_delete(timerid) == -1) {
        printf("Timer: Error in timer_delete()\n");
    }
}

void Timer::start() {
    if(timer_settime(timerid, 0, &timer, NULL) == -1){
        printf("Timer: Error in timer_settime()\n");
    }
}

void Timer::stop() {
    // stop timer
    timer.it_value.tv_sec = 0;
    timer.it_value.tv_nsec = 0;
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_nsec = 0;
    if(timer_settime(timerid, 0, &timer, NULL) == -1){
        printf("Timer: Error in timer_settime()\n");
    }

    // reset it
    reset();
}

void Timer::pause() {
    timer.it_value.tv_sec = 0;
    timer.it_value.tv_nsec = 0;
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_nsec = 0;

    // backup running timer values
    if(timer_gettime(timerid, &backupTimer) == -1){
        printf("Timer: Error in timer_gettime()\n");
    }
    // disarm 
    if(timer_settime(timerid, 0, &timer, NULL) == -1){
        printf("Timer: Error in timer_settime()\n");
    }
}

void Timer::cont() {
    // recover old values
    timer = backupTimer;
    // Arm timer
    if(timer_settime(timerid, 0, &timer, NULL) == -1) {
        printf("Timer: Error in timer_settime()\n");
    }
}

void Timer::reset(){
    timer.it_value.tv_sec = seconds;
    timer.it_value.tv_nsec = miliSeconds * MILLISECONDS_NANOSECONDS_CONV;
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_nsec = 0;
}

void Timer::changeTime(int sec, int msec){
    seconds = sec;
    miliSeconds = msec;
    reset();
}

1) Can I set the timer struct’s values to zero (to stop the timer) and backup the values in another back struct the way I am doing it? Or is the running timer decremented in the timer struct?

2) Can i easily restore the old timer values with a simple timer = backupTimer?

3) And finally, if I create and start a timer in a function, something like this:

void coolClass::interestingFunction() {
    //do other time consuming stuff here...
    Timer timer(chid, 10, 0);
    timer.start();
}

… then the timer is created on the stack and when I exit this function (and its variables etc.) are no longer valid. Will the timer still count down and fire my pulse? Or will I have to use a classifier in my header file for this timer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:58:58+00:00Added an answer on June 15, 2026 at 12:58 pm

    Ok i fixed all my problems ;>
    if some1 is interested in how, heres the modified code:

    #include "Timer.h"
    
    Timer::Timer(int chid, int sec, int msec, int msg) {
        if ((coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0)) == -1) {
            printf("Timer: Error in ConnectAttach\n");
        }
    
        SIGEV_PULSE_INIT(&event, coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_TIMER, msg/*Timer abgelaufen*/);
    
        if (timer_create(CLOCK_REALTIME, &event, &timerid) == -1) {
            printf("Timer: Error in timer_create()\n");
        }
    
        seconds = sec;
        miliSeconds = msec;
    
        reset();
    }
    
    Timer::~Timer() {
        if (ConnectDetach(coid) == -1) {
            printf("Timer: Error in ConnectDetach\n");
        }
    
        if (timer_delete(timerid) == -1) {
            printf("Timer: Error in timer_delete()\n");
        }
    }
    
    void Timer::start() {
        //TODO running flag, wg doppelt pause / continue
        if(timer_settime(timerid, 0, &timer, NULL) == -1){
            printf("Timer: Error in timer_settime()\n");
        }
    }
    
    void Timer::stop() {
        // Stoppe den Timer
        if(timer_settime(timerid, 0, NULL, NULL) == -1){
            printf("Timer: Error in timer_settime()\n");
        }
    
        // Zuruecksetzen
        reset();
    }
    
    void Timer::pause() {
        // disarm (da erster Wert NULL)
        if(timer_settime(timerid, 0, NULL, &backupTimer) == -1){
            printf("Timer: Error in timer_settime()\n");
        }
    }
    
    void Timer::cont() {
        // Arm, da Werte im struct wieder != 0
        if(timer_settime(timerid, 0, &backupTimer, NULL) == -1) {
            printf("Timer: Error in timer_settime()\n");
        }
    }
    
    void Timer::reset(){
        timer.it_value.tv_sec = seconds;
        timer.it_value.tv_nsec = miliSeconds * MILLISECONDS_NANOSECONDS_CONV;
        timer.it_interval.tv_sec = 0;
        timer.it_interval.tv_nsec = 0;
    }
    
    void Timer::changeTime(int sec, int msec){
        seconds = sec;
        miliSeconds = msec;
        reset();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a class project, we have to create a hangman game in Java (we're
I had wrote a web site using asp.net mvc3 and used Membership class for
I wrote a C# application that is a simple countdown timer. I use it
I've got a function I wrote quite some time ago that works fine, but
I wrote a simple example, which estimates average time of calling virtual function, using
I wrote a .net assembly using c# to perform functions that will be used
Assuming that a project has been using the C class prefix for a long
I want to call a class and its method with the name of the
About project: I'm trying to write CMS-like web-site on Java. Previously I used PHP
I used the OWLGrinder to create the assembly and imported the library into my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.