dimanche 28 octobre 2018

Defining a Job Manager with a queue of "Task" function pointers

I am working on making a Job Manager that will have a thread pool of worker threads, and each of those worker threads will pull from the queue and execute the given jobs if there are any.

What I am struggling with is actually calling the function pointer that I have stored in a concurrent queue.

I have defined a CpuJob struct as follows:

class ITask {};  // An interface that should be inherited 
                 // from if you want your class to be "Jobified" 

typedef void ( ITask::*task_func )( void*, int ); // A job function 
                                                  // MUST take a void* and an int 

struct CpuJob
{
    task_func task_func_ptr;

    void* args = nullptr;

    int index = 0;
};

And here is my AddJob Function:

 void Jobs::JobManager::AddJob_Member( task_func aJob, void * aArgs, int aIndex )
 {
     CpuJob temp = {};
     temp.task_func_ptr = aJob;
     temp.args = aArgs;
     temp.index = aIndex;

     readyQueue.emplace_back( temp );    // This is a wrapper around std::queue to be concurrent and thread safe

     jobAvailableCondition.notify_one();

 }

And inside my worker thread, where I want to call the stored function pointer, is where I get my error:

 // Other stuff popping jobs off the queue
 if ( !readyQueue.empty() )
 {
     CpuJob CurJob;
     readyQueue.pop_front( CurJob );

     CurJob.task_func_ptr( CurJob.args, CurJob.index );  // This is giving me an error 

        // Notify other threads that a job has been taken and we should probably
        // check to make sure that there isn;t more
        jobAvailableCondition.notify_one();
    }

The error that I am getting is as follows:

Error (active) expression preceding parentheses of apparent call must have (pointer-to-) function type

I think that it has something to do with how I am referencing the function, but I have tried several ways of doing to no avail.

Aucun commentaire:

Enregistrer un commentaire