Digital Art Zone

 
     
SMP Multi-thread code basics
Posted: 10 March 2013 12:02 AM   [ Ignore ]
New Member
Avatar
Total Posts:  25
Joined  2004-03-30

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
  {
  public:
  virtual MCCOMErr MCCOMAPI Work();
  };


MCCOMErr MyDeformer::DeformFacetMesh(real lod,FacetMesh* MeshIn,FacetMesh** MeshOut)
  {
  ...
  TMCArray<TVector3> OffsetVertices;
  TVector3 vOffset;
  uint32 uVertex,uTotalVertices;
  vOffset.x=0.0;
  vOffset.y=1.0;
  vOffset.z=2.0;
  uTotalVertices=MeshIn->VerticesNbr();
  OffsetVertices.SetElemCount(uTotalVertices);
  for(uVertex=0;uVertex<uTotalVertices;uVertex++)
  OffsetVertices[uVertex]=MeshIn->fVertices[uVertex]+vOffset;
  ...
  // MYTHREAD MyThread;
  int32 iThreadID;
  // iThreadID=gShellSMPUtilities->LaunchSMPThread(&MyThread;,0,NULL,kHighPriority,&bAbortThread;);
  // wait for thread to finish…
  ...
  // rebuild the facet offset mesh
  }

MyThread::Work()
  {

  // ! MOVE THE ABOVE CODE TO HERE !

  // signal that the work is done…
  return MC_S_OK;
  }

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.

 Signature 

www.Sparrowhawke3D.com

Profile