2

Few weeks ago i coded a Arithmetic algorithm and now I´m trying to increase the speed using threads. The problem im facing is to pass the function to the thread. This is what i have for now.

This is the class where i have the function to encode.

class ModelI
{
public:
    ModelI();
    void Process(fstream *source, fstream *target, ModeE mode,int bits);

protected:
    /*........*/
};

And here how I create the thread on the main

    vector<thread> t;
    ModelI* model = new ModelI;;
   clock_t begin = clock();
   for (int i = 0;i < 8;i++) {
            t.push_back(thread(model->Process,ref(source),ref(target),MODE_ENCODE,bits));//---> Error here

THe error I get is: "No instance of constructor std::thread::thread" matches the argument list. What am i doing wrong?

Thanks.

Capie
  • 976
  • 1
  • 8
  • 20
  • 1
    Edited with the error that i get. `model-->Process` is a mistake made when writting this question, i dont have it like this on the code. – Capie Jan 07 '17 at 18:19
  • 2
    Possible duplicate of [Start thread with member function](http://stackoverflow.com/questions/10673585/start-thread-with-member-function) – ks1322 Jan 07 '17 at 18:20
  • Thanks for updating to post the actual error message. For future reference, never *write* code in SO editors; *paste* it from a source where the actual problem exists, and include enough so *we* can take your post, dump it into a source file, compile it (or try to), and replicate the same error. Unrelated to your error, but related to your goal, be wary of sending a file stream to be used by multiple threads concurrently. The problems of concurrent positioning and operations on that stream can become a significant source of pain in one's head. – WhozCraig Jan 07 '17 at 18:30
  • I did as you mention, the problem came when trying to indent, I deleted the `-` (or i thought so) and i wrote it again. About the source file, dont worry, `source` will be a different file on each thread, so none of them will be using the same. – Capie Jan 07 '17 at 18:33

1 Answers1

1

You're doing several things wrong.

model-->Process

That's an obvious typo. But besides that, this won't work anyway. "model->Process" is not a callable object.

Your likely intent is this:

t.push_back(thread(&ModelI::Process, model, &source, &target, MODE_ENCODE, bits))

One of the ways to construct a new thread, that will work here, is by a pointer to a class method (&ModelI::Process), and the object to use to invoke the class method in the new thread (model).

And this assumes that model and source are std::fstream objects, and not pointers to std::fstream objects.

Additionally, whether those are objects or pointers to those objects, you obviously understand that they must remain in scope, and not get destroyed, until the resulting threads are joined, since those threads will accept them via their pointer arguments.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148