samedi 6 février 2021

C++ template for Runnable function

I am writing a library for thread management using C++ for my app and as part of the same I am trying to write a template class that takes a functionPointer to be executed inside the run function. I am a Java developer and trying to visualize as follows:

class MyRunnable : public Runnable {

    public:
        MyRunnable(fp)
        {
            mFp = fp;
        }

    private:

    FunctionPointer mFp;

    // Will be called by the thread pool using a thread
    void run() 
    {
         mFp();
    }

}

class ThreadManager {

    public:
        void execute(MyRunnable runnable) {
            executeOnAThreadPool(runnable);
        }

}

Since I am not fluent with C++ syntax, I am finding hard to get the constructor defined to take a functionPointer as argument with variable number of arguments for the functionPointer. Something like:

MuRunnable(Fp fp, Args... args)

Can someone please help me defining the constructor for MyRunnable class above. Thanks.

Aucun commentaire:

Enregistrer un commentaire