Thread: pass arguments to p_thread

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    pass arguments to p_thread

    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?
    Last edited by std10093; 03-22-2014 at 09:58 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass arguments though lookup table
    By robi in forum C Programming
    Replies: 1
    Last Post: 10-16-2011, 01:54 AM
  2. pass on variable arguments
    By Fader_Berg in forum C Programming
    Replies: 5
    Last Post: 09-11-2009, 09:04 AM
  3. Correct way to pass arguments to this function
    By eponymous in forum C Programming
    Replies: 6
    Last Post: 03-19-2008, 06:56 AM
  4. How can I pass arguments through a GUI (win32)?
    By bikr692002 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 05:17 PM
  5. how do i pass arguments to a function^^^please help
    By adzza69 in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2004, 06:03 PM