On 25/07/2015 at 05:16, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 15+
Platform: Windows ;
Language(s) : C++ ;
---------
I'm doing a simple image processing, which consumes time, so I put it in a thread "plugin VideoPost"
now C4dThread always crashes or not running correctly
AutoGeFree<SevenPhotonsImageThread> imageThread = NewMemClear(SevenPhotonsImageThread, 1);
imageThread->kndData = kndData;
imageThread->input = input;
imageThread->node_index = n;
imageThread->shader = shader;
imageThread->mat = mat;
imageThread->imageData = imageData;
imageThread->w = w;
imageThread->h = h;
imageThreads.push_back(imageThread);
imageThread->Start();//THREADMODE_SYNCHRONOUS
// other stuff, later
auto threadIterator = imageThreads.begin();
while(threadIterator != imageThreads.end())
{
if(*threadIterator && (*threadIterator)->IsRunning())
{
(*threadIterator)->End();
}
imageThreads.erase(threadIterator++);
}
the above code fails, and the scenario of using thread pool with a control thread is horrible "I want to launch threads as soon as I get the image, not gathering all images in 1 go then launch"
here is what worked for me "exactly the same code but using C++11 std::thread"
std::thread *sThread = new std::thread(SPImageThread, w, h, shader, mat, imageData);
stlImageThreads.push_back(sThread);
// other stuff, later
auto threadIterator = stlImageThreads.begin();
while(threadIterator != stlImageThreads.end())
{
if((*threadIterator)->joinable())
(*threadIterator)->join();
delete *threadIterator;
*threadIterator = nullptr;
stlImageThreads.erase(threadIterator++);
}
and this works like a charm, just afraid of exceptions.