From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 907987EE4C for ; Tue, 1 Oct 2013 13:30:33 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=pra; client-ip=212.227.126.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=mailfrom; client-ip=212.227.126.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@moutng.kundenserver.de designates 212.227.126.171 as permitted sender) identity=helo; client-ip=212.227.126.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="postmaster@moutng.kundenserver.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiECAJixSlLU436rlWdsb2JhbABagz+De71ZgTQWDgEBAQEHDQkJEiqCJQEBBAEjBC4kBQsLQgICVwYTCRKHZQoIqgSSRYk1hXYmB4JqgTgDjnuPN45t X-IPAS-Result: AiECAJixSlLU436rlWdsb2JhbABagz+De71ZgTQWDgEBAQEHDQkJEiqCJQEBBAEjBC4kBQsLQgICVwYTCRKHZQoIqgSSRYk1hXYmB4JqgTgDjnuPN45t X-IronPort-AV: E=Sophos;i="4.90,1013,1371074400"; d="asc'?scan'208";a="28682744" Received: from moutng.kundenserver.de ([212.227.126.171]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 01 Oct 2013 13:30:33 +0200 Received: from office1.lan.sumadev.de (dslb-088-069-134-081.pools.arcor-ip.net [88.69.134.81]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0M7Fp4-1VnQzB39WA-00xKSC; Tue, 01 Oct 2013 13:30:32 +0200 Received: from [192.168.0.110] (ip-109-90-191-98.unitymediagroup.de [109.90.191.98]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 3C7EDC00CF; Tue, 1 Oct 2013 13:30:31 +0200 (CEST) Message-ID: <1380627030.23931.15.camel@zotac> From: Gerd Stolpmann To: Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com> Cc: caml-list Date: Tue, 01 Oct 2013 13:30:30 +0200 In-Reply-To: <5249C348.9070408@gmail.com> References: <5249C348.9070408@gmail.com> Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-FnPDJ6p6X6g+O9tm7VH+" X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 X-Provags-ID: V02:K0:8zzXZrBeKWbt6mu0elGOHfX0tdp1Fu9Cy/NqnxZv2Yn dvlvICoJwVoDbaAebFID0QlnUVddiw6dl5sLWMbsM1vmIJz+Ux NVddH7NK/jnX/+pObhXgTfjmIzhfW8oKtRk8jtdWUB0o/vp2no mANDTi0kaWb5S6krFrJ9uZjtKU9q2g3GI8oeeCEZbfIA5rcTfo He3W6hk9ulWkQrGCD4ht22oRMPaHZxbc06eLSOaDrqLfA60U7I vZDWISM6T1zfWIZ6s6Dly5YEf7Z0sYwmYxVdE0oO89IV80drXB Rj/aEYeZ3K7Jx+FZCj1lUKkSP+Akg7gLv6PcyXUoQyganzrNbt a6k3/cLmFwWDV5hvLJnI= Subject: Re: [Caml-list] semaphore puzzle --=-FnPDJ6p6X6g+O9tm7VH+ Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, Ocamlnet includes support for semaphores (at least for Unix platforms).=20 Am Montag, den 30.09.2013, 19:30 +0100 schrieb Matej Kosik: > Hi, >=20 > I am trying to find the most simple and, if possible, clean way to synchr= onize my threads. >=20 > In this particular case, semaphores would be ideal > (alternatively, unbounded channels) >=20 > (* Create a new semaphore with a given initial value. *) > val create : int -> t Netsys_posix.sem_create (or sem_open or sem_init). >=20 > (* V *) > val up : t -> unit Netsys_posix.sem_post > (* P *) > val down : t -> unit Netsys_posix.sem_wait with option SEM_WAIT_BLOCK. > val try_down : t -> bool Netsys_posix.sem_wait with option SEM_WAIT_NONBLOCK. > The "down" or "try_down" are almost the solutions, but not quite. >=20 > "down" may block forever ---> there is no timeout at all > (I need some) >=20 > "try_down" ---> the timeout is zero > (I prefer a non-zero timeout) >=20 > Obviously, I could use "try_down" in a loop, checking the system time mys= elf and then either give up or actually manage to decrement the semaphore. >=20 > Is there a better solution than this? The POSIX API specifies sem_timedwait, but I haven't included it in my bindings, as it is optional in POSIX. What you could do as workaround is to start a helper thread which will sem_post after the timeout. I admit that it is difficult to find a clean solution here without race conditions. > (There is a Unix module, there are signals, but I am not sure whether it = is safe to use them in multithreaded program. > At least, I did not have a luck.) The signal handling as provided by Unix (or better by the OCaml runtime) is not sufficient for this kind of programming. An alternative might be to use a pipe as returned by Unix.pipe. The number of bytes in the pipe buffer is the semaphore counter: post =3D add a byte to the buffer wait =3D read a byte from the buffer You can use Unix.select to specify a timeout when waiting. The solution works as long as the pipe buffer doesn't fill up (at least 4096 bytes are guaranteed). You can totally avoid that if you combine the pipe with an additional counter (i.e. semaphore counter =3D pipe buffer + additional counter), and you use the pipe only when the additional counter is 0. Gerd >=20 > Thanks in advance for patience &| help. >=20 --=20 ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de My OCaml site: http://www.camlcity.org Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de ------------------------------------------------------------ --=-FnPDJ6p6X6g+O9tm7VH+ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAABAgAGBQJSSrJWAAoJEAaM4b9ZLB5T+sEH/1LFJk6uo9bfrEP5ZOwrZvF8 MjtF51ynFM9C+ljdDiX9G1oImdvYsdekJOsqaALf6t1DN13J8MESTVm988r1vwg1 EpPVXfAu5ZQDr9T8BBkw+tVjuB6/r2OwqGgM5AJiq3RS6w1aGjI0O6XrBSUvCapG ssiFTDWF6diEuyCO9qU4yB04qTafC9mhtQAjcQQDJ4vIimzGt/Bf79Pb0CZcDSdr rbP1DjwiZWTl08MaiTgaH/liQLAZcYd60j80PtGSU38/4aC6JPLIASkETaZtUDMW YTHAUCUi5ybbAg0qACVgClgFwuFRxeaI6/S/E5gdpneabaJpCuQdBy48AssBW1k= =ayiT -----END PGP SIGNATURE----- --=-FnPDJ6p6X6g+O9tm7VH+--