CThreadHelper.cpp 931 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "CThreadHelper.h"
  2. #include <boost/thread.hpp>
  3. #include <boost/bind.hpp>
  4. /*
  5. * CThreadHelper.cpp, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. CThreadHelper::CThreadHelper(std::vector<boost::function<void()> > *Tasks, int Threads)
  14. {
  15. currentTask = 0; amount = Tasks->size();
  16. tasks = Tasks;
  17. threads = Threads;
  18. }
  19. void CThreadHelper::run()
  20. {
  21. boost::thread_group grupa;
  22. for(int i=0;i<threads;i++)
  23. grupa.create_thread(boost::bind(&CThreadHelper::processTasks,this));
  24. grupa.join_all();
  25. }
  26. void CThreadHelper::processTasks()
  27. {
  28. int pom;
  29. while(true)
  30. {
  31. rtinm.lock();
  32. if((pom=currentTask) >= amount)
  33. {
  34. rtinm.unlock();
  35. break;
  36. }
  37. else
  38. {
  39. ++currentTask;
  40. rtinm.unlock();
  41. (*tasks)[pom]();
  42. }
  43. }
  44. }