Linux教程

Linux sem_trywait实现sem_timedwait

本文主要是介绍Linux sem_trywait实现sem_timedwait,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  • sem_timedwait说明

          int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);

          其中第2个参数 struct timespec *abs_timeout要求为绝对时间,如果手动将系统时间往后修改会导致sem_timedwait长时间阻塞

  • sem_trywait说明

         int sem_trywait(sem_t *sem);

         sem_trywait与 sem_wait() 类似,若信号不可用,则返回错误(errno 设置为EAGAIN)而不是阻塞

  • sem_timedwait自定义实现
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <semaphore.h>
#include <errno.h>


unsigned long long GetClockTimeMS( void )
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000;
    time += curTime.tv_nsec/1000000;
    return time;
}

unsigned long long GetClockTimeUS( void )
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000000;
    time += curTime.tv_nsec/1000;
    return time;
}

unsigned long long GetClockTimeNS(void)
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000000000;
    time += curTime.tv_nsec;
    return time;
}

int SleepEx(int us)
{
    struct timeval timeout;

    timeout.tv_sec = us/1000000;
    timeout.tv_usec = us%1000000;
    if(-1 == select(0, NULL, NULL, NULL, &timeout))
    {
        return errno;
    }
    return 0;
}

int WaitEvent(sem_t *hEvent, unsigned int milliseconds)
{
    int ret;
#if 0
        struct timespec timeout = { 0 };
#ifdef _DEBUG
        ret = clock_gettime(CLOCK_REALTIME, &timeout);
#else
        clock_gettime(CLOCK_REALTIME, &timeout);
#endif//_DEBUG
        timeout.tv_sec += (milliseconds / 1000);
        timeout.tv_nsec += ((milliseconds % 1000) * 1000000);
        if (0 == sem_timedwait((sem_t *)hEvent, &timeout))
        {
            return 0;
        }
        return errno;
#else
        unsigned long long timeout = milliseconds*1000;//微秒
        unsigned long long beg = GetClockTimeUS();
        unsigned long long cur;
        do 
        {
            if (0 == sem_trywait((sem_t *)hEvent))
            {
                return 0;
            }
            ret = errno;
            if (EAGAIN != ret)
            {
                return ret;
            }
            cur = GetClockTimeUS();
            if (cur - beg >= timeout)
            {
                break;
            }
            SleepEx(1);
        } while (1);
        return ETIMEDOUT;
#endif
}

int main(void)
{
    sem_t event;
    sem_init(&event, 0, 0);
    unsigned long long begUS = GetClockTimeUS();
    WaitEvent(&event, 2000);
    printf("end %llu..\n", GetClockTimeUS() - begUS);
    return 0;
}

 

这篇关于Linux sem_trywait实现sem_timedwait的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!