Csound Csound-dev Csound-tekno Search About

Re: [Cs-dev] csoundCreateThread() clarification

Date2007-09-10 22:20
FromMichael Gogins
SubjectRe: [Cs-dev] csoundCreateThread() clarification
Your program starts out in the main thread of its process. csoundCreateThread then creates a second thread, in which the Csound performance runs. This is so that the main thread can continue to handle GUI events and such without interfering with the Csound performance, or vice versa. csThread in your example is the 'thread routine' that is called by this second thread. The second thread calls csThread, not csoundPerformKsmps; csThread then calls csoundPerformKsmps in a loop until the performance is finished. In other words, there is NOT a new thread for each call to csoundPerformKsmps.

When you run a program on a computer, it runs in a process that has its own memory space and command console with stdin, stdout, etc. This process also has a thread that has access to the CPU registers and stack. So every process and every program must have at least one thread. When you create a new thread in a process, it gets its own copies of the CPU registers and its own stack pointer, but it keeps using the same memory space and console as the main thread. 

If your computer has only one CPU, that CPU runs each thread (there are hundreds of them, not just the two in your program but the threads from all other programs as well) in turn (very brief turns!) saving and restoring its CPU registers and stack as required. If your computer has more than one CPU, it can run more than one thread literally at the same time. All contemporary computers have at least 2 CPUs.

If you don't know what the CPU registers and stack are, you should probably read a basic introduction to computing, or look on the Wikipedia.

Hope this helps,
Mike

-----Original Message-----
>From: benjohns 
>Sent: Sep 10, 2007 1:18 PM
>To: csound-devel@lists.sourceforge.net
>Subject: [Cs-dev] csoundCreateThread() clarification
>
>
>I’ve been following Rory’s Introduction to the API document (and it has been
>extremely helpful). I have everything working swell, but I was hoping to get
>some further explanation on what is happening in this performance thread
>code (from part 4). Specifically, is a separate thread being generated for
>every input event? Or a separate thread for every control sample worth
>(ksmps)? Or is it just a single thread for the entire performace? I would
>like to get it straight in my head so I don’t look goofy explaining it to
>other people.
>
>For those without Rory’s document in easy reach, here is a rough summary of
>the code:
>
>Main() {
>csoundCreate()
>csoundCompile()
>csoundCreateThread(csThread)
>
>user input
>output updated through csoundGetChannelPtr()
>// in my code I use csoundScoreEvent() instead
>}
>
>csThread() {
>Loop: csoundPerformKsmps()
>}
>
>-- 
>View this message in context: http://www.nabble.com/csoundCreateThread%28%29-clarification-tf4416931.html#a12598178
>Sent from the Csound - Dev mailing list archive at Nabble.com.
>
>
>-------------------------------------------------------------------------
>This SF.net email is sponsored by: Microsoft
>Defy all challenges. Microsoft(R) Visual Studio 2005.
>http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>_______________________________________________
>Csound-devel mailing list
>Csound-devel@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/csound-devel




-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net
https://