sem_timedwait () pthreads-win32 errno use - PullRequest
       43

sem_timedwait () pthreads-win32 errno use

0 голосов
/ 23 сентября 2018

Я использую pthreads-win32 для потоков в приложении Win32.Я рассчитываю временной интервал, используя первую опубликованную функцию здесь .

Вызов sem_timedwait(), кажется, ожидает указанное время ts, однако каждый раз, когда он завершается, я получаюследующее сообщение:

Ошибка ожидания на семафор: Нет ошибки

Я проверил файл sem_timedwait.c здесь , и они указывают то же самоеошибочные значения и возвращаемые значения.Следовательно, я не знаю, почему это происходит с этой ошибкой, и хотел бы знать, почему.

#ifdef _WIN32

#define HAVE_STRUCT_TIMESPEC

#endif // _WIN32  

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>

#include "include.h"

// Platform includes
#ifdef _WIN32

#include <sched.h>
#include <windows.h>

#else

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#endif  // _WIN32

// ...

sem_init(&job_semaphore, 0, 0);

// ...

// Set wait time relative to current time (semaphore wait time 10s)
#ifdef _WIN32

        clockGetTime(0, &ts);

#else

        if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
            perror("Error getting current time");
        }

#endif  // _WIN32

        ts.tv_sec += 10;

        while ((s = sem_timedwait(&job_semaphore, &ts)) == -1 && errno == EINTR) {

            // Restart if interrupted by handler
            continue;
        }

        // Timeout or error occurred
        if (s == -1) {
            if (errno != ETIMEDOUT) {
                perror("Error waiting on semaphore");
            }
        }
        // Job to process
        else {

        }

Код для clockGetTime

#define HAVE_STRUCT_TIMESPEC
#include <windows.h>
#include <pthread.h>

int clockGetTime(int clock_filler, struct timespec *spec) {  //C-file part

    __int64 wintime; GetSystemTimeAsFileTime((FILETIME*)&wintime);
    wintime -= 116444736000000000i64;               //1jan1601 to 1jan1970
    spec->tv_sec =(time_t) (wintime / 10000000i64);           //seconds
    spec->tv_nsec =(long) (wintime % 10000000i64 * 100);    //nano-seconds

    return 0;
}
...