This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

sem_trywait never returns EAGAIN


Hi,

>From the Linux man pages I concluded that calling sem_trywait on a semaphore with a zero count should result in an EAGAIN return value. I get a -1 all time 
time. Intiializing the count with 1 and calling sem_trywait two times results in 0 and -1 return codes. Initializing with 0, calling sem_trywait results in a -1 return 
code. Initializing with 0, calling sem_post, and sem_trywait twice results in 0 and -1 return codes for the sem_trywait calls.

Are my expectations wrong or is the behaviour of the library wrong?

Here is my sample code:

#include <pthread.h>
#include <semaphore.h>
#include <cassert>
#include <cstdio>

#pragma comment(lib, "pthreadvce")

int main(int argc, char** argv)
{
  sem_t s;
  assert(sem_init(&s, 0, 0) == 0);
  int result = sem_trywait(&s);
  if ( result == -1 )
  {
    perror("sem_wait"); // No error
  }
  else
  {
    printf("ok\n");
  }

  result = sem_post(&s);

  result = sem_trywait(&s);
  if ( result == -1 )
  {
    perror("sem_wait");
  }
  else
  {
    printf("ok\n");
  }

  return 0;
}
  
  

	
--
Patrick Frants
Senior Software Engineer
Quintiq
patrick@quintiq.com
www.quintiq.com



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]