Classes | Core > Kernel

Routine : Thread : Stream : AbstractFunction : Object

Functions that can return in the middle and then resume where they left off


A Routine runs a Function and allows it to be suspended in the middle and be resumed again where it left off. This functionality is supported by the Routine's superclass Thread. Effectively, Routines can be used to implement co-routines as found in Scheme and some other languages.

A Routine is started the first time -next is called, which will run the Function from the beginning. It is suspended when it "yields" (using Object: -yield within the Function), and then resumed using -next again. When the Function returns, the Routine is considered stopped, and calling -next will have no effect - unless the Routine is reset using -reset, which will rewind the Function to the beginning. You can stop a Routine before its Function returns using -stop.

When a Routine is scheduled on a Clock (e.g. using -play), it will be started or resumed at the scheduled time. The value yielded by the Routine will be used as the time difference for rescheduling the Routine. (See -awake).

Since Routine inherits from Thread, it has its own associated logical time, etc. When a Routine is started or resumed, it becomes the current thread.

Routine also inherits from Stream, and thus shares its ability to be combined using math operations and "filtered".

Class Methods, stackSize: 512)

From superclass: Thread

Creates an instance of Routine, passing it the Function with code to run.



A Function with code for the Thread to run.


Call stack size (an Integer).


Inherited class methods

Undocumented class methods, stackSize, clock, quant)

Instance Methods


This method performs differently according to the Routine's state:

Since Routine inherits from Thread, it will become the current thread when it is started or resumed; i.e. thisThread used in the Routine Function will return the Routine. It will inherit the parent thread's logical time and clock (see Thread: -parent).

Synonyms for next are -value and -resume.


  • Either the value that the Routine yields (the Object on which yield is called within the Routine Function),
  • ...or nil, if the Routine has stopped.


When a Routine is started by a call to this method (or one of its synonyms), the method's argument is passed on as the argument to the Routine Function:

After the Routine has yielded (it has been suspended at the point in its Function where yield is called on an Object), a call to this method (or its synonyms) resumes executing the Function and the argument to this method becomes the return value of yield. To access that value within the Function, you have to assign it to a variable - typically, the argument of the Function is reused:

Typically, a Routine yields multiple times, and each time the result of the yield is reassigning to the argument of its Function.


Equivalent to -next.


Equivalent to -next.


Equivalent to the Routine Function reaching its end or returning: after this, the Routine will never run again (the -next method has no effect and returns nil), unless -reset is called.


Causes the Routine to start from the beginning next time -next is called.


If a Routine is stopped (its Function has returned or -stop has been called), it will never run again (the -next method has no effect and returns nil), unless this method is called.

A Routine cannot reset itself, except by calling Object: -yieldAndReset.

See also: Object: -yield, Object: -alwaysYield

.play(clock, quant)

From superclass: Stream

In the SuperCollider application, a Routine can be played using a Clock, as can any Stream. every time the Routine yields, it should do so with a float, the clock will interpret that, usually pausing for that many seconds, and then resume the routine, passing it the clock's current time.



a Clock, TempoClock by default


see the Quant helpfile


using Object:idle within a routine, return values until this time is over. Time is measured relative to the thread's clock.

.awake(inBeats, inSeconds, inClock)

This method is called by a Clock on which the Routine was scheduled when its scheduling time is up. It calls -next, passing on the scheduling time in beats as an argument. The value returned by next (the value yielded by the Routine) will in turn be returned by this method, thus determining the time which the Routine will be rescheduled for.



The scheduling time in beats. This is equal to the current logical time (Thread: -beats).


The scheduling time in seconds. This is equal to the current logical time (Thread: -seconds).


The clock which awoke the Routine.

Accessible instance variables

Routine inherits from Thread, which allows access to some of its state:


.beats = inBeats

From superclass: Thread


The elapsed beats (logical time) of the routine. The beats do not proceed when the routine is not playing.


.seconds = inSeconds

From superclass: Thread


The elapsed seconds (logical time) of the routine. The seconds do not proceed when the routine is not playing, it is the converted beat value.


.clock = inClock

From superclass: Thread


The thread's clock. If it has not played, it is the SystemClock.

Inherited instance methods

Undocumented instance methods