concurrency, PRocesses, shreds
sporking shredsthe 'me' keyWordusing machine.add()inter-shred communication…is able to run many processes concurrently…
A ChucKian process is called a shred. To spork a shred means creating and adding a new process to the virtual(虛擬的) machine. Shreds may be sporked from a variety of places(各種各樣的地方), and may themselves spork new shreds.
(直接代換理解:spork a shred == creating & adding a process)
ChucK supports sample-synchronous(采樣同步), non-preemptive concurrency(非搶占式并發). Via the timing mechanism(機制). any number of programs/shreds can be automatically(自動的) shreduled(往后看,便于理解,實際上就是創建添加的過程,感覺就像是shred動作化了) and synchronized(同步的) use the timing information.A shreduler in the virtual(虛擬的) machine does the shreduling. The concurrency(并發性) is ‘sample-synchronous’, meaning that inter-process audio timing is guaranteed(保證) to be precise(精確的) to the sample(樣品). Note that each process/shred does not necessarily need to know about each other - it only has to deal with time locally. The virtual machine will make sure things happen correctly “across the board(全體,全面)”. Finally, concurrency - like timing - is deterministic(確定性的) in ChucK.
The simplest way to to run shreds concurrently is to specify(指定) them on the command line:
chuck foo.ck bar.ck boo.ckThe above command runs chuck on foo.ck, bar.ck, and boo.ck concurrently. There are other ways to run shreds concurrently (see on-the-fly programming commands). Next, we show how to create new shreds from within ChucK programs.
To spork means to shredule a new shred.
To spork a shred, use the spork keyword/Operator:
spork dynamically(動態地) sporks shred from a function call從函數調用中動態創建進程this operation is sample-synchronous, the new shred is shreduled to execute(執行) immediately新進程創建來立即執行the parent shred continues to execute, until time is advanced (see manipulating time) or until the parent explicitly(顯式地) yields(產生) (see next section).父進程繼續執行,直到時間被推進或者直到父進程顯示產生。in the current implementation(實現), when a parent shred exits, all child shreds all exit (this behavior(行為) will be enhanced(提高) in the future.)在當前實現中,當父進程退出,所有的子進程會退出。sporking a functions returns reference to the new shred, note that this operation does not return what functions returns - the ability to get back the return value at some later point in time will be provided in a future release.產生一個函數返回新進程的引用,注意這個操作不會返回函數返回值-返回在某個靠后的時間點上的返回值的能力就被在未來的版本中提供。// define function go()fun void go(){ // insert code}// spork a new shred to start running from go()spork ~ go();// spork another, store reference to new shred in offspringspork ~ go() => Shred @ offspring;a slightly longer example:
// define functionfun void foo( string s ){ // infinite time loop while( true ) { // print s <<< s >>>; // advance time 5::second => now; }}// spork shred, passing in "you" as argument to foospork ~ foo( "you" );// advance time by 2s2::second => now;// spork another shredspork ~ foo( "me" );// infinite time loop - to keep child shreds aroundwhile( true ) 1::second => now;//測量://輸出you后,第2s輸出me,第5s再次輸出兩個進程互有交錯,”you”先輸出,”me”后輸出,循環輸出。 個人理解:1::second => now;
感覺上就像是將進程掛起,sleep()的感覺就像是1s后再跳到這個程序。
The me
keyword (type Shred
) refers the current shred.
Sometimes it is useful to suspend(暫停) the current shred without advancing time, and let other shreds shreduled for the current time to execute(實行). me.yield()
does exactly that. This is often useful immediately after sporking a new shred, and you would like for that shred to have a chance to run but you do not want to advance time yet for yourself.
It may also be useful to exit the current shred. For example if a MIDI device(裝置) fails to open, you may exit the current shred.
// make a MidiIn objectMidiIn min;// try to open device 0 (chuck --probe to list all device)if( !min.open( 0 ) ){ // print error message <<< "can't open MIDI device" >>>; // exit the current shred me.exit();}You can get the shred id:
// print out the shred id<<< me.id(); >>>;These functions are common to all shreds, but yield()
and exit()
are commonly used with the current shred.
Machine.add( string path )
takes the path to a chuck program, and sporks it. Unlike spork ~
, there is no parent-child relationship between the shred that calls the function and the new shred that is added. This is useful for dynamically(動態地) running stored programs.
Presently, this returns the id of the new shred, not a reference to the shred. This will likely be changed in the future.
Similarly, you can remove shreds from the virtual(虛擬的) machine.
// addMachine.add( "foo.ck" ) => int id;// remove shred with idMachine.remove( id );// addMachine.add( "boo.ck" ) => id// replace shred with "bar.ck"Machine.replace( id, "bar.ck" );Shreds sporked in the same file can share the same global variables(變量). They can use time and events to synchronize to each other. (see events
) *Shreds sporked from different files can *share data (including events).**For now, this is done through a public class with static data (see classes
). Static data is not completely implemented! We will fix this very soon!
ChucK supports passing arbitrary(任意的) data from the command line into ChucK programs using optional(可選擇的) command line arguments. An argument is specified(指定) by appending(附加) a colon(冒號) character “:” to the name of the ChucK program to which you wish to send that argument, followed by the argument itself.
chuck foo.ck:fooMultiple arguments can be specified, each separated by the colon character.
chuck foo.ck:foo:bar:booFurthermore(此外), each ChucK program has its own set of arguments, which are specified separately.
chuck foo.ck:foo bar.ck:bar boo.ckCommand line arguments can also be used when using on-the-fly programming facilities(即時編程工具) of ChucK.
chuck + foo.ck:foo bar.ck:bar:boo(后面有一篇博文會講解相關參數功能的,包括這里的+
)
Machine.add() and Machine.replace() accept command line arguments in a similar fashion.
// add foo.ck// pass foo and bar as command line argumentsMachine.add( "foo.ck:foo:bar" ) => int id;// replace shred with "bar.ck"http:// pass foo and bar as command line argumentsMachine.replace( id, "bar.ck:foo:bar" );To access command line arguments within a ChucK program, use the me.args()
and me.arg()
functions.
直接運行會輸出: C:/Users/abc1/Desktop>chuck test.ck:foo [test.ck]:line(2): class ‘Shred’ has no member ‘numArgs’
(真就尷尬了)
新聞熱點
疑難解答