怎样在一个新创的thread里用定时器?

大家好,我想在一个新创建的thread里面使用定时器,用的是例子里的定时程序,当只在main函数里用的时候可以,但是在Thread里面好像就受不好pulse, 下面是我的程序,大家帮忙给看看该怎么改,谢谢

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 (;;)
	{
	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");
				 		}
 		 		}
}

运行的情况是,在rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL)是就卡住了,然后跳出程序。如果我把chid换成conid的话,则MsgReceive(chid, &msg, sizeof(msg), NULL)则不会自己跳出程序,但是rcvid是错误的返回值。。
谢谢了!!!

测试了一下,结果很正常,代码还是你的,只是加了头文件
#include <stdlib.h>
#include <stdio.h>

/*

  • Demonstrate how to set up a timer that, on expiry,
  • sends us a pulse. This example sets the first
  • expiry to 1.5 seconds and the repetition interval
  • to 1.5 seconds.
    */

#include <stdio.h>
#include <time.h>
#include <sys/netmgr.h>
#include <sys/neutrino.h>
#include <pthread.h>
#include <sched.h>
#include <time.h>


#define MY_PULSE_CODE _PULSE_CODE_MINAVAIL
void *sockthread(void *arg);

typedef union {
struct _pulse pulse;
/* your other message structures would go
here too */
} my_message_t;

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( void *arg)
{

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”);
}
}
}
return arg;
}