timer_timeout到底怎么用?谁有例子,能不能给小弟看看。

typedef union {
struct _pulse pulse;
//其他结构
} my_message_t;
my_message_t msg;
int rcvid;
chid = ChannelCreate(0);
pthread_mutex_lock( &mutex );
event.sigev_notify = SIGEV_PULSE;
event.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0,chid,0, 0);
event.sigev_priority = getprio(0);
event.sigev_code =9;
timeout = 100000000;
TimerTimeout( CLOCK_REALTIME, _NTO_TIMEOUT_CONDVAR ,&event,&timeout, NULL );
for (;:wink:
{ printf("we got a pulse from our timer
");
rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
printf("we got a pulse from our timer
");
if (rcvid==0) {
if (msg.pulse.code ==9) {
printf("we got a pulse from our timer
");
}
}
}
pthread_cond_wait( &cond, &mutex );
我这样写,程序会停在rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
也就是说第二个printf("we got a pulse from our timer
");
不会执行,换句话说,根本就接受到msg。但是我找不出错在哪里?

TimerTimeout() 只对接下来的一个内核调用起作用;象你的程序,TimerTimeout()后面接的是printf();printf()里就有 MsgSend()把字符串送去console设备,这样你的TimerTimeout()就被取消掉了。

再有,即使你把 printf()那一行删掉,那么 TimerTimeout()后面紧跟的是 MsgReceive();因为 MsgReceive() 只进入 STATE_RECEIVE状态, 不会进入 STATE_CONDVAR,你的条件没有满足,计时不会开始,你也不会收到你要的 message。

最后,如果你只是不想长期 condvar wait的话,有现成的 pthread_cond_timedwait() 可以用,不用自己去 TimerTimeout().

xtang,谢谢你,我明白了。