On Mon, Mar 31, 2003 at 07:45:30AM -0800, Austin Hastings wrote:
I've been thinking about closures, continuations, and coroutines, and
one of the interfering points has been threads.

What's the P6 thread model going to be?

As I see it, parrot gives us the opportunity to implement preemptive
threading at the VM level, even if it's not available via the OS.

I think we should consider cooperative threading, implemented using continuations. Yielding to another thread would automatically happen when a thread blocks, or upon explicit request by the programmer.


It has many advantages:
1. fast: low task switching overhead and no superfluous task switching
2. no synchronization problems. locking not needed in most common cases
3. thanks to (2), shared state by default without issues
4. most code will not need any special design to be thread-safe, even when it uses globals shared by all threads.
5. no interference with continuations etc, since they're based on it
6. less VM code since an existing mechanism is used, which also means less code over which to spread optimization efforts


And optionally if round-robin scheduling is really desired for some odd reason (it's not easy to think of a situation) then that can be easily added by using a timer of some kind that does a context switch - but you'd regain the synchronization problems you have with preemptive threading.

One problem with this threading model is that code that runs a long time without blocking or yielding will hold up other threads. Preventing "rude" code from affecting the system is one of the reasons modern OSes use preemptive scheduling. This problem is obviously much smaller in perl scripts however since all of the code is under control of the programmer. And if a CPAN module contains rude code, this would be known soon enough. (the benefits of Open Source :-)

Another problem is the inability to easily take advantage of symmetrical multiprocessing, but this basically only applies to code that does heavy computation.

I think if we apply the Huffman principle here by optimizing for the most common case, cooperative threading wins from preemptive threading.

People who really want to do SMP should just fork() and use IPC, or use the Thread::Preemptive module which *someone* will no doubt write :-)

--
Matthijs van Duin  --  May the Forth be with you!

Reply via email to