0

I have an object of class AI, which has a private non static void function which has a lot of arguments: minmax(double& val,double alpha, double beta, unsigned short depth, board* thisTurn); because this is a very time intensive function I want to use a number of threads to run it concurrently, therefor I must create a thread with this function inside another function inside the AI class;

According toThis question to make threads inside member functions containing non static member functions wiht no arguments of said class, one must call:

std::thread t(&myclass::myfunc,this);

And according to this tutorial threads of fucntions with multiple arguments can be created as such:

std::thread t(foo,4,5) 

where the function 'foo' has 2 integer arguments

However I desire to mix these to things, to call a function which has arguments, which is also a non static member function, from inside the class that it is a member of, and i am not sure how to mix these to things.

I have off course tried (remember that it is inside a function inside the AI class):

std::thread t(&AI::minmax,val,alpha,beta,depth,thisTurn,this);

and

std::thread t(&AI::minmax,this,val,alpha,beta,depth,thisTurn);

But both cases fails with a compile error like this:

error: no matching constructor for initialization of 'std::thread'
candidate constructor template not viable: requires single argument '__f', but
  7 arguments were provided

My question is therefor, if or if not, it is even possiple to -- from inside a member function of a class -- call a non static member function which has several arguments as a thread, and if this is the case, how it is done.

This question is however not wether or not it is a good idea to use multithreading in my specific case.

Edit

After doing some testing i realized that i can not call functions with arguments as threads at all, not even non-member functions with only one argument called directly from main, this was however solved by asking this other question where i realized that i simply need to add the flag -std=c++11 (because apparantly macs don't always use all c++11 features as standard) after doing so the answer works fine.

7
  • try std::thread t(&AI::minmax,this,std::ref(val),alpha,beta,depth,thisTurn,this); Commented Feb 14, 2017 at 8:25
  • @PiotrSkotnicki doing so gives the same error as before, and this new error no matching function for call to '__invoke' Commented Feb 14, 2017 at 9:57
  • show the code or it never happened Commented Feb 14, 2017 at 10:33
  • @PiotrSkotnicki You have one "this" too much. It should read std::thread t(&AI::minmax, this, std::ref(val), alpha, beta, depth, thisTurn);. Commented Feb 14, 2017 at 10:40
  • @pschill a copy-paste of OP's code but probably you're right Commented Feb 14, 2017 at 10:46

1 Answer 1

0

You need to wrap references using std::ref. The following example compiles fine:

#include <iostream>
#include <thread>

class board;

class AI
{
public:

    void minmax(double& val, double alpha, double beta, unsigned short depth, board* thisTurn)
    {
        std::cout << "minmax" << std::endl;
    }

    void spawnThread()
    {
        double val;
        double alpha;
        double beta;
        unsigned short depth;
        board* thisTurn;

        std::thread t(&AI::minmax, this, std::ref(val), alpha, beta, depth, thisTurn);

        t.join();
    }

};

int main()
{
    AI ai;
    ai.spawnThread();
}
Sign up to request clarification or add additional context in comments.

2 Comments

that example code still produces two error: firstly: std::ref cause compiler error: error: no matching function for call to '__invoke'followed by several lines of: ...in file included from... but in the end indicating that the problem is std::ref(val). Secondly it cause the old no matching constructor for initialization of 'std::thread' candidate constructor template not viable: requires single argument '__f', but 7 arguments were provided
@Nikolaj The code I posted compiles well in Visual Studio 2015 (see rextester.com/ALGB12143). I do not have VS2013 here to test the code, and earlier versions will not work because #include <thread> is missing there. What compiler do you use?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.