I created the Mutex in the main thread and gived threads a shared pointer, but still crashing (when I set nThreads=1 it's all going fine)
Now the code looks like that:
void
Manager_farmers::landAllocation(){
// .. serial code ..
for (int i=0; i < nThreads; i++){
Manager_farmers_threads* wThread = new Manager_farmers_threads;
wThreads.push_back(wThread);
}
// parallelise the code to z workingThreads..
for (uint y=0;y<myAgents.size();y++){
bool assigned = false;
while(!assigned) {
for (uint z=0;z<wThreads.size();z++){
if (!wThreads[z]->isRunning()){
wThreads[z]->assignJob(myAgents[y], freePlot, mutex);
wThreads[z]->start();
assigned = true;
break;
}
}
}
}
// be sure that all threads are ended..
for (int z=0; z < nThreads; z++){
wThreads[z]->wait();
}
// ..continue with serial code...
for (int i=0; i < nThreads; i++){
delete wThreads[i]; // deleting the threads
}
delete mutex;
}
void
Manager_farmers_threads
::assignJob(Agent_space
* agent_h,
const Pixel
* plot_h,
QMutex* mutex_h
){ // .. this code shuld be run serialised by the main thread..
agent = agent_h;
plot = plot_h;
mutex = mutex_h;
agent->cachedOffer = 0;
}
void
Manager_farmers_threads::run(){
// This code should be run parallelised...
mutex.lock();
agent->cachedOffer = agent->offerRentalPrice(plot);
mutex.unlock();
}
class Manager_farmers_threads
: public QThread { Q_OBJECT
public:
Manager_farmers_threads();
void assignJob
(Agent_space
* agent_h,
const Pixel
*plot_h,
QMutex* mutex_h
);
protected:
void run();
private:
volatile Agent_space* agent;
const Pixel* plot;
};
void
Manager_farmers::landAllocation(){
// .. serial code ..
QMutex* mutex = new QMutex;
for (int i=0; i < nThreads; i++){
Manager_farmers_threads* wThread = new Manager_farmers_threads;
wThreads.push_back(wThread);
}
// parallelise the code to z workingThreads..
for (uint y=0;y<myAgents.size();y++){
bool assigned = false;
while(!assigned) {
for (uint z=0;z<wThreads.size();z++){
if (!wThreads[z]->isRunning()){
wThreads[z]->assignJob(myAgents[y], freePlot, mutex);
wThreads[z]->start();
assigned = true;
break;
}
}
}
}
// be sure that all threads are ended..
for (int z=0; z < nThreads; z++){
wThreads[z]->wait();
}
// ..continue with serial code...
for (int i=0; i < nThreads; i++){
delete wThreads[i]; // deleting the threads
}
delete mutex;
}
void
Manager_farmers_threads::assignJob(Agent_space* agent_h, const Pixel* plot_h, QMutex* mutex_h){
// .. this code shuld be run serialised by the main thread..
agent = agent_h;
plot = plot_h;
mutex = mutex_h;
agent->cachedOffer = 0;
}
void
Manager_farmers_threads::run(){
// This code should be run parallelised...
mutex.lock();
agent->cachedOffer = agent->offerRentalPrice(plot);
mutex.unlock();
}
class Manager_farmers_threads : public QThread {
Q_OBJECT
public:
Manager_farmers_threads();
void assignJob(Agent_space* agent_h, const Pixel*plot_h, QMutex* mutex_h);
protected:
void run();
private:
volatile Agent_space* agent;
const Pixel* plot;
QMutex* mutex;
};
To copy to clipboard, switch view to plain text mode
Bookmarks