how to set timer in a new thread??

I try to set a timer in a new created thread, but it’s not working when the timer set in the thread, my code is below:

[code]int main( )

{
int flag=1;
// Initialise I/O permitions to ISA card memory address

 struct pci_dev_info info; 
 void *hdl; 
 int i,jj; 
 char F; 
 pthread_attr_t  attr; //thread 
 pthread_attr_init( &attr ); 
  pthread_attr_setdetachstate( &attr,     PTHREAD_CREATE_DETACHED ); 
  pthread_create( NULL, &attr, &sockthread, NULL ); 

}

void* sockthread( )
{

int i; 

//========================timer setup20100623
struct sigevent event;
struct itimerspec itime;
timer_t timer_id;
int chid;
int rcvid;
my_message_t msg;
int conid;
//===================================timer setup 20100623
//=============below set timer 20100623

chid = ChannelCreate(0);
conid=ConnectAttach(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0)ï¼›
event.sigev_notify = SIGEV_PULSE;
event.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0,
chid,
_NTO_SIDE_CHANNEL, 0);
event.sigev_priority = getprio(0);
event.sigev_code = MY_PULSE_CODE;
timer_create(CLOCK_REALTIME, &event, &timer_id);

itime.it_value.tv_sec = 0;
// 500 million nsecs = .004 secs
itime.it_value.tv_nsec = 4000000;
itime.it_interval.tv_sec = 0;
// 500 million nsecs = .004 secs
itime.it_interval.tv_nsec = 4000000;
timer_settime(timer_id, 0, &itime, NULL);
//=============below set timer 20100623
for (;:wink:
{
rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);

if (rcvid ==0) 
{ 
             if(msg.pulse.code == MY_PULSE_CODE) 
        { 

printf(“we got a pulse from our timer\n”);
}
}
}
[/code]
It is a simple test, butrcvid always go wrong,please help!!
THANKS A LOTS

SnowingFish,

Personally I create my timers like so:

timer_t timerId;
struct sigevent event;
int conid; 
int userId = 999;  // Normally this is a unique identifier if you have more than 1 timer

chid = ChannelCreate(0); 

SIGEV_PULSE_INIT(&event, ConnectAttach_r(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0), SIGEV_PULSE_PRIO_INHERIT, 0, userId);

if ((timerId = TimerCreate_r(CLOCK_REALTIME, &event)) < 0)
{
   printf ("Error %d\n", timerId);
   exit(-1);
}

Then you can do your timer initialization etc.

Out of curiosity, what is rcvid giving you if it’s not 0? I assume -1 in which case you should tell us what the error code is.

Tim

Thx Tim,

I misunderstood the function, I thought there can be more then one timer in one process… :blush: , Actually I just use multi-thread, and I assume the timer create a new thread when it trigger, right?

Thank you!

SnowingFish,

There definitely can be more than one timer in one process. There can also be more than one timer in one thread.

timer_t timerId1, timerId2;
struct sigevent event;
int conid;
int userId1 = 1;
int userId2 = 2;

chid = ChannelCreate(0);

SIGEV_PULSE_INIT(&event, ConnectAttach_r(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0), SIGEV_PULSE_PRIO_INHERIT, MY_PULSE_CODE, userId1);

if ((timerId1 = TimerCreate_r(CLOCK_REALTIME, &event)) < 0)
{
   printf ("Error %d\n", timerId);
   exit(-1);
} 

SIGEV_PULSE_INIT(&event, ConnectAttach_r(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0), SIGEV_PULSE_PRIO_INHERIT, MY_PULSE_CODE, userId2);

if ((timerId2 = TimerCreate_r(CLOCK_REALTIME, &event)) < 0)
{
   printf ("Error %d\n", timerId);
   exit(-1);
} 

// I am not showing the setup of the timer times...


for (;;)
{
    rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
         
    if (rcvid ==0)
    {
         if(msg.pulse.code == MY_PULSE_CODE) 
         {
             if(msg.timerPulse.value.sival_int == userId1)
             {
                printf("we got a pulse from timer1\n");
             }
             if(msg.timerPulse.value.sival_int == userId2)
             {
                printf("we got a pulse from timer2\n");
             }
         }
     }
}          

No, a timer does not create a thread when it is created or when it triggers. It’s entirely handled in the kernel.

Tim

Thx very much, Tim, I got it…