dougbinks / enkiTS

A permissively licensed C and C++ Task Scheduler for creating parallel programs. Requires C++11 support.
zlib License
1.66k stars 138 forks source link

Where is the proper position to add PreRun and PostRun? #119

Open creeperZY opened 4 months ago

creeperZY commented 4 months ago

Excuse me, sir. I want to add 2 new functions named PreRun and PostRun, where I want to do some preparation before running with multi-thread. I finished them in function: void TaskScheduler::WaitforTask( const ICompletable pCompletable, enki::TaskPriority priorityOfLowestToRun ) , and added these codes at the begin: auto nonConstICompletablePtr = constcast<ICompletable*>(pCompletable); if (nonConstICompletablePtr) nonConstICompletablePtr->PreRunExecuteRange(); added these codes at the end: if (nonConstICompletablePtr) nonConstICompletablePtr->PostRunExecuteRange(); It works well on Linux, but reports error on Windows, I don't know what caused it. Could you please give me some advice about where I should add these 2 functions properly? THANKS!

dougbinks commented 4 months ago

Altering enkiTS internals is non-trivial, and I wouldn't recommend doing so.

I also don't quite understand what you want to do. The function WaitforTask is not doing any multi-threaded work, it is simply running available tasks on it's own thread whilst waiting (rather than doing nothing).

If you want to run code before and after the ExecuteRange function then the best approach would be to either create a task dependency chain (see the example Dependencies.cpp), or add a new base task function as follows:

// note: untested code
class ITaskSetWithPrePost : public ITaskSet
{
public:
    virtual void ExectuteRangeWithPrePost( TaskSetPartition range_, uint32_t threadnum_  ) = 0; // override this not ExecuteRange

    void ExecuteRange( TaskSetPartition range_, uint32_t threadnum_  ) override final
    {
        PreFunc();
        ExectuteRangeWithPrePost( range_, threadnum_ );
        PostFunc();
    }
}

If this isn't what you are trying to achieve let me know.

You may also want to look at the ProfilerCallbacks in case what you want is to call functions when certain events occur. See the eniTSExamples for examples on how to use these.

creeperZY commented 4 months ago

您的邮件我已收到!