pthread_cond_timedwait超时未起作用,程序被阻塞(函数gettimeofday和clock_gettime)系统Android O 32bit

最近遇到一个奇怪的问题,开机时程序的定时器失效了。但是重启程序后又正常了。定时器的主要代码:

void BThost_initEvent(BThost_Event* event,unsigned char manual_reset/*,const TCHAR* Eventname*/)
{
	event->is_set =  FALSE;
    event->is_pulse=  FALSE ;
    event->is_manual_reset=  manual_reset;
	pthread_mutex_init( &event->lock, NULL );
    pthread_cond_init( &event->cond, NULL );
}

int BThost_WaitEvent(BThost_Event* event, unsigned long waittime_usec) 
{
    int ret = 0;
    struct timeval    tp;
    struct timespec ts; 
    if ( FALSE == event->is_manual_reset )
        event->is_set = FALSE;
    pthread_mutex_lock( &event->lock );
    if ( TRUE != event->is_set )
    {
        if(waittime_usec == 0xFFFFFFFF)
        {
            pthread_cond_wait( &event->cond, &event->lock);
            ret = 0;
        }
        else
        {
            int usec = 0;
            gettimeofday(&tp, NULL);
            ts.tv_sec  = tp.tv_sec;
            usec = tp.tv_usec+waittime_usec;
            if(usec > 1000000)
            {
                ts.tv_sec += (usec/1000000);
                usec = usec % 1000000;
            }
            ts.tv_nsec = usec * 1000;
            ret = pthread_cond_timedwait( &event->cond, &event->lock ,(const struct timespec *)&ts);
            while(ret == 0 && TRUE != event->is_set)
            {
                ret = pthread_cond_timedwait( &event->cond, &event->lock ,(const struct timespec *)&ts);
            }
        }
    }
    pthread_mutex_unlock( &event->lock );
    if( ret != 0 ) //timeout 
        return 0;
    return 1;    
}

程序就在上图pthread_cond_timedwait被阻塞,后来使用下列代码将系统时间打印出来

FILE *fp = popen("date","r");
fread(buff,1024,1,fp);
pclose(fp);

打印的结果如下:

系统时间从2021年变成了2005年。

修改方法:不用gettimeofday,改用clock_gettime

void BThost_initEvent(BThost_Event* event,unsigned char manual_reset/*,const TCHAR* Eventname*/)
{
	event->is_set =  FALSE;
    event->is_pulse=  FALSE ;
    event->is_manual_reset=  manual_reset;
	pthread_mutex_init( &event->lock, NULL );
#if 0
    pthread_cond_init( &event->cond, NULL );
   
#else
	pthread_condattr_t attr;
	pthread_condattr_init(&attr);
    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
    pthread_cond_init(&event->cond, &attr);
#endif
}


int BThost_WaitEvent(BThost_Event* event, unsigned long waittime_usec) 
{
    int ret = 0;
#if 0
    struct timeval    tp;
#else
	struct timespec tp;
#endif
     struct timespec ts; 
    if ( FALSE == event->is_manual_reset )
        event->is_set = FALSE;
    pthread_mutex_lock( &event->lock );
    if ( TRUE != event->is_set )
    {
        if(waittime_usec == 0xFFFFFFFF)
        {
            pthread_cond_wait( &event->cond, &event->lock);
            ret = 0;
        }
        else
        {
            int usec = 0;
#if 0			
            gettimeofday(&tp, NULL);
            ts.tv_sec  = tp.tv_sec;
            usec = tp.tv_usec+waittime_usec;
            if(usec > 1000000)
            {
                ts.tv_sec += (usec/1000000);
                usec = usec % 1000000;
            }
            ts.tv_nsec = usec * 1000;

#else
            long nanoseconds;
            clock_gettime(CLOCK_MONOTONIC, &tp);
            nanoseconds = tp.tv_nsec + (waittime_usec*1000L);
            ts.tv_sec  = tp.tv_sec;

            if(nanoseconds > 1000000000)
            {
                ts.tv_nsec = nanoseconds % 1000000000L;
                ts.tv_sec +=  (nanoseconds / 1000000000L);
            }
            else
                ts.tv_nsec = nanoseconds;
#endif
            ret = pthread_cond_timedwait( &event->cond, &event->lock ,(const struct timespec *)&ts);
            while(ret == 0 && TRUE != event->is_set)
            {
                ret = pthread_cond_timedwait( &event->cond, &event->lock ,(const struct timespec *)&ts);
            }
        }
    }
    pthread_mutex_unlock( &event->lock );
    if( ret != 0 ) //timeout 
        return 0;
    return 1;    
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值