Page 1 of 1

VM obsolescence -> multi threading thoughts.

Posted: Thu Jun 01, 2006 12:29 pm
by mystran
This message grew from reply to the following quote, but got long enough that it wouldn't fit into the same message with the on-topic part, and I thought I'd be offtopic enough to warrant it's own thread.
AST's comments about swapping remind me of when he argued against multithreading 15 years ago. :P
I think multithreading is highly overrated. I think the reason is that C and it's derivatives (including Java) make it so hard to do decent event-based processing; you basicly have to decompose your program into manual continuation passing style, which isn't exactly the most natural thing to write in a system without direct support for closures.

This is also why writing well behaving graphical applications is often so hard, and why writing web-based program are even harder. In languages with C-style control flow, you have to write your code to handle the events. In a language where you have direct support for closures, or co-routines or something, you could write direct code for operations, and have the system interleave it with an event-loop for you, basicly giving you co-operative multi-threading within a single process.

There is one good use for multi-threading though: you can run on several processors at the same time. Now that I think of it, there's no fundamental reason why you couldn't specify some constraints on which events are allowed to execute at the same time, and have the event-loop automatically dispatch events on several processors. Logical question would be: how does this differ from normal multi-threading?

Well, since you have to prepare for any (valid) order of events anyway, you need a consistent global state between the events. Since pure event-based processing never blocks, except when waiting for more events, you only need to figure out which events rely on the same mutable state, and ask the system to serialize those events. This will temporarily degenerate the system into uni-threading model (especially if events can't be reordered) but at least it'll never deadlock: nobody is waiting for anything, we are just serializing things that aren't safe concurrently.

I think this a pretty interesting idea. Event based "concurrency" is pretty easy to reason about (even when hard to write on C-style languages) since it's explicit, so adding true concurrency as an optimization shouldn't complicated matters much.

I think I'll have to write a small language that implement the above. I think it has some potential.

Re:VM obsolescence -> multi threading thoughts.

Posted: Sun Jun 04, 2006 5:40 pm
by Crazed123
If you want to think about event-based programming, have you ever tried Delphi/Kylix?

Re:VM obsolescence -> multi threading thoughts.

Posted: Sun Jun 04, 2006 7:46 pm
by mystran
No I have not used Delphi/Kylix, but pretty much every GUI toolkit does use some form of event-based programming, and Win32 basicly works like that too.

It's just that I want to make everything event-based, to get the maximum benefit from it.

Re:VM obsolescence -> multi threading thoughts.

Posted: Sun Jun 04, 2006 8:09 pm
by Crazed123
Well I suppose it's a pretty simple threading model, like the one used in many network-protocol X servers: A event-handling thread dispatches a pool of worker threads (possibly by creating them). The event-handling thread itself either loops while polling for events (Windows model), or blocks until an event wakes it up. Event threads run until the event is fully handled.

It sounds like, in addition to threads themselves, a useful data type for your language would be events. "Events" would be defined as a type of function-pointer which, when called, transparently forks a thread to run the actual function. The thread then returns with the function's return value, which your language could probably redirect to some destination variable, or simply into oblivion.