|
请问,我将posix:tmr定时器封装为C++的类,可是不知道为什么声明2个对象时,若将定时的时间设置的一样的话,其中一个信号受不到,这是为什么呀,能否指点一下,并且问一下posix:tmr定时器是多线程可靠的吗,我的意思是说有什么会使posix:tmr定时器在时间到了后不发消息。
#ifndef TIMETRIGGER_H
#define TIMETRIGGER_H
#include <time.h>
#include <signal.h>
#include "EventDispUnit.hxx"
#include "FIFOQueue.hxx"
#include "Event.hxx"
namespace SIP
{
namespace Base
{
class TimeTrigger
{
typedef void (*func_entry)(sigval_t cb_data);
friend void saHandler(int signo, siginfo_t *info, void *context);
public:
TimeTrigger();
TimeTrigger( EventDispUnit *dispUnit, FIFOQueue<EventDispUnit *> *eventQuery, long ticks );
~TimeTrigger();
void Start();
void SetTime( long ticks );
void Stop();
void Pause();
void ReStart( long ticks );
void TimeOut();
private:
timer_t mTimer;
struct itimerspec itval;
struct sigevent sigev;
struct sigaction act;
long mTicks;
EventDispUnit *pEventDispUnit;
FIFOQueue<EventDispUnit *> *pEventQuery;
};
} //namespace Base
} //namespace SIP
#endif //ndef TIMETRIGGER_H
cpp文件:
#include "TimeTrigger.hxx"
#include <iostream>
using namespace SIP::Base;
void sigevHandler(int signo, siginfo_t *info, void *context)
{
TimeTrigger *tt = (TimeTrigger *)(info->si_value.sival_ptr);
tt->TimeOut();
}
TimeTrigger::TimeTrigger()
{
}
TimeTrigger::TimeTrigger( EventDispUnit *dispUnit, FIFOQueue<EventDispUnit *> *eventQuery, long ticks )
{
pEventDispUnit = dispUnit;
pEventQuery = eventQuery;
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGALRM;
sigev.sigev_value.sival_ptr = this;
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = sigevHandler;
if((sigemptyset(&act.sa_mask)== -1) || (sigaction(SIGALRM, &act,NULL) == -1))
{
}
SetTime( ticks );
int iResult = timer_create( CLOCK_REALTIME, &sigev, &mTimer );
std::cout << "new timetrigger tick is " << ticks << "timeid is" << mTimer << endl;
}
TimeTrigger::~TimeTrigger()
{
int iResult = timer_delete( mTimer );
}
void TimeTrigger::Start()
{
int iResult = 0;
iResult = timer_settime( mTimer, 0, &itval, NULL );
}
void TimeTrigger::SetTime( long ticks )
{
mTicks = ticks;
if ( ticks > 1000 )
{
itval.it_value.tv_sec = ticks / 1000; itval.it_value.tv_nsec = 0;
}
else if ( ticks > 0 && ticks < 1000 )
{
itval.it_value.tv_nsec = ticks * 10^6; }
else
{
itval.it_value.tv_nsec = 1 * 10^6;
}
itval.it_interval = itval.it_value;
}
void TimeTrigger::Stop()
{
itval.it_value.tv_sec = 0; itval.it_value.tv_nsec = 0;
int iResult = timer_settime( mTimer, 0, &itval, NULL );
}
void TimeTrigger::ReStart( long ticks )
{
SetTime( ticks );
int iResult = timer_settime( mTimer, 0, &itval, NULL );
}
void TimeTrigger::TimeOut()
{
pEventQuery->Push( pEventDispUnit );
} |
|