ParallelFor: TestBreak()



  • Hello,

    I'm successfully using the Parallel stuff from the API.

    What I didn't understand yet is how to check for a user break within a ParallelFor worker lambda. I'm thinking the BreakContext has something to do with it, but I couldn't figure out yet how to do it.

    With the old C4DThreads, we could simply do it like this:

    // Check for user break every 64 iterations
    if (thread && !(x & 63))
    {
    	if (thread->TestBreak())
    		break;
    }
    

    How does this work with the new Parallel functions?

    Cheers,
    Frank



  • hi,
    sorry for the late reply.

    you have to use the BreakContext in the template of the parrallelFor.

    And pass your thread to the lamba function as a reference. In the lambda function you call the Break function if needed.

    there are other context you can use

            maxon::Result<void> errReturn;
    	auto worker = [&escThread]
    	(maxon::Int32 y , maxon::ParallelFor::BreakContext& context) -> maxon::Result < void >
    	{
    		
    		for (maxon::Int32 loop = 0; loop < 10000; loop++)
    		{
    			if (MAXON_UNLIKELY(escThread->TestBreak()))
    				context.Break(maxon::UnexpectedError(MAXON_SOURCE_LOCATION));
    			continue;
    		}
    		return maxon::OK;
    	};
    
    	errReturn = maxon::ParallelFor::Dynamic<maxon::ParallelFor::BreakContext>(0, 100000, worker, numThreads, maxon::PARALLELFOR_DEFAULTGRANULARITY );
    	
    	if (errReturn == maxon::OK)
    		ApplicationOutput("we didn't got stoped");
    	else if (errReturn == maxon::FAILED)
    		ApplicationOutput("we've been stopped @", errReturn.GetError());
    
    

    Sorry i cant really provide a fully working example but that's the way to do it :)

    Cheers,
    Manuel



  • Super cool, thank you for the example code!

    Greetings,
    Frank



  • Oh wait, what's escThread in your example code? Where did that come from?



  • I would guess that's the thread returned by GeGetEscTestThread(). See Thread Utility Manual.

    Or it is just the thread that provides the context for your call.



  • That makes sense. Thanks again!

    Cheers,
    Frank



  • hi,

    as @PluginStudent said i was using GeGetEscTestThread and by the way, it's not really working =)

    But that's the same question with your code.
    You test the thread that have been provided to you or your own thread. For that you pass that thread as a reference to the lambda function.

    Cheers,
    Manuel