This is the mail archive of the 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]

Re: semaphores

Thanks for the advice. There is one error in the
original code frag, though:
(taken from the Linux threads man pages)
The sem_wait and sem_getvalue functions always return 0.
So, the assert() on sem_getvalue() should always have worked,
under the linux threads documentation.
pthreads-win32, however, seems to return -1 on failure (which
is ok for the code frag) 
if ( result != 0 )
      errno = result;
      return -1;
However, this indicates that the sem_getvalue() implementation in pthreads-win32, or my compilation, is not quite as it should be.
Thanks to J Bossom for his suggestion. The following fragment
compiles and runs just fine:
#include <semaphore.h>
#include <cassert>
#include <iostream>
using namespace std;
int main()
    * We want to check wether a semaphore can be initialised
    * with a value.
   // a semaphore
   sem_t psem;
   // initialise it with value 10
   assert(sem_init(&psem,0,10) == 0);
   // if the semaphore initialisation was ok, the sem
   // should now have the value 10
   // trying out J Bossom's idea of counting down
   // using a trywait
   int cnt=0;
   while (sem_trywait(&psem)==0) cnt++;
   cout << "Final value of cnt is " << cnt << endl << flush;
   return 0;
This indicates that the sem_init() and sem_trywait(), functions
are OK.

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