semaphores

Rob Fanner rfanner@stonethree.com
Thu May 30 09:33:00 GMT 2002


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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://sourceware.org/pipermail/pthreads-win32/attachments/20020530/8553b22e/attachment.htm>


More information about the Pthreads-win32 mailing list