Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Rich Neswold <rich.neswold@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Smart ways to implement worker threads
Date: Fri, 16 Jul 2010 15:02:48 +0200	[thread overview]
Message-ID: <87630fbmvb.fsf@frosties.localdomain> (raw)
In-Reply-To: <AANLkTik4KwrOX4TvxtLtwjoL0y7GLcsSCdRaanmqH6o_@mail.gmail.com> (Rich Neswold's message of "Thu, 15 Jul 2010 23:23:19 -0500")

Rich Neswold <rich.neswold@gmail.com> writes:

> On Thu, Jul 15, 2010 at 11:02 PM, Goswin von Brederlow <goswin-v-b@web.de>
> wrote:
>
>     Rich Neswold <rich.neswold@gmail.com> writes:
>
>     Thanks. That is about what I got so I do seem to understand the
>     differences right.
>
>     For my use case this would then come down to implement solution 3 with
>     channels instead of my own queues. Well, channels are thread safe queues
>     just by another name. I think I see now how they make the code simpler
>     to write.
>
>
> Channels are a thread-safe communication channel of depth one (i.e. you can
> only pass one item at a time.) The channel is a primitive that allows reliable

Urgs, so what happens if I call "sync (send ...)" twice without the
other end calling recieve? Lets test:

let ch = Event.new_channel ()

let reciever () =
  for i = 0 to 10 do
    Printf.printf "recieved %d\n" (Event.sync (Event.receive ch));
    flush_all ();
    Unix.sleep 2;
  done

let _ =
  ignore (Thread.create reciever ());
  for i = 0 to 10 do
    Printf.printf "sending %d\n" i;
    flush_all ();
    Event.sync (Event.send ch i);
    Unix.sleep 1;
  done

% ocamlopt -thread -o foo unix.cmxa threads.cmxa foo.ml && ./foo
sending 0
recieved 0
sending 1
recieved 1
sending 2
recieved 2
sending 3
recieved 3
...

So the send blocks until the event is recieved. That certainly isn't
helpfull for me. One could say I want asynchronous remote function calls
(and returns).

> synchronized communication between threads. The Reppy book describes in later
> chapters how to use the channel primitive to build up queues and other, more
> complicated constructs (like RPCs and multicasting to many processes.)
>
> In fact, you might use the RPC ideas to pass your checksumming requests to
> worker tasks and receive the results.

Yeah. But then why not build it around the simple Mutex and Condition
modules instead of Event? At first glance the blocking aspect of Events
seem to be more of a hindrance than help.


I find it odd that there is no Threaded_Queue module, a thread save
version of Queue with 2 extra functions:

val wait_and_take : 'a t -> 'a

   wait_and_take q waits for the queue q to be not empty and removes and
   returns the first element in queue q. Raises Empty when the queue is
   closed.

val close : 'a t -> unit

   close q closes the queue and wakes up waiting threads.


MfG
        Goswin


  reply	other threads:[~2010-07-16 13:03 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-14 16:09 Goswin von Brederlow
2010-07-15 15:58 ` [Caml-list] " Rich Neswold
2010-07-15 16:19   ` David McClain
2010-07-15 17:16   ` Ashish Agarwal
2010-07-15 18:24   ` Goswin von Brederlow
2010-07-15 18:37     ` David McClain
2010-07-15 18:40     ` David McClain
2010-07-15 19:56     ` Rich Neswold
2010-07-16  4:02       ` Goswin von Brederlow
2010-07-16  4:23         ` Rich Neswold
2010-07-16 13:02           ` Goswin von Brederlow [this message]
2010-07-16 14:40             ` Dawid Toton
2010-07-16 16:18             ` [Caml-list] " Rich Neswold
2010-07-17 17:53               ` Eray Ozkural
2010-07-20  4:54             ` Satoshi Ogasawara
2010-07-17 18:34         ` Eray Ozkural
2010-07-17 19:35           ` Goswin von Brederlow
2010-07-17 22:00             ` Eray Ozkural
2010-07-15 16:32 ` Romain Beauxis
2010-07-15 17:46   ` Goswin von Brederlow
2010-07-15 18:44     ` Romain Beauxis
2010-07-16  3:52       ` Goswin von Brederlow
2010-07-16  4:19         ` Romain Beauxis
2010-07-16 13:05           ` Goswin von Brederlow
2010-07-16 13:20             ` Romain Beauxis
2010-07-17  9:07               ` Goswin von Brederlow
2010-07-17 13:51                 ` Romain Beauxis
2010-07-17 14:08                   ` Goswin von Brederlow
2010-07-17  9:52 ` Goswin von Brederlow
2010-07-17 14:20   ` Romain Beauxis
2010-07-17 15:52     ` Goswin von Brederlow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87630fbmvb.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=rich.neswold@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox