Threads vs. continuations

Discussion in 'Python' started by miller.paul.w@gmail.com, Feb 19, 2008.

  1. Guest

    I've been doing some thinking, and I've halfway convinced myself of
    the following statement: that threads as implemented by Python (or
    Java) are exactly equivalent to one-shot continuations in Scheme. Am
    I right? (I'd have asked in the scheme groups, but I feel like I'm
    less likely to get flamed to death here... hehe.)

    If that's the case, it seems threads plus hygeinic macros and a few
    primitives a la Scheme would form a solid basis upon which to build a
    programming language. The only thing preventing Python from being
    that language is the difficulty of integrating a macro system, n'est-
    ce pas?

    Thanks!

    Paul
    , Feb 19, 2008
    #1
    1. Advertising

  2. Tim Daneliuk Guest

    wrote:
    > I've been doing some thinking, and I've halfway convinced myself of
    > the following statement: that threads as implemented by Python (or
    > Java) are exactly equivalent to one-shot continuations in Scheme. Am
    > I right? (I'd have asked in the scheme groups, but I feel like I'm
    > less likely to get flamed to death here... hehe.)
    >
    > If that's the case, it seems threads plus hygeinic macros and a few
    > primitives a la Scheme would form a solid basis upon which to build a
    > programming language. The only thing preventing Python from being
    > that language is the difficulty of integrating a macro system, n'est-
    > ce pas?
    >
    > Thanks!
    >
    > Paul


    An interesting question to which I shall stay tuned for an answer.

    However, allow me to point out that there is a macro language for
    Python. It's called 'm4' ... <ducks and runs>

    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
    Tim Daneliuk, Feb 19, 2008
    #2
    1. Advertising

  3. Aahz Guest

    In article <>,
    <> wrote:
    >
    >If that's the case, it seems threads plus hygeinic macros and a few
    >primitives a la Scheme would form a solid basis upon which to build a
    >programming language. The only thing preventing Python from being
    >that language is the difficulty of integrating a macro system, n'est-
    >ce pas?


    "Difficulty" as measured in Guido being less likely than the survival of
    a snowball in the Sahara to ever consider anything like hygenic macros.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "All problems in computer science can be solved by another level of
    indirection." --Butler Lampson
    Aahz, Feb 19, 2008
    #3
  4. On Feb 19, 8:26 pm, wrote:
    [...]
    > The only thing preventing Python from being
    > that language is the difficulty of integrating a macro system, n'est-
    > ce pas?


    Well there's logix (http://www.livelogix.net/logix/)


    --
    Arnaud
    Arnaud Delobelle, Feb 19, 2008
    #4
  5. > I've been doing some thinking, and I've halfway convinced myself of
    > the following statement: that threads as implemented by Python (or
    > Java) are exactly equivalent to one-shot continuations in Scheme. Am
    > I right?


    No. In case of threads, progress can be made in an overlapping
    (concurrent), in case of Java even parallel fashion. In particular,
    if a thread blocks in a blocking operating system call (e.g. a network
    receive operation), other threads can continue. I believe this is not
    possible with continuations in Scheme.

    In more detail, threads as provided by the operating system underly
    a system scheduler: they can be preempted, they have priorities,
    and they may voluntarily block. All this is not possible with
    continuations.

    IOW, threads are more expressive than continuations.

    Regards,
    Martin
    Martin v. Löwis, Feb 19, 2008
    #5
  6. Tim Daneliuk Guest

    Martin v. Löwis wrote:
    >> I've been doing some thinking, and I've halfway convinced myself of
    >> the following statement: that threads as implemented by Python (or
    >> Java) are exactly equivalent to one-shot continuations in Scheme. Am
    >> I right?

    >
    > No. In case of threads, progress can be made in an overlapping
    > (concurrent), in case of Java even parallel fashion. In particular,
    > if a thread blocks in a blocking operating system call (e.g. a network
    > receive operation), other threads can continue. I believe this is not
    > possible with continuations in Scheme.
    >
    > In more detail, threads as provided by the operating system underly
    > a system scheduler: they can be preempted, they have priorities,
    > and they may voluntarily block. All this is not possible with
    > continuations.
    >
    > IOW, threads are more expressive than continuations.
    >
    > Regards,
    > Martin


    That's assuming that the threading implemented at the language
    level is actually realized by underlying kernel threading.
    Is/Was it not the case, though, that some languages present
    a threading model to the programmer that is realized in user
    space, but not in the kernel. ISTR some early implementations
    of Posix Threads that worked that way. The API was there
    and was correct, but - since everything was actually running
    in user space - when a single "thread" blocked, they all did.

    'Not trying to start a fight here, I'm just curious about the
    current state of that art. It is the case today that all
    modern language threading is realized over a kernel implementation
    of threading that behaves as you suggest?



    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
    Tim Daneliuk, Feb 19, 2008
    #6
  7. Paul Rubin Guest

    Tim Daneliuk <> writes:
    > 'Not trying to start a fight here, I'm just curious about the
    > current state of that art. It is the case today that all
    > modern language threading is realized over a kernel implementation
    > of threading that behaves as you suggest?


    Certainly not. See Erlang, Haskell, Concurrent ML, etc.
    The low level i/o in the runtime systems for those languages has
    to be written to never block, but the payback is much lighter weight
    user threads.
    Paul Rubin, Feb 19, 2008
    #7
  8. > That's assuming that the threading implemented at the language
    > level is actually realized by underlying kernel threading.
    > Is/Was it not the case, though, that some languages present
    > a threading model to the programmer that is realized in user
    > space, but not in the kernel.


    You were asking about Python and Java specifically. Python
    has been using OS threads as its threading foundation ever
    since operating systems started supporting threads (there is
    still support for user-level thread libraries in Python,
    but I doubt it's still in use anywhere). Java had different
    versions of the JVM in the past, one that supported only
    user-mode threads, but likewise, these got out of use quite
    some time ago.

    > ISTR some early implementations
    > of Posix Threads that worked that way. The API was there
    > and was correct, but - since everything was actually running
    > in user space - when a single "thread" blocked, they all did.


    No, not in any good user-mode thread library. E.g. the GNU Pth
    library manages to provide user-mode dispatching and provides
    wrappers for blocking calls that dispatch to a different thread
    if blocking would occur.

    > 'Not trying to start a fight here, I'm just curious about the
    > current state of that art. It is the case today that all
    > modern language threading is realized over a kernel implementation
    > of threading that behaves as you suggest?


    I didn't suggest it for all languages, only for Python (or Java),
    see your original posting.

    Regards,
    Martin
    Martin v. Löwis, Feb 19, 2008
    #8
  9. John Nagle Guest

    Tim Daneliuk wrote:
    > Martin v. Löwis wrote:


    > Is/Was it not the case, though, that some languages present
    > a threading model to the programmer that is realized in user
    > space, but not in the kernel. ISTR some early implementations
    > of Posix Threads that worked that way. The API was there
    > and was correct, but - since everything was actually running
    > in user space - when a single "thread" blocked, they all did.


    People did things like that to hammer threading onto operating
    systems so dumb they couldn't context switch, like
    DOS, early Windows, and MacOS through 7. Nobody does that
    any more.

    For one thing, it's easier to build a real scheduler
    than to build the hacks for working without one. (Mac programmers
    referred to this as the Mess Inside - no real CPU dispatcher, but
    "deferred tasks", "timer tasks", "vertical interval tasks", and
    similar hacks to work around the lack of one.)

    John Nagle
    John Nagle, Feb 20, 2008
    #9
  10. Paul Rubin Guest

    John Nagle <> writes:
    > People did things like that to hammer threading onto operating
    > systems so dumb they couldn't context switch, like
    > DOS, early Windows, and MacOS through 7. Nobody does that
    > any more.


    I see stuff heading more the other way; here's a description of a test
    of Erlang with 20 million (userspace) threads:

    http://groups.google.com/group/comp.lang.functional/msg/33b7a62afb727a4f

    I don't know of any OS's that can handle that many threads.
    Lightweight userspace threads also makes it sane to do things like
    make GUI's with a separate thread per widget, and in general to
    handle large numbers of concurrent tasks without the large memory
    footprint and context switch overhead of kernel level threads.
    Paul Rubin, Feb 20, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Wallace

    Are line continuations needed?

    Russell Wallace, Apr 7, 2004, in forum: Python
    Replies:
    13
    Views:
    480
    Peter Maas
    Apr 8, 2004
  2. Batista, Facundo

    RE: Are line continuations needed?

    Batista, Facundo, Apr 7, 2004, in forum: Python
    Replies:
    9
    Views:
    322
    Duncan Booth
    Apr 9, 2004
  3. Mike Thompson
    Replies:
    17
    Views:
    736
  4. Robert
    Replies:
    0
    Views:
    505
    Robert
    Feb 12, 2006
  5. Florian Lindner

    RFC 822 continuations

    Florian Lindner, May 2, 2006, in forum: Python
    Replies:
    2
    Views:
    280
Loading...

Share This Page