I am new to c++, and trying to implement multithreading in my code. However, I get this weird error during compilation:
photongen.cc:(.text+0x90a): undefined reference to `void multiprocess(int, std::vector<Photon, std::allocator >, void ()(Photon))'
I've declared multithreading function in my header file and defined it in another cpp file.
My code snippets: Photongen:
void photongen() {
// Iterating over Muon list
for (auto m = muonList.begin(); m != muonList.end(); ++m) {
nP = 0; // Initialise the number of photons to 0
vector<Photon> tempP; // Temporary vector to store generated photons
// Muon propagation
bool stopLoop = false;
while (!stopLoop) {
// Photon generation
if (nPhotons()) {
for (int i = 0; i < nPhotons(); i++){
// Create photon instance
Photon *P = new Photon(m->x, m->y, m->z, m->run, m->event, i);
tempP.push_back(*P);
delete P; // Free memory by photon object
}
}
// Check if muon has left the detector and stop loop...
...
}
multiprocess<Photon>(1, tempP, &propagation);
tempP.clear();
}}
propagation
void propagation(Photon *P) {
if (photonProp(P)) {
pLock.lock();
photonList.push_back(*P);
nP++;
pLock.unlock();
}}
multiprocess
template <typename T>
void multiprocess(int nThreads, vector<T> arg, void (*func)(T*)) {
int i;
for (i=0; i < arg.size() - nThreads; i += nThreads) {
thread t[nThreads];
for (int j = 0; j < nThreads; j++){
// cout << i+j << endl;
t[j] = thread(func, arg[i+j]);
}
for (int j = 0; j < nThreads; j++){
t[j].join();
}
}
thread t[arg.size()%nThreads];
for (int j = 0; j < arg.size()%nThreads; j++){
t[j] = thread(func, arg[i + j]);
}
for (int j = 0; j < arg.size()%nThreads; j++){
t[j].join();
}
}