Hello.. I am in the progress of parallelise a multi-agent model where "agents" are different instances (thousands) of a class and they have to independently perform a relatively cpu-intensive task (few seconds).
Looks the perfect situation for a reentrant code .... it seems...
My problem is that, even if I mutex.lock() all the should-be-concurrent code (for debugging reasons of course) my application crash, while if I use a single thread it works.
This is the code that I implemented so far:
Qt Code:
  1. void
  2. Manager_farmers::landAllocation(){
  3. // .. serial code ..
  4.  
  5. for (int i=0; i < nThreads; i++){
  6. Manager_farmers_threads* wThread = new Manager_farmers_threads;
  7. wThreads.push_back(wThread);
  8. }
  9.  
  10. // parallelise the code to z workingThreads..
  11. for (uint y=0;y<myAgents.size();y++){
  12. bool assigned = false;
  13. while(!assigned) {
  14. for (uint z=0;z<wThreads.size();z++){
  15. if (!wThreads[z]->isRunning()){
  16. wThreads[z]->assignJob(myAgents[y], freePlot);
  17. wThreads[z]->start();
  18. assigned = true;
  19. break;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. // be sure that all threads are ended..
  26. for (int z=0; z < nThreads; z++){
  27. wThreads[z]->wait();
  28. }
  29.  
  30. // ..continue with serial code...
  31.  
  32. for (int i=0; i < nThreads; i++){
  33. delete wThreads[i]; // deleting the threads
  34. }
  35. }
  36.  
  37. void
  38. Manager_farmers_threads::assignJob(Agent_space* agent_h, const Pixel* plot_h){
  39. // .. this code shuld be run serialised by the main thread..
  40. agent = agent_h;
  41. plot = plot_h;
  42. agent->cachedOffer = 0;
  43. }
  44.  
  45. void
  46. Manager_farmers_threads::run(){
  47. // This code should be run parallelised...
  48. QMutex localMutex;
  49. localMutex.lock();
  50. agent->cachedOffer = agent->offerRentalPrice(plot);
  51. localMutex.unlock();
  52. }
To copy to clipboard, switch view to plain text mode 
The question is.. why for hell it is crashing even if all the code is locked ?