3

I try to implement pthread functionality in my code. Unfortunately, I am not be able to implement correctly function pthread_cond_timedwait(). In Linux everything works fine. But in Windows this function always returns error code 10060. Here is my simple code:

#include <fstream>
#include <Windows.h>
#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>

int main()
{
  int rcTimedwait = 0;
  struct timespec timeout;
  pthread_mutex_t mutex;
  pthread_cond_t condVar;
  pthread_mutex_init(&mutex, NULL);
  pthread_cond_init(&condVar, NULL);
  timeout.tv_sec = 1;
  timeout.tv_nsec = 0;
  SetLastError(0);
  errno = 0;
  pthread_mutex_lock(&mutex);
  rcTimedwait = pthread_cond_timedwait(&condVar, &mutex, &timeout);
  printf("rcTimedwait = %d\n", rcTimedwait);
  printf("errno = %d   GetLastError = %d\n", errno, GetLastError());
  printf("tv_sec = %d   tv_nsec = %d\n", timeout.tv_sec, timeout.tv_nsec);
  pthread_mutex_unlock(&mutex);
  pthread_cond_destroy(&condVar);
  pthread_mutex_destroy(&mutex);

  return 0;
}

and output:

rcTimedwait = 10060
errno = 0   GetLastError = 0
tv_sec = 1   tv_nsec = 0

Thanks in advance for any help and sorry for my English

Yann Droneaud
  • 5,277
  • 1
  • 23
  • 39
Vlad
  • 31
  • 4

1 Answers1

2

pthread_cond_timedwait() returned 10060, which looks like the value for WSAETIMEDOUT. I'm surprised the function doesn't return ETIMEDOUT as expected.

Anyway, it's a timeout, which is not surprising since in your example there's no other thread to signal the condition variable.

Yann Droneaud
  • 5,277
  • 1
  • 23
  • 39