From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 35683BB91 for ; Wed, 12 Jan 2005 18:29:33 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j0CHTWQ5003177 for ; Wed, 12 Jan 2005 18:29:32 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA30909 for ; Wed, 12 Jan 2005 18:29:32 +0100 (MET) Received: from smtp004.mail.ukl.yahoo.com (smtp004.mail.ukl.yahoo.com [217.12.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j0CHTVBr023237 for ; Wed, 12 Jan 2005 18:29:32 +0100 Received: from unknown (HELO ?10.0.0.115?) (pasckosky@62.101.126.224 with plain) by smtp004.mail.ukl.yahoo.com with SMTP; 12 Jan 2005 17:29:31 -0000 Message-ID: <41E55E70.2080309@barettadeit.com> Date: Wed, 12 Jan 2005 18:29:20 +0100 From: Luca Pascali User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041007 Debian/1.7.3-5 X-Accept-Language: en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: [Caml-list] Mutex and posix References: <41E501B4.4060601@yahoo.it> <20050112155607.GB8652@yquem.inria.fr> In-Reply-To: <20050112155607.GB8652@yquem.inria.fr> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 41E55E7C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 41E55E7C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 posix:01 wrote:01 posix:01 threads:01 api:01 threads:01 polling:01 mutexes:01 mutexes:01 ocaml:01 polling:01 baretta:01 baretta:01 ...:98 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=1.8 required=5.0 tests=RCVD_IN_BL_SPAMCOP_NET autolearn=disabled version=3.0.0 X-Spam-Level: * Xavier Leroy wrote: >[...] >The latter is a recent addition to the POSIX threads API -- it's not >in the original POSIX threads spec (POSIX 1003.1c-1995). I wouldn't >rely on this function being available in all POSIX threads >implementations. > > > I didn't know this. >>Polling continously is different. If I have two threads that are running >>with scantimes one multiple of the other, it is possible that one of the >>two threads (the slower one) fails always or almost always the try_lock >>command. >> >> > >It's hard to give useful suggestions without knowing more about your >application, but it could be the case that you're using mutexes to do >things they are not really designed for, i.e. plain mutual exclusion, >for which neither trylock nor timedlock are needed. > >Maybe your application needs a more complex but better suited >synchronization mechanism, which can generally be built on top of >mutexes and conditions, or (at a higher semantic level) Concurrent >ML-style events. > >- Xavier Leroy > > > I haven't an application. I was just thinking about how I could port some programs organizations into Ocaml. The case that rised my question was: If I have a shared resource (let's say a pipe or a queue, or a generic file descriptor) for performing, for example, asyncronous communication between threads, it's quite dangerous in terms of deadlocks to perform myself a polling or locking one thread indefinetly until the resource is freed. Polling with try_lock can bring to a deadlock or to a long freezing if one thread locks the mutex periodically, let's say, every 100 ms for 10 ms and the other one looks for the same mutex periodically about every 1200 ms. The probabilty to fall into the locking period is incredibly high. If I'm not wrong, the timed mutex is handled by the operating system, so when the mutex is released the OS directly assigns it to the first waiting requesting thread, at least if the mutex cannot be locked during the specified time, function returns. Anyway, knowing that this function is not actually available (I hope it will be in the future), it's obvious that I will look for another solution, maybe changing the syncronization method. Thanks for the answer Luca -- ********************************************************************* Luca Pascali luca@barettadeit.com asxcaml-guru@barettadeit.com http://www.barettadeit.com/ Baretta DE&IT A division of Baretta SRL tel. 02 370 111 55 fax. 02 370 111 54 Our technology: http://www.asxcaml.org/ http://www.freerp.org/