[chuck-users] Question on Shred Scheduling

Spencer Salazar ssalazar at princeton.edu
Mon Jun 12 19:12:26 EDT 2006


Howdy,

On Jun 12, 2006, at 2:44 PM, Mike McGonagle wrote:
> So, what I would like to know is how the following code would execute.
>
> now + 0::ms => now;
>
> Would this push the execution of the code to follow to the end of the
> current time slice? Or is this functionality undefined, and would it
> execute these shreds in an arbitrary fashion?

Im not sure what that would do, but check out me.yield(), I think  
that serves exactly such a purpose.

> (this is embedded in an object)
> false => int locked;
> false => int keepGoing;
> MController @ controllers[];  // this gets initialized to match the
> number of controlled parameters
>
> fun void helper(....) {
>    true => keepGoing;
>    while(keepGoing) {
>       while(locked) {
>          now + 0::ms => now;
>       }
>       true => locked;
>       for(0 => int i; i < controllers.cap(); i++) {
>          if (controllers[i] != null) {
>             <<< "do something with this controller", "" >>>;
>          }
>       }
>       false => locked;
>    }
> }

Hmm, i see a few potential issues here.  helper() never allows for  
the passage of time, which might be a problem, unless keepGoing is  
set to false relatively quickly.  If time doesnt advance within the  
helper shred, time cant advance for other shreds.

Secondly, I dont think your locking mechanism is guaranteed to work  
all of the time.  In this code:

>       while(locked) {
>          now + 0::ms => now;
>       }
>       // point A
>       true => locked;

what if another shred is scheduled to execute at point A?   Say this  
shred is also waiting on locked; when it is scheduled to execute,  
locked is false, so it exits from the while loop.  Both shreds would  
then proceed as if they had acquired the lock, which is probably  
undesirable.

Its not really possible to write a reliable mutex using standard  
control structures and variables.  It might be possible using Events,  
but I think they are really something that should be provided by the  
ChucK language (feature request!).  Until that happens, I would  
recommend trying to use Events somehow...

spencer



More information about the chuck-users mailing list