passing int array to std::thread

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();
}
Last edited on
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.
Last edited on
Could you post the actual error message?
$ g++ -I bamtools/include/ -L bamtools/lib/ -o tes threading-basic.cpp -lz -lbamtools -pthread -fpermissive -std=c++0x
threading-basic.cpp: In function ‘int main(int, char**)’:
threading-basic.cpp:255:108: error: no matching function for call to ‘std::thread::thread(void (&)(int*, int, int, int), int [(((sizetype)(((ssizetype)thread_divide) + -1)) + 1)], int&, int&, int&)’
std::thread thethread(process,start_holder,thread_divide,gtf_matrix[gtf_i].start,gtf_matrix[gtf_i].end);
^
threading-basic.cpp:255:108: note: candidates are:
In file included from threading-basic.cpp:6:0:
/usr/include/c++/4.8/thread:133:7: note: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8/thread:133:7: note: template argument deduction/substitution failed:
threading-basic.cpp:255:108: note: variable-sized array type ‘int (&)[(((sizetype)(((ssizetype)thread_divide) + -1)) + 1)]’ is not a valid template argument
std::thread thethread(process,start_holder,thread_divide,gtf_matrix[gtf_i].start,gtf_matrix[gtf_i].end);
^
In file included from threading-basic.cpp:6:0:
/usr/include/c++/4.8/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8/thread:128:5: note: candidate expects 1 argument, 5 provided
/usr/include/c++/4.8/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8/thread:122:5: note: candidate expects 0 arguments, 5 provided
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?
and when i convert the array to string, then pass it as std::ref I get another type mismatch

mismatched types ‘std::reference_wrapper<_Tp>’ and ‘std::string* {aka std::basic_string<char>*}’
Last edited on
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>

using namespace 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 [])
{
    const int 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?

Topic archived. No new replies allowed.