Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_cond_timedwait() in Windows

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

like image 452
Vlad Avatar asked Sep 13 '26 14:09

Vlad


1 Answers

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.

like image 50
Yann Droneaud Avatar answered Sep 15 '26 04:09

Yann Droneaud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!