Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Automatic wrapper generator
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: thornber@r...
Subject: Re: [Caml-list] Functional critical section
On Tue, May 18, 2004 at 04:10:44PM -0400, SWAMPY wrote:
>     Then, I could do something like:
> 
>     critical (threadsafeFunction arg1 arg2 arg3)
> 
>     and have this do the equivalent of
> 
>     Mutex.lock someMutex; let tmp = threadsafeFunction arg1
> arg2 arg3; Mutex.unlock someMutex; tmp;;

I use a 'with_resource' function:

| let try_finally fn fin =
|   try
|     let result = fn () in
|     fin ();
|     result;
|   with x -> fin (); raise x
| 
| let with_resource resource fn post =
|   try_finally (fun () -> fn resource) (fun () -> post resource)


Which could be used to make a 'with_file' or 'with_mutex' fn:

| let with_file file fn =
|   with_resource (open_in file) fn close_in
|
| let with_mutex fn =
|   with_resource (Mutex.lock someMutex) fn Mutex.unlock

- Joe

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners