I'm having some difficulty passing an array of integers to a threaded function.
void process(int array_holder[], int second_parameter);
int holder[n];
int additional_parameter;
std::thread first_thread(process,holder,additional_parameter);
I've tried passing by reference with:
std::thread first_thread(process,std::ref(holder),additional_parameter);
but I still get a type mismatch when I try to compile. "variable-sized array type ‘int (&)[(((sizetype)(((ssizetype)thread_divide) + -1)) + 1)]’ is not a valid template argument". What is the proper way I should be passing an array of integers to a thread?
maybe this will recapitulate it:
#include<thread>
using namespace std;
void process(int start_holder[], int size){
for (int t = 0; t < size; t++){
cout << holder[t] << "\n";
}
}
int main (int argc, char *argv[])
{
int size = 5;
int list[size] = { 16, 2, 77, 40, 12071}
std::thread run_thread(process,list,size);
run_thread.join();
}
should i be using pthread instead. i was using it previously, and i had the data structured, with on of the variables in the struct being a vector <int>, but apparently the compiler doesn't like the vector type, so i was thinking an array through std::thread would be a work around and ultimately more straight forward. eh. my google foo is failing me. i need help.
can i not pass an array of int? i thought it was possible with string. should i convert the array to string, then convert back to int when it's in the function?
The error message you provide does not match the code you've given. Either give the error message that goes with the code you've given or give the code for the error message you've supplied.
This, the code example you gave above, modified slightly so it would compile, seems to work perfectly fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// http://ideone.com/ogC10G
#include <iostream>
#include<thread>
usingnamespace std;
void process(int start_holder [], int size) {
for (int t = 0; t < size; t++) {
cout << start_holder[t] << "\n";
}
}
int main(int argc, char *argv [])
{
constint size = 5;
int list[size] = { 16, 2, 77, 40, 12071 };
std::thread run_thread(process, list, size);
run_thread.join();
}
Does it not work for you? How does it differ from what you are actually trying to do?