THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/02/2012 at 06:22, xxxxxxxx wrote:
In C++, you need two parts, a controller class and a thread class derived from Thread. Here are simplified header and source for each:
////////////////////////////////////////////////////////////////
// ControlThread.h
////////////////////////////////////////////////////////////////
#ifndef _CONTROLTHREAD_H_
#define _CONTROLTHREAD_H_
#include "c4d.h"
#include "GMPThread.h"
// CLASS: ControlThread
class ControlThread : public Thread
{
public:
// Data
// - MP Thread related
LONG cpu_count;
#ifdef C4D_R12
MPThreadPool mp;
#else
MPThread mp;
#endif
GMPThread thread[MAX_THREADS];
Thread* tlist[MAX_THREADS];
// Methods
ControlThread();
~ControlThread();
// - Thread
void Main();
const CHAR* GetThreadName() { return "ControlThread"; }
// - ControlThread
Bool Initialize();
};
#endif //_CONTROLTHREAD_H_
////////////////////////////////////////////////////////////////
// ControlThread.cpp
////////////////////////////////////////////////////////////////
#include "ControlThread.h"
// METHODS: ControlThread ======================================================================================================
// Constructor
//*---------------------------------------------------------------------------*
ControlThread::ControlThread()
//*---------------------------------------------------------------------------*
{
}
// Destructor
//*---------------------------------------------------------------------------*
ControlThread::~ControlThread()
//*---------------------------------------------------------------------------*
{
}
// Thread.Main
// - This is the Multi-Processor Control Thread
//*---------------------------------------------------------------------------*
void ControlThread::Main()
//*---------------------------------------------------------------------------*
{
#ifdef C4D_R11 // or later
if (!mp.Start(THREADPRIORITY_NORMAL)) return;
#else
if (!mp.Start()) return;
#endif
mp.Wait();
}
// ControlThread.Initialize
//*---------------------------------------------------------------------------*
Bool ControlThread::Initialize()
//*---------------------------------------------------------------------------*
{
// Get this once and for task-separation purposes
cpu_count = GeGetCPUCount();
for (LONG i = 0L; i != cpu_count; ++i)
{
tlist[i] = &thread[i];
thread[i].Initialize(this);
}
// Initialize MPThread for later use
if (!mp.Init(*this,cpu_count,tlist)) return GePrint("ControlThread.Initialize.mp.Init()");
return TRUE;
}
////////////////////////////////////////////////////////////////
// GMPThread.h
////////////////////////////////////////////////////////////////
#ifndef _GMPTHREAD_H_
#define _GMPTHREAD_H_
#include "c4d.h"
#include "GMPSupport.h"
// Predefines for GMPThread
class ControlThread;
// CLASS: GMPThread
class GMPThread : public Thread
{
private:
// Data
ControlThread* cthread;
public:
// Data
// Methods
GMPThread();
~GMPThread();
// - Thread
void Main();
const CHAR* GetThreadName();
// - GMPThread
Bool Initialize(ControlThread* ct);
};
#endif //_GMPTHREAD_H_
////////////////////////////////////////////////////////////////
// GMPThread.cpp
////////////////////////////////////////////////////////////////
#include "GMPThread.h"
#include "ControlThread.h"
// METHODS: GMPThread ======================================================================================================
// Constructor
//*---------------------------------------------------------------------------*
GMPThread::GMPThread()
//*---------------------------------------------------------------------------*
{
}
// Destructor
//*---------------------------------------------------------------------------*
GMPThread::~GMPThread()
//*---------------------------------------------------------------------------*
{
}
// Thread.GetThreadName
//*---------------------------------------------------------------------------*
const CHAR* GMPThread::GetThreadName()
//*---------------------------------------------------------------------------*
{
return "GMPThread";
}
// GMPThread.Initialize
//*---------------------------------------------------------------------------*
Bool GMPThread::Initialize(ControlThread* ct)
//*---------------------------------------------------------------------------*
{
// Store ControlThread
cthread = ct;
return TRUE;
}
// Thread.Main
// - This is the Single-Processor Thread
//*---------------------------------------------------------------------------*
void GMPThread::Main()
//*---------------------------------------------------------------------------*
{
// Do your thing! :)
}
Initialize the threads through the controller class - only needs to be done once
// - Main Multi-Processor Thread
if (!cthread.Initialize()) return ErrorException::Throw(GeLoadString(GREERR_GENERAL), "GreeblerObj.Init.cthread");
Start the thread or threads running:
// - Begin Multiprocessing
cthread.Start(THREADMODE_SYNCHRONOUS);
A thread ends its active processing when it exits its Main() method.