<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/1eefe810f7f0f0aa410a2eab708b2234"
  from="Artem Prisyaznuk &lt;tema@s...&gt;"
  author="Artem Prisyaznuk"
  date="2002-07-06T14:18:21"
  subject="[Caml-list] Problem with threads &amp; netclient"
  prev="2002/07/905e2118bb1242c688e9904a4ace9afc"
  next="2002/07/746ede35d7b862c4b263842d67508891"
  next-in-thread="2002/07/55f251b04e7d3d2f236094ccb9aebe84"
  prev-thread="2002/07/e1d274a133b68487086df9bb59b11307"
  next-thread="2002/07/746ede35d7b862c4b263842d67508891"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Problem with threads &amp; netclient">
<msg 
  url="2002/07/1eefe810f7f0f0aa410a2eab708b2234"
  from="Artem Prisyaznuk &lt;tema@s...&gt;"
  author="Artem Prisyaznuk"
  date="2002-07-06T14:18:21"
  subject="[Caml-list] Problem with threads &amp; netclient">
<msg 
  url="2002/07/55f251b04e7d3d2f236094ccb9aebe84"
  from="John Max Skaller &lt;skaller@o...&gt;"
  author="John Max Skaller"
  date="2002-07-06T17:15:23"
  subject="Re: [Caml-list] Problem with threads &amp; netclient">
</msg>
</msg>
</thread>

<contents>
I write function for multithread list maping.
I use it for get some web pages. But sometime it's don't 
work properly, after get a several hundred pages,
my program exit by segfault. 

I compile it under Linux, with pthread, and I use netclient
library, Ocaml version 3.04.

Program here:
============================================================
type 'a th_res = Init | Val of 'a | Exn of exn;;

let map ?(max = 10) (fun_t : 'a -&gt; 'b) (lst : 'a list) =
  let mtx_count = Mutex.create () in
  let count = ref 0 in
  let count_incr () =
    Mutex.lock mtx_count; incr count; Mutex.unlock mtx_count
  in
  let count_decr () =
    Mutex.lock mtx_count; decr count; Mutex.unlock mtx_count
  in
  let cond_exit = Condition.create () in
  let thr_f ref_res f arg =
    ref_res :=
      begin try Val (f arg) with
        ex -&gt; prerr_endline (Printexc.to_string ex); flush stderr; Exn ex
      end;
    count_decr ();
    Condition.signal cond_exit
  in
  let thds =
    List.map
      (fun arg -&gt;
         let res = ref Init in
         Mutex.lock mtx_count;
         if !count &lt; max then () else Condition.wait cond_exit mtx_count;
         Mutex.unlock mtx_count;
         try
           let r = Some (Thread.create (thr_f res fun_t) arg), res in
           incr count; r
         with
           ex -&gt;
             prerr_endline (Printexc.to_string ex);
             flush stderr;
             res := Exn ex;
             None, res)
      lst
  in
  List.map
    (fun (th, res) -&gt;
       begin match th with
         Some t -&gt; Thread.join t
       | None -&gt; ()
       end;
       !res)
    thds
;;
let prlog s = print_string s; flush stdout;;
open Http_client;;
let http_get link =
  let get = new get link in
  let pipe = new pipeline in
  pipe#add get;
  begin try pipe#run () with
    any -&gt; pipe#reset (); raise any
  end;
  pipe#reset ();
  get#get_resp_body ()
;;

let f (i, adr) =
  Printf.printf "."; flush stdout; String.length (http_get adr)
;;

let make_lst adr len =
  let rec mk_aux len accum =
    if len = 0 then accum else mk_aux (len - 1) ((len, adr) :: accum)
  in
  mk_aux len []
;;
 
let test count url =
  let args = make_lst url count in
  let res = map ~max:4 f args in
  print_endline "=================\n";
  List.iter2
    (fun (i, a) r -&gt;
       match r with
         Val r -&gt; Printf.printf "%3d. %s = %d\n" i a r
       | Init -&gt; Printf.printf "%3d. %s = Init\n" i a
       | Exn ex -&gt;
           Printf.printf "%3d. %s = Exn %s\n" i a (Printexc.to_string ex))
    args res
;;
let url = ref "";;
let count = ref 0;;
let replay = ref 0;;
let spec =
  ["-u", Arg.String (fun s -&gt; url := s), "- url";
   "-c", Arg.Int (fun i -&gt; count := i), "- size of list";
   "-r", Arg.Int (fun r -&gt; replay := r), "- count of repaly"]
;;
Arg.parse spec (fun _ -&gt; ()) "Test Threads\n";;

for i = 1 to !replay do
  test !count !url;
  print_endline "\n\nnext_loop *************************\n\n"
done;;
============================================================

I call it:

	./prog -l 3000 -c 3 -u http://localhost/manual/index.html

Where's problem?

-- 
Artem Prisyaznuk
tema@sit.kiev.ua
-------------------
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

</contents>

</message>

