From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19553 invoked by alias); 30 May 2002 16:33:18 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 19503 invoked from network); 30 May 2002 16:33:15 -0000 Received: from unknown (HELO mail.twc-online.net) (66.8.86.35) by sources.redhat.com with SMTP; 30 May 2002 16:33:15 -0000 Received: from mondriaan.stonethree.com (cache-1.twc-online.net [66.8.86.58]) by mail.twc-online.net (8.11.3/8.11.3) with ESMTP id g4UGXDR25861; Thu, 30 May 2002 18:33:13 +0200 Received: from thor (thor.stonethree.com [192.168.1.12]) by mondriaan.stonethree.com (8.11.0/8.11.0) with SMTP id g4UGX5302158; Thu, 30 May 2002 18:33:05 +0200 Message-ID: <010a01c207f7$85bb0ce0$0c01a8c0@thor> From: "Rob Fanner" To: "Bossom, John" , References: <430F887D415DD1118C2700805F31ECF106B591B1@sota0005.cognos.com> Subject: Re: semaphores Date: Thu, 30 May 2002 09:33:00 -0000 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0107_01C20808.48AE2C90" X-Priority: 3 X-MSMail-Priority: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-SW-Source: 2002/txt/msg00068.txt.bz2 This is a multi-part message in MIME format. ------=_NextPart_000_0107_01C20808.48AE2C90 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-length: 1370 Thanks for the advice. There is one error in the=20 original code frag, though: (taken from the Linux threads man pages) The sem_wait and sem_getvalue functions always return 0.=20 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)=20 if ( result !=3D 0 ) { errno =3D result; return -1; } =20 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=20 compiles and runs just fine: #include #include #include using namespace std; int main() { /** * We want to check wether a semaphore can be initialised * with a value. */ =20=20=20 // a semaphore sem_t psem; // initialise it with value 10 assert(sem_init(&psem,0,10) =3D=3D 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=3D0; while (sem_trywait(&psem)=3D=3D0) cnt++; cout << "Final value of cnt is " << cnt << endl << flush; =20 return 0; } This indicates that the sem_init() and sem_trywait(), functions are OK. ------=_NextPart_000_0107_01C20808.48AE2C90 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-length: 3271
Thanks for the advice. There is one error = in the=20
original code frag, though:
(taken from the Linux threads man=20 pages)
The sem_wait and sem_getvalue=20 functions always return 0.
So, the assert() on sem_getvalue() should = always=20 have worked,
under the linux threads documentation.
 
pthreads-win32, however, seems to ret= urn -1=20 on failure (which
is ok for the code frag) =
 
if ( result !=3D 0 )
   = =20 {
      errno =3D=20 result;
      return -1;
   = =20 }
 
However, this indicates that the sem_getvalue() implementation in pthreads-win32= , or my=20 compilation, is not quite as it should be.
 
Thanks to J Bossom for his suggestion. The= =20 following fragment
compiles and runs just fine:
 
#include <semaphore.h>
#include=20 <cassert>
#include <iostream>
 
using namespace std;
 
int main()
{
  =20 /**
    * We want to check wether a semaphore can be=20 initialised
    * with a value.
   =20 */
  
   // a semaphore
   sem_t=20 psem;
 
   // initialise it with value=20 10
   assert(sem_init(&psem,0,10) =3D=3D 0);
 &nbs= p; // if=20 the semaphore initialisation was ok, the sem
   // should now = have=20 the value 10
 
   // trying out J Bossom's idea= of=20 counting down
   // using a trywait
   int=20 cnt=3D0;
   while (sem_trywait(&psem)=3D=3D0) cnt++;
&n= bsp; =20 cout << "Final value of cnt is " << cnt << endl <<= =20 flush;
 
   return 0;
}
 
This indicates that the sem_init() and=20 sem_trywait(), functions
are OK.
 
------=_NextPart_000_0107_01C20808.48AE2C90--