2

I'm having difficulty passing an array of integers to a function using std::thread. It seems like thread doesn't like the array portion of it. What other way is there to pass an array to a threaded function?

#include <thread>
#include <ostream>
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[]){
  int size = 5;
  int holder_list[size] = { 16, 2, 77, 40, 12071};
  std::thread run_thread(process,holder_list,size); 
  //std::ref(list) doesnt work either
  //nor does converting the list to std::string then passing by std::ref
  run_thread.join();
} 
11
  • 1
    If you're going to use standard library names for your variables, don't say using namespace std;. In fact, don't say it anyway. Commented Dec 8, 2015 at 21:25
  • Is this actually a problem with std::thread, or is it int list[size] = { 16, 2, 77, 40, 12071};, which doesn't work with Clang despite the VLA extension? Commented Dec 8, 2015 at 21:27
  • ok, any idea how to fix this problem? Commented Dec 8, 2015 at 21:27
  • i think it's a problem with std::thread. should i use pthread instead and put the array in a struct? Commented Dec 8, 2015 at 21:28
  • Change size's type to const int. Commented Dec 8, 2015 at 21:29

2 Answers 2

2

Since you are using C++ start using std::vector or std::list instead of the c-style arrays. There are many other container types as well. If you want a fixed size array use std::array instead (since C++11).

These containers have functions to get the size so you do not need to send it through as a separate argument.

#include <thread>
#include <iostream>
#include <vector>

void process(std::vector<int> start_holder){
    for(int t = 0; t < start_holder.size(); t++){
       std::cout << start_holder[t] << "\n";
    }
    // Or the range based for
    for(int t: start_holder) {
       std::cout << t << "\n";
    }
}

int main (int argc, char *argv[]){
    std::vector<int> holder_list{ 16, 2, 77, 40, 12071};
    std::thread run_thread(process, holder_list); 
    run_thread.join();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Make size constant:

#include <thread>
#include <iostream>

void process(int* start_holder, int size){
  for (int t = 0; t < size; t++){
   std::cout << start_holder[t] << "\n";
  }
}

int main (int argc, char *argv[]){
  static const int size = 5;
  int holder_list[size] = { 16, 2, 77, 40, 12071};
  std::thread run_thread(process, holder_list, size); 
  run_thread.join();
}

If size is variable, int arr[size] is not a standard C++. It is a variable array extension to the language as your compiler says in the error and is not compatible with int* aka int [].

Comments

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.