I want to learn to implement code using SMP threads but I’m having trouble understanding how to achieve this with the SDK functions - especially without some complete sample code. Like most of the SDK it’s a baffling puzzle that is only simple to understand if you already know the answer.
Any references I have found and read about multi-thread code have their API specific calls and examples are usually in an application context with a main thread. The Carrara SDK functions which I can find are different and almost seem incomplete.
I’m aware that it is a complex and more dangerous area of programming. Perhaps somebody can fill in the gaps for me or give me a kick in the right direction…
1. Creating and launching a thread is clear enough but without a ‘WaitForThread’ or ‘WaitForMultipleObjects’ how do we know when its ::Work() is done ? Must all the synchronizing be achieved through IShSemaphore and if so exactly how ?
2. How are large amounts of data in TMCArray(s) accessed and processed by a thread in ::Work() ? We can’t use globals and obviously we don’t want to copy them. Does all the large data have to use the LocalStorage ? How are TMCSMPArray and TMCSMPArrayRequest used ?
To present an example: how can I move this section of code, in the context of a Deformer call, out into a thread ?
class MYTHREAD: public TBasicSmpThread
virtual MCCOMErr MCCOMAPI Work();
MCCOMErr MyDeformer::DeformFacetMesh(real lod,FacetMesh* MeshIn,FacetMesh** MeshOut)
// MYTHREAD MyThread;
// wait for thread to finish…
// rebuild the facet offset mesh
// ! MOVE THE ABOVE CODE TO HERE !
// signal that the work is done…
If that isn’t too hard then the next example could be how to launch more than one thread for each iteration of the loop and get a few running simultaneously and wait for them to finish. Another example to clear things up might be to have 3 different threads with each to process x,y and z separately.