<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Thanks for the advice. There is one error in the
</FONT></DIV>
<DIV><FONT face=Arial size=2>original code frag, though:</FONT></DIV>
<DIV><FONT face=Arial size=2>(taken from the Linux threads man
pages)</FONT></DIV>
<DIV><FONT face=Arial size=2>The <B>sem_wait</B> and <B>sem_getvalue</B>
functions always return 0. </FONT></DIV>
<DIV><FONT face=Arial size=2>So, the assert() on sem_getvalue() should always
have worked,</FONT></DIV>
<DIV><FONT face=Arial size=2>under the linux threads documentation.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>pthreads-win32, however, seems to return -1
on failure (which</FONT></DIV>
<DIV><FONT face=Arial size=2>is ok for the code frag)</FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>if ( result != 0 )<BR>
{<BR> errno =
result;<BR> return -1;<BR>
}<BR> </FONT></DIV>
<DIV><FONT face=Arial size=2>However, this </FONT><FONT face=Arial
size=2>indicates that the sem_getvalue() implementation in pthreads-win32, or my
compilation, is not quite as it should be.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Thanks to J Bossom for his suggestion. The
following fragment </FONT></DIV>
<DIV><FONT face=Arial size=2>compiles and runs just fine:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>#include <semaphore.h><BR>#include
<cassert><BR>#include <iostream></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>using namespace std;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>int main()<BR>{<BR>
/**<BR> * We want to check wether a semaphore can be
initialised<BR> * with a value.<BR>
*/<BR> <BR> // a semaphore<BR> sem_t
psem;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> // initialise it with value
10<BR> assert(sem_init(&psem,0,10) == 0);<BR> // if
the semaphore initialisation was ok, the sem<BR> // should now have
the value 10</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> // trying out J Bossom's idea of
counting down<BR> // using a trywait<BR> int
cnt=0;<BR> while (sem_trywait(&psem)==0) cnt++;<BR>
cout << "Final value of cnt is " << cnt << endl <<
flush;<BR> <BR> return 0;<BR>}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>This indicates that the sem_init() and
sem_trywait(), functions</FONT></DIV>
<DIV><FONT face=Arial size=2>are OK.</FONT></DIV>
<DIV> </DIV></BODY></HTML>