caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tom Ridge <tom.j.ridge+caml@googlemail.com>
To: Chet Murthy <murthy.chet@gmail.com>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Thread behaviour
Date: Sun, 29 Sep 2013 18:18:16 +0100	[thread overview]
Message-ID: <CABooLwPrO6Zrk_pQgPEM5snkg1YALwUQn+C5wHfUWFO_C0f_Xg@mail.gmail.com> (raw)
In-Reply-To: <23010395.NVkQDdK53E@groupon>

Hi Chet,



On 29 September 2013 17:46, Chet Murthy <murthy.chet@gmail.com> wrote:
>
>> OK, but this means that I can't think of OCaml code running in
>> different threads in a simple way - I have to consider the single
>> runtime lock at least (if I care about performance). So maybe the
>> question is: how should I understand the functioning of the single
>> runtime lock? For example, is starvation the only thing I need to
>> think about?
>
> Tom,
>
> Yaron's note made a careful distinction between two things:
> "concurrency" and "performance".  Let me explain a little more what
> he's getting at.

I sort-of understand what people mean by concurrency and
*parallelism*. Parallelism is e.g. trying to use multiple CPUs.
Concurrency is using constructs like threads etc. I think these words
aren't the best choice if trying to make this distinction, but that is
another issue.

>
> "concurrency" means writing programs using constructs that look like
> concurrent execution, for the purpose of .... well, for whatever
> purpose you'd like, but usually modularity.
>
>   --> in short, you don't care if your code's running on a single
>       processor -- you just want to -write- it with multiple threads

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.

>
>   [of course, here you care about -starvation-, hence the worry about
>   a tight loop with no possibility of suspension/"releasing the global
>   lock"; but let's set that aside, since it's a different concern
>   (even the LISPm, from what I remember, had issues here -- heck, so
>   do some JVMs, for GC)]

This is my concern! I want to understand what the issues are with
running code which uses multiple threads (from OCaml's Thread lib), on
a single core, given that there is a global lock.

>
> "parallelism" means exploiting hardware facilities to -run- code in
> parallel, usually for purposes of performance.
>
>   --> you care very much if your code "properly" exploits
>       SMP/multicore parallelism
>
> When you speak of "performance", I'm going to guess that you're really
> speaking of "parallelism" and not "concurrency".

No! I really am talking about concurrency. I am a firm believer that
message-passing is a very good way to utilize multiple cores. What I
want is to implement a message passing library, using multi-threading,
on a single core. I want to understand the issues that the single
runtime lock can cause when trying to do this. My naive idea is that
different threads execute as they would, say, using POSIX threads in
C. But the presence of the single runtime lock means that threads do
not behave like this (the code I started this thread off with
illustrates this). So how do they behave?

> If so, Ocaml's
> threads are not for you, nor are monadic programming constructs.  Not
> merely effectively, but -by- -construction- any threading system with
> a global lock can only use a single CPU.  You can get more than that
> from Ocaml, if Ocaml threads, spend a lot of time in C/C++ code that's
> CPU-intensive.  But I'm assuming that that's not what you're looking
> to do.

I didn't quite get that last sentence, but yes, I understand what you
are saying. I really do want to understand how to think about my code,
which uses OCaml Thread threads, on a single core, given that there is
a global lock. Now, it may be that to understand this there is no more
abstract approach than looking at the OCaml implementation. That is
fine. But it may be that people will say "the only problem you can
have is starvation, and you can avoid that by ensuring that each of
your threads calls Thread.yield() sufficiently often". That would be a
nice answer to have!

Thanks

T

  reply	other threads:[~2013-09-29 17:18 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 [this message]
2013-09-29 17:47                 ` Chet Murthy
2013-09-30  8:24                   ` Romain Bardou
2013-10-07 14:57                     ` Goswin von Brederlow
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=CABooLwPrO6Zrk_pQgPEM5snkg1YALwUQn+C5wHfUWFO_C0f_Xg@mail.gmail.com \
    --to=tom.j.ridge+caml@googlemail.com \
    --cc=caml-list@inria.fr \
    --cc=murthy.chet@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;
as well as URLs for NNTP newsgroup(s).