caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Berke Durak" <berke.durak@gmail.com>
To: "Martin Berger" <M.Berger@doc.ic.ac.uk>
Cc: caml-list@inria.fr, "Jon Harrop" <jon@ffconsultancy.com>
Subject: Re: [Caml-list] Re: Where's my non-classical shared memory concurrency technology?
Date: Mon, 19 May 2008 14:24:26 +0200	[thread overview]
Message-ID: <b903a8570805190524y64154ce0x758571764813bf79@mail.gmail.com> (raw)
In-Reply-To: <4831686F.8010903@doc.ic.ac.uk>

[-- Attachment #1: Type: text/plain, Size: 4169 bytes --]

On Mon, May 19, 2008 at 1:45 PM, Martin Berger <M.Berger@doc.ic.ac.uk>
wrote:

> Jon Harrop wrote:
>
>  Similarly, avoiding threads removes concurrency bugs...
>>>
>>
>  I don't believe you have removed any concurrency bugs. I think you just
>> pushed them around a bit.
>>
>
> I couldn't agree more. If you 'avoid' concurrency by writing your own
> 'sequential' event handling code, you have not removed the concurrency,
> you just face it in a slightly different form, and you have to
> program the event handling code yourself, rather than relying
> on a tried and tested library, i.e. you have an additional
> source of bugs, without removing the problems that are inherent
> in concurrency (e.g. deadlocks, livelocks, fairness ...). There
> are reasons why writing your own concurrency mechanisms might
> be the way to go, but it's a highly non-trivial endeavor.


Let's say that there are two kinds of concurrency bugs : consistency bugs
and
synchronization bugs.

- Consistency bugs arise when two or more threads mutate the same
datastructure concurrently,
leading to inconsistent data.

- Synchronization bugs occur when you have things like starvation (a queue
with waiter never gets filled),
unfairness (some code almost never gets a chance to run), deadlocking (ye
olde deadlock), livelocking
(threads spend time communicating back and forth without making progress),
etc.

Avoiding threads means to me that you're either using Lwt-style monadic
threads, or an event-based
framework (the two being similar).

Avoiding threads almost eliminates consistency bugs.

Code you write is, by default, atomic.  Concurrency must be explicitly
invoked, and
generally shows in the types of functions using concurrency (one of the
situations where monad
creep seems to be a good thing.)

If you take a data structure that was not intended for concurrency, then it
will almost certainly be
concurrency-safe unless it is some kind of structure that can store thunks
and invoke them, and
you store concurrency-producing thunks in them.

Hence, using, say, Lwt, I can have tens of thousands of lightweight threads
that happily mutate
an unprotected, possibly complex datastructure implemented in a
concurrency-agnostic module.
For instance, I can and do use a global reference to a Map module and mutate
it without any lock
of any kind.

Now let me classify synchronization bugs in two sorts :
  - Type A: Logical synchronization bugs due to algorithmic issues
(livelocks, etc.)
  - Type B: Synchronization bugs due to consistency bug avoidance
techniques,

Short of a formal system where concurrency properties are statically proven,
you probably can't
avoid type A bugs, since it's a high-level correctness issue.

Take for instance two mutually-recursive functions calling themselves using
an inter-process mechanism.
If they were supposed to terminate, it's a livelock, otherwise - if they are
some kind of persistent client-server
combination, they are running as usual.  Yet they will be using the same
primitives.

So no one expects logical synchronization bugs to be statically detected.

Type B bugs typically occur when you or someone else peppers code with
locks/unlock pairs (or "synchronized"
attributes, or "perform_under_lock" higher-order function...) and get a
deadlock.

While some type B bugs can be dynamically (and even statically) detected, or
some lock/unlock pairs be
removed by your JIT, others type B bugs will slip thru: even if you maintain
some kind of dependency graph
between locks, as you cannot model a synchronization effect conditioned on
another lock if the unlocking goes
thru a piece of unknown code.

If you avoid threads, type A bugs become much less likely.  Hence you won't
need to wrap almost
every shared datastructure with locks to prevent them, and hence you will
avoid a lot of type B bugs.

In short, with monadic threads, you can safely invoke non-concurrent code
from concurrent code.  (The inverse
can be dangerous - but you usually don't do this anyway since you will end
up optaining an 'a Lwt.t).
This means that locking is almost never needed and hence your code is
safer.  Sequential, yes, but safer.
-- 
Berke Durak

[-- Attachment #2: Type: text/html, Size: 5221 bytes --]

  reply	other threads:[~2008-05-19 12:24 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-18  8:39 Berke Durak
2008-05-18 16:35 ` Jon Harrop
2008-05-19 11:45   ` [Caml-list] " Martin Berger
2008-05-19 12:24     ` Berke Durak [this message]
2008-05-19 21:47       ` Jon Harrop
2008-05-19 22:24         ` Berke Durak
2008-05-19 22:37           ` Raoul Duke
2008-05-20  0:04             ` Pierre-Evariste Dagand
2008-05-20 21:27           ` David Teller
2008-05-21  7:52             ` Martin Berger
2008-05-21  8:06       ` Martin Berger
2008-05-19 14:09     ` Gerd Stolpmann
2008-05-19 16:30       ` Richard Jones
2008-05-19 18:26       ` Jon Harrop
2008-05-20  7:40       ` Ulf Wiger (TN/EAB)
2008-05-21  8:18         ` Martin Berger
2008-05-21  8:06       ` Martin Berger
2008-05-21 13:50         ` Gerd Stolpmann
2008-05-26 15:29         ` Damien Doligez
2008-05-26 16:08           ` Jon Harrop
2008-05-27  9:34           ` Martin Berger
2008-05-28 11:18             ` Damien Doligez
2008-05-28 12:16               ` Jon Harrop
2008-05-28 17:41               ` Martin Berger
2008-05-29 12:02               ` Frédéric Gava

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=b903a8570805190524y64154ce0x758571764813bf79@mail.gmail.com \
    --to=berke.durak@gmail.com \
    --cc=M.Berger@doc.ic.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=jon@ffconsultancy.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).