[chuck-users] can there be many ::ms in while?

Kassen signal.automatique at gmail.com
Fri Aug 3 06:06:57 EDT 2007


On 8/3/07, Martin Ahnelöv <operagasten at gmail.com> wrote:
>
> > loops, but wanted them to run "side-by-side", but looping at different
> > speeds, how would you do it?
> >




Yes, you can do that. FYI, it's called threading in traditional
> programming languages and is often a pain in the ass, but since chuck
> isn't a traditional language it's called sporking and is as simple as
> running a chuck file.


That's right!



>
> You can also do sporking in the code, but I haven't looked at that
> enough - it's in the help pages, though.


That's also quite simple. You can run threads (we call them shreds) next to
the main one. You define those as functions and you "spork" the function.
=======================================
//let's define a function
fun void paralel()
{
while(true)
    {
    //this will loop forever
    <<<"beep">>>;
    second => now;
    }
}


//let's try sporking it
spork ~ paralel();

//now we write the main loop that will run next to it
while(true)
{
//this will also loop forever
<<<"boop">>>;
.75::second => now;
}


//anything that you would write here will never get executed because the
program will always be stuck in that last loop. you could still define
functions here though.

-----------------------------

See? not at all hard. You can run as many functions next to eachother as you
like, they can all be different or they could all be the same. If they are
all the same you only need to define it once and can spork it as many times
as you'd like (untill the cpu gives up! Notice that they can all run at
different speeds if you'd like, in my example above one loops in .75 seconds
while the other takes a whole second so they will drift in and out of phase.

Cheers,
Kas.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20070803/706b6e13/attachment.htm 


More information about the chuck-users mailing list