From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id D01C6BC57 for ; Tue, 13 Apr 2010 22:34:19 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArIAAJ5xxEuM9zwYkWdsb2JhbACDFIxPi1odAQEBAQcNCgcRBR2sOYh0iF2EH24E X-IronPort-AV: E=Sophos;i="4.52,199,1270418400"; d="scan'208";a="60654483" Received: from bowser.eecs.harvard.edu (HELO mail.eecs.harvard.edu) ([140.247.60.24]) by mail4-smtp-sop.national.inria.fr with ESMTP; 13 Apr 2010 22:34:19 +0200 Received: from mail-wy0-f180.google.com (mail-wy0-f180.google.com [74.125.82.180]) (using TLSv1 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by mail.eecs.harvard.edu (Postfix) with ESMTP id A28441B003F for ; Tue, 13 Apr 2010 16:33:29 -0400 (EDT) Received: by wyf22 with SMTP id 22so1399072wyf.39 for ; Tue, 13 Apr 2010 13:34:16 -0700 (PDT) MIME-Version: 1.0 Reply-To: gmalecha@cs.harvard.edu Received: by 10.216.6.90 with HTTP; Tue, 13 Apr 2010 13:33:46 -0700 (PDT) From: Gregory Malecha Date: Tue, 13 Apr 2010 16:33:46 -0400 Received: by 10.216.90.17 with SMTP id d17mr539541wef.117.1271190856122; Tue, 13 Apr 2010 13:34:16 -0700 (PDT) Message-ID: Subject: Threads Scheduling To: caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=0016e6d7ee6f6bc12c0484242f7d X-Spam: no; 0.00; 0.3:98 0.3:98 threads:01 threads:01 terminates:01 terminates:01 terminate:01 terminate:01 eecs:03 seems:03 seems:03 mutex:03 mutex:03 library:03 library:03 X-Attachments: cset="UTF-8" cset="UTF-8" --0016e6d7ee6f6bc12c0484242f7d Content-Type: text/plain; charset=UTF-8 Hello, I'm trying to write a function (run_guarded) that takes another function (f), and runs it for some amount of time. If f terminates with value x, then the result of run_guarded should be Some x, otherwise it should be None. Here's my implementation using the Thread library: let run_guarded f x = let res = ref None in let m = Mutex.create () in let c = Condition.create () in let _ = Mutex.lock m in let tid1 = Thread.create (fun x -> let z = f x in let _ = res := Some z in let _ = Mutex.lock m in let _ = Mutex.unlock m in let _ = Condition.broadcast c in ()) x and tid2 = Thread.create (fun () -> let _ = Thread.delay 0.3 in let _ = Mutex.lock m in let _ = Mutex.unlock m in let _ = Condition.broadcast c in ()) () in let _ = Condition.wait c m in let _ = Mutex.unlock m in let _ = try Thread.kill tid2 with _ -> () in let _ = try Thread.kill tid1 with _ -> () in !res It seems like it should work, but it doesn't work if the function f doesn't terminate. It seems to be running everything serially. I know that threads aren't actually parallel, but I thought they were preemptive in which case it seems like this should work. Does anyone know what I did wrong here? Thank you very much. -- gregory malecha --0016e6d7ee6f6bc12c0484242f7d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello,

I'm trying to write a function (run_guarded) = that takes another function (f), and runs it for some amount of time. If f = terminates with value x, then the result of run_guarded should be Some x, o= therwise it should be None. Here's my implementation using the Thread l= ibrary:

let run_guarded f x =3D=C2=A0
=C2=A0=C2= =A0let res =3D ref None in
=C2=A0=C2=A0let m =C2=A0 =3D Mutex.cre= ate () in
=C2=A0=C2=A0let c =C2=A0 =3D Condition.create () in
=C2=A0=C2=A0let _ =3D Mutex.lock m in
=C2=A0=C2=A0let tid1= =3D Thread.create (fun x ->=C2=A0
=C2=A0=C2=A0 =C2=A0let z =3D f x in
=C2=A0=C2=A0 =C2=A0let _= =3D res :=3D Some z in
=C2=A0=C2=A0 =C2=A0let _ =3D Mutex.lock m= in
=C2=A0=C2=A0 =C2=A0let _ =3D Mutex.unlock m in
=C2= =A0=C2=A0 =C2=A0let _ =3D Condition.broadcast c in
=C2=A0=C2=A0 = =C2=A0()) x
=C2=A0=C2=A0and tid2 =3D Thread.create (fun () -><= /div>
=C2=A0=C2=A0 =C2=A0let _ =3D Thread.delay 0.3 in
=C2=A0=C2= =A0 =C2=A0let _ =3D Mutex.lock m in
=C2=A0=C2=A0 =C2=A0let _ =3D = Mutex.unlock m in
=C2=A0=C2=A0 =C2=A0let _ =3D Condition.broadcas= t c in
=C2=A0=C2=A0 =C2=A0()) ()
=C2=A0=C2=A0in
=C2=A0=C2=A0let _ =3D Condition.wait c m in
=C2=A0=C2=A0let _ =3D Mutex.unlock m in
=C2=A0=C2=A0let _ = =3D try Thread.kill tid2 with _ -> () in
=C2=A0=C2=A0let _ =3D= try Thread.kill tid1 with _ -> () in
=C2=A0=C2=A0!res

It seems like it should work, but it doesn't work if = the function f doesn't terminate. It seems to be running everything ser= ially. I know that threads aren't actually parallel, but I thought they= were preemptive in which case it seems like this should work. Does anyone = know what I did wrong here?

Thank you very much.

--
gregory= malecha
--0016e6d7ee6f6bc12c0484242f7d--