0

Hello i have a little problem with my program (i want to multiply array by scalar).

Basicly i want to create a vector of threads that will do the multiplication staff (element by element)

Code samples

First mainImplementation function

void mainImplementation(){

vector<thread> threads;
vector< vector<int> > result;
vector< vector<int> > tab;
vector<int> temp;
int row = 0;
int col = 0;
int scalar = 5;

loadDataFromFile(tab,temp,row,col);

int availableThreads = thread::hardware_concurrency();


    for(int i = 0; i < row; i++){

        for(int j = 0; j < col; j++){

            for(int t = 1; i <= availableThreads; t++){

                threads.push_back(thread(scalarMultiplication,std::ref(tab),
                    std::ref(result),std::ref(temp),std::ref(i),std::ref(j),std::ref(scalar)));
            }

        }
    }

}

now function that implements scalar multiplication

void scalarMultiplication(vector< vector<int> >& vtab, vector< vector<int> >& vresult, vector<int>& vtemp, int& i, int& j, int& scalar){

//...implementation

}

I haven't implement this part yet but i cant resolve one issue

In the line

threads.push_back(thread(scalarMultiplication,std::ref(tab),
                    std::ref(result),std::ref(temp),std::ref(i),std::ref(j),std::ref(scalar)));

compiler says that there is an issue there

"Error: no instance of constructor std::thread::thread matches the argument list".

I can't seem to fix this issue. I did read that i should pass variables to the function in thread constructor by reference, so i think this is not an issue here. I pass to the multiplication function 6 variables so it should be ok but it isnt and i have no idea what to do here...google can't help me too cuz i searched for similar problem.

Thanks in advance for help.

tezaja
  • 13
  • 4
  • I get no error from GCC 4.6. Is it possible you're using an old compiler that doesn't support variadic templates? – Mike Seymour Nov 05 '13 at 12:18
  • I think `_VARIADIC_MAX` was `5` on MSVC. – Jesse Good Nov 05 '13 at 12:20
  • (As a side note, launching a thread for every single element is going to be extremely slow; and launching several for each element is almost certainly wrong. You'd be better off launching a small number of threads, each of which processes some subset of the data.) – Mike Seymour Nov 05 '13 at 12:20
  • well i use Visual Studio Ultimate 2012 (from dreamspark :D) so it should not throw errors. Well ill try to do multiplying stuff by rows or columns :) i tried #define _VARIADIC_MAX 10 and didnt help – tezaja Nov 05 '13 at 12:23
  • (You also might want to fix the inner loop condition; if you get this to compile, then you've got a fork bomb.) – Mike Seymour Nov 05 '13 at 12:27
  • You probably don't want references to `i` and `j` as parameters, as their lifetimes are constrained to the loop body. – molbdnilo Nov 05 '13 at 12:28
  • The problem says `std::thread::thread` - does this mean it can't copy construct the threads. Does moving them work? – doctorlove Nov 05 '13 at 12:33

1 Answers1

2

It was as I thought, Visual Studio Ultimate 2012 doesn't have variadic templates. The default is 5 so you need to add a #define to increase the limit (the max is 10):

#define _VARIADIC_MAX 10
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • 1
    @tezaja: hmm, did you add it *before* the header includes? – Jesse Good Nov 05 '13 at 12:29
  • well i guess i was wrong and visual had some bug that didn't show the error first, now it shows it again after using this define http://ifotos.pl/zobacz/stuff2jpg_nanhwae.jpg/ – tezaja Nov 05 '13 at 12:37
  • @tezaja: Is `void scalarMultiplication(vector< vector >&, vector< vector >&, vector&, int&, int&, int&)` declared before you use it (i.e. forward declaration)? It says `unknown type` meaning it doesn`t recognize it. – Jesse Good Nov 05 '13 at 12:41
  • hahaha i had to move this function above mainImplementation function in header file, now it works, THANKS! It is so embarassing to make error like this :) i feel ashamed – tezaja Nov 05 '13 at 12:44