Reputation: 13
I have a small C++ thread-based application that compiles and works correctly on my Windows machine with G++ 7.3.0. However, the very same .cpp file does not compile on another Linux machine, also using G++ 7.3.0.
I believe the reason must be in compiler options that I am not aware of. Particularly, I already tried specifying the -std version but it doesn't change anything.
As I believe this is not code-dependent, I'm not posting the code, but I will if it is helpful for someone
This is the error message I get on the second machine:
In file included from ./parallel_prefix_sum.cpp:4:0:
./parallel_executor.cpp: In instantiation of ‘void parallel_executor<T>::test_overhead() [with T = int]’:
./parallel_prefix_sum.cpp:24:19: required from here
./parallel_executor.cpp:152:29: error: invalid use of non-static member function ‘void parallel_executor<T>::upsweep(std::vector<T>&, int, int, int) [with T = int]’
threads.push_back(std::thread(upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], i%4));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./parallel_executor.cpp:79:8: note: declared here
void upsweep(std::vector<T>& v, int begin, int stop, int d) {
^~~~~~~
Edit2: Here is a MRE
parallel_prefix_sum.cpp
#include <iostream>
#include <chrono>
#include <vector>
#include "parallel_executor.cpp"
using namespace std;
int sum (int a, int b){
return a+b;
}
int main(){
int size = 100;
vector<int> my_input;
for (int i = 0; i < size; i++) {
my_input.push_back(i);
}
parallel_executor<int>* s = new parallel_executor<int>(my_input, sum, 1);
s->test_overhead();
return 0;
}
parallel_executor.cpp
#include <functional>
#include <vector>
#include <math.h>
#include <thread>
#include <chrono>
using namespace std;
template<class T> class parallel_executor{
//public:
vector<T> input_vector;
function<T(T,T)> input_function;
int workers;
int size;
public:
parallel_executor(vector<T> v, function<T(T,T)> f, int w){
for (int i = 0; i < v.size(); ++i){
input_vector.push_back(v[i]);
}
input_function = f;
workers = w;
size = input_vector.size();
}
void upsweep(std::vector<T>& v, int begin, int stop, int d) {
for (int i = begin + pow(2, d) - 1; i < stop && i < size; i += pow(2,d+1)) {
int j = i + pow(2, d);
v[j] = input_function(v[i], v[j]);
}
}
void test_overhead(){
std::vector<std::thread> threads;
std::vector<int> thread_partition_up{0,size};
for(int i = 0; i < thread_partition_up.size(); ++i){
threads.push_back(std::thread(upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], 1));
}
for(int i = 0; i < threads.size(); ++i){
threads[i].join();
}
}
};
Upvotes: 0
Views: 145
Reputation: 385098
You need an ampersand and a scope name:
threads.push_back(std::thread(¶llel_executor<T>::upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], 1));
// ^^^^^^^^^^^^^^^^^^^^^^^
If your code is accepted by MinGW, that's an implementation bug.
Upvotes: 2