[Linux] C++ Timer programming
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > [Linux] C++ Timer programming




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    [Linux] C++ Timer programming  
Fabien R


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

Hello,

Did anyone succeed in using the timer API with Linux ?
I tried to use my code to create a repeated timer but it run only once :-(
Any hint ?
--
Fabien
--------------------------------------------------------------------------
#ifndef __TimerClass_
#define __TimerClass_
//<SECTION> Standard Include =============================
#include <iostream.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <stdexcept>

//<SECTION> Local Include =============================
#include "utils.h"


//<SECTION> Constant Definitions ===============================
typedef void (*SIGFUNC) (int, siginfo_t*, void *);

//<SECTION> Class Declaration  ========================================

class TimerClass
{

/*
* Attributes
*/

private:
// Timer id
timer_t tmTimerId;
// Indicates if the timer is enabled
bool tmTimerEnabled;
// Indicates if the timer is triggered once
bool tmSingleShot;
// Timer interval in {second, nanosecond}
struct itimerspec tmTimerInterval;
/*
* Operations
*/
public:
/*<FUNCTION> TimerClass  ****************************************
****
*
* Description: Constructor
*
* Input parameters:
* - sigNb:			Signal triggered by the timer
* - p_function:    Function triggered by the timer
* - tag:			Tag used to differentiate the timers
* - singleShot:	Indicates if the timer triggers only once (defaulted to no)
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
TimerClass(int sigNb, SIGFUNC p_function, int tag, bool singleShot = false);
/*<FUNCTION> ~TimerClass  ****************************************
****
*
* Description: Destructor
*
* Input parameters:
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
~TimerClass();
/*<FUNCTION> setEnabled  ****************************************
****
*
* Description: This function enables or disables the timer
*
* Input parameters:
* - b_value:        Indicates the required state
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
void
setEnabled(bool b_value);
/*<FUNCTION> setInterval  ****************************************
****
*
* Description: This function updates the timer interval
*
* Input parameters:
* - tmi:        Interval in {seconds, nanoseconds}
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
void
setInterval(struct itimerspec tmi);


};
#endif

-------------------------------------------------------------------------
//<SECTION> Local Include =============================
#include "TimerClass.h"

/*<FUNCTION> TimerClass  ****************************************
****
*
* Description: Constructor
*
* Input parameters:
* - sigNb:			Signal triggered by the timer
* - p_function:    Function triggered by the timer
* - tag:			Tag used to differentiate the timers
* - singleShot:	Indicates if the timer triggers only once (defaulted to no)
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
TimerClass::TimerClass(int sigNb, SIGFUNC p_function, int tag, bool singleSh
ot)
{
try
{
tmSingleShot = singleShot;
// The timer is suspended
tmTimerEnabled = false;
// The interval is defaulted to 1 second
tmTimerInterval.it_value.tv_sec = 1;
tmTimerInterval.it_value.tv_nsec = 0;
// Defines the timer action
struct sigaction sAction;
sigemptyset(&sAction.sa_mask);
sAction.sa_flags = SA_SIGINFO;
sAction.sa_sigaction = p_function;
int i_res = sigaction(sigNb, &sAction, 0);
if (i_res == -1)
{
perror("TimerClass::TimerClass:sigaction");
}
// Defines the signal associated to the timer
struct sigevent sEvent;
sEvent.sigev_notify = SIGEV_SIGNAL;
sEvent.sigev_signo = sigNb;
sEvent.sigev_value.sival_int = tag;
// Creates the timer
i_res = timer_create(CLOCK_REALTIME, &sEvent, &tmTimerId);
if (i_res == -1)
{
perror("TimerClass::TimerClass:timer_create");
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::TimerClass"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> ~TimerClass  ****************************************
****
*
* Description: Destructor
*
* Input parameters:
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
TimerClass::~TimerClass()
{
try
{
// Suspends the timer
setEnabled(false);
// Destroys the timer
int i_res = timer_delete(tmTimerId);
if (i_res == -1)
{
perror("TimerClass::~TimerClass:timer_delete");
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::~TimerClass"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> setEnabled  ****************************************
****
*
* Description: This function enables or disables the timer
*
* Input parameters:
* - b_value:        Indicates the required state
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
void
TimerClass::setEnabled(bool b_value)
{
try
{
tmTimerEnabled = b_value;
int i_res;
switch(b_value)
{
case true:
{
// Starts the timer
i_res = timer_settime(tmTimerId, 0, &tmTimerInterval, 0);
if (i_res == -1)
{
perror("TimerClass::setEnabled true:timer_settime");
}
break;
}
case false:
{
// Stops the timer
struct itimerspec tmi;
tmi.it_value.tv_sec = 0;
tmi.it_value.tv_nsec = 0;
tmi.it_interval.tv_sec = 0;
tmi.it_interval.tv_nsec = 0;
i_res = timer_settime(tmTimerId, 0, &tmi, 0);
if (i_res == -1)
{
perror("TimerClass::setEnabled false:timer_settime");
}
break;
}
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::setEnabled"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> setInterval  ****************************************
****
*
* Description: This function updates the timer interval
*
* Input parameters:
* - tmi:        Interval in {seconds, nanoseconds}
*
* Output parameters:
*
* Return value:
*
 ****************************************
***********************************/
void
TimerClass::setInterval(struct itimerspec tmi)
{
tmTimerInterval.it_value.tv_sec = tmi.it_value.tv_sec;
tmTimerInterval.it_value.tv_nsec = tmi.it_value.tv_nsec;
// Checks if the timer triggers once
if (tmSingleShot)
{
tmTimerInterval.it_interval.tv_sec = 0;
tmTimerInterval.it_interval.tv_nsec = 0;
}
else
{
tmTimerInterval.it_interval.tv_sec = tmi.it_interval.tv_sec;
tmTimerInterval.it_interval.tv_nsec = tmi.it_interval.tv_nsec;
}

}

/*==================== End of file =======================*/





[ Post a follow-up to this message ]



    Re: [Linux] C++ Timer programming  
red floyd


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

Fabien R wrote:

> Hello,
>
> Did anyone succeed in using the timer API with Linux ?
> I tried to use my code to create a repeated timer but it run only once :-(
> Any hint ?

[code redacted]

You might want to check the thread titled "pthread_cond_signal in a
signal handler?".  While not obvious from the title, it was related to a
discussion of multithreaded timers.





[ Post a follow-up to this message ]



    Re: [Linux] C++ Timer programming  
Fabien R


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

>
> You might want to check the thread titled "pthread_cond_signal in a
> signal handler?".  While not obvious from the title, it was related to a
> discussion of multithreaded timers.
Thanks,
I read the related articles but I didn't see any timer functions.
So, I'll also simulate my timers using my own ThreadClass.

Regards,
Fabien





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:49 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register