caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Thread behaviour
Date: Mon, 7 Oct 2013 16:57:50 +0200	[thread overview]
Message-ID: <20131007145750.GB2035@frosties> (raw)
In-Reply-To: <52493521.4000204@inria.fr>

On Mon, Sep 30, 2013 at 10:24:01AM +0200, Romain Bardou wrote:
> Le 29/09/2013 19:47, Chet Murthy a écrit :
> > 
> >> This is my situation. I don't care if the code runs on a single core
> >> (in fact, I hope it does), but I do want to use threads which are
> >> scheduled reasonably independently and reasonably fairly. My first
> >> example shows that one thread is effectively starved by the other
> >> thread.
> > 
> > Ah.  ok.  In this case, it's easier.  You just need to ensure that in
> > every loop,in every recursive function, there's a call to something
> > that yield()s, on every path.  It's that simple, and that icky.  But
> > then, if you have code that literally doesn't do anything that yields,
> > in a loop, it's compute-intensive, and -that- means you're not really
> > asking for concurrency, are you?
> > 
> > BTW, to your original question "why should the while loop affect
> > scheduling of f's thread": because there is a global operation
> > (scheduling) that needs cooperation from all threads in order to
> > execute.  And that requires explicit coding by the programmer.  Now,
> > the compiler -could- have inserted code to do the yield() (in some old
> > LISPms, it was done at every backward jump and return, I think).
> > 
> > I can't speculate as to why it wasn't done, but given that the goal of
> > ocaml's threads is concurrency, and not parallelism, it isn't common
> > (at least, in my experience) to write code that doesn't naturally
> > reach yield points frequently.
> 
> Unfortunately there is a huge class of such kind of code: code which
> uses libraries which do not yield. For instance, code which loads a DLL
> to communicate with hardware and which may block (or worse).
> 
> Cheers,

But that coulde would be external and you would release the global
lock before the blocking call. That's what e.g. Unix.write does. You
can have many threads that run in parallel. But only one at a time can
run ocaml code. All others can only run external code.

Also your example was bad because in real code you wouldn't
(shouldn't) have a busy loop that does nothing without at least a
yield in it. You assumed the flush would do something that triggeres
the scheduler but that doesn't seem to be the case if the buffer is
empty. Kind of makes sense. If there is nothing to flush then the call
just returns and nothing happens. Only when there is something to
flush it writes data, releasing the runtime lock and scheduling as a
side effect.

MfG
	Goswin

PS: Tip: Use Printf.printf "...%!"

  reply	other threads:[~2013-10-07 14:57 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-27 10:10 Tom Ridge
2013-09-27 10:22 ` Simon Cruanes
2013-09-27 10:27 ` Romain Bardou
2013-09-27 10:51   ` Benedikt Grundmann
2013-09-28 19:09     ` Tom Ridge
2013-09-29  7:54       ` Tom Ridge
2013-09-29 12:37         ` Yaron Minsky
2013-09-29 16:25           ` Tom Ridge
2013-09-29 16:46             ` Chet Murthy
2013-09-29 17:18               ` Tom Ridge
2013-09-29 17:47                 ` Chet Murthy
2013-09-30  8:24                   ` Romain Bardou
2013-10-07 14:57                     ` Goswin von Brederlow [this message]
2013-09-30  8:16       ` Romain Bardou
2013-10-01  3:32         ` Ivan Gotovchits
2013-10-07 14:49       ` Goswin von Brederlow
2013-09-30  9:18 ` Xavier Leroy
2013-09-30 15:12   ` Tom Ridge
2013-09-30 16:01     ` Török Edwin
2013-09-30 16:56     ` Gabriel Kerneis
2013-09-30 18:18       ` Alain Frisch
2013-10-01  5:01   ` Pierre Chambart
2013-10-01  7:21     ` Gabriel Kerneis
2013-10-02 10:37     ` Wojciech Meyer
2013-10-02 11:52       ` Francois Berenger
2013-10-02 11:58         ` Wojciech Meyer

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=20131007145750.GB2035@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).