layout: post
title: Threads
-
7 November 2011
A thread object is a kind of function references; makethread turns a function reference into a thread function reference;
What if the function reference has captures / non local references ? One can either
When the function is called via this reference, then a new interpreter thread is created, the new thread has its own stack. Function parameters are pushed onto the new stack. The new thread stack has a reference to the function object; so that both threads (invoking and created thread) hold a reference to the function object.
When the thread calls the yield function for the first time (or returns a value) , then the invocation of the function reference from calling thread returns the yield/return value.
The calling thread can later call resume on the function object; this will pass controll back to the thread function; resume can receive an argument, a message from calling thread to new thread; this value will be returned by the yield function, which has been called previously by the new thread.
A thread terminates, when it calls return ; The calling thread can check if the thread is running by calling
The function object that represents the thread will need to have a reference to the thread (in order to pass values around and resume and stuff)
the thread has a reference to the parent thread + calling function object (in order to pass values around).
Controll passes back to thre thread if
When a thread Yields a value, or exits, then control is passed back to the invoking thread.
we don’t have that here.