A solution (not tested yet):
I have one, I will make a constructor which will take the arguments and assign them to data members. Run() will acess them and I think this will work.
~~~~~~~~~~~~~~
I have to use p_thread with C++ (rules are rules).
I had implemented a job scheduler doing this and every job was actually a ComputePi method, which received no arguments. Now, I need to pass arguments to it. I want to process a vector with a method. Every method will proccess a range of the vector.
The job scheduler actually handles Tasks.
Code:
class Task {
public:
Task(Job* job_, JobListener* listener_,
std::vector< std::pair<int, JobStatus> >& p, int i_)
: job(job_), listener(listener_), profile(p), i(i_), exit_code((void*)-1) {
int not_ok = pthread_create( &thread, NULL, &Job::RunHelper, job);
if(not_ok) { status() = ERROR; std::cout << "Error at creation\n"; }
else status() = RUNNING;
}
...
};
p.
and then the job class is this
Code:
class Job {
public:
Job() { }
virtual ~Job() { }
// This method should be implemented by subclasses.
virtual void* Run(void*) = 0;
static void *RunHelper(void *classRef)
{
struct arg_struct args;
args.arg1 = 5;
args.arg2 = 7;
return ((Job *)classRef)->Run((void*)&args); // I would call this as ..->Run(NULL); when I didn't need arguments
}
....
};
and then in the class of ComputePi, I have
Code:
class ComputePi : public Job {
public:
ComputePi(int steps) : steps_(steps) { }
void* Run(void* arguments) {
if(arguments == NULL) std::cout << "u" << std::endl;
..
}
However, 'u' is printed all the time and when I try to acess 'arguments', it crashes, since 'arguments' is NULL.
Any idea how to pass the arguments?