CThreadHelper.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. /*
  3. * CThreadHelper.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. typedef std::function<void()> Task;
  12. /// Can assign CPU work to other threads/cores
  13. class DLL_LINKAGE CThreadHelper
  14. {
  15. boost::mutex rtinm;
  16. int currentTask, amount, threads;
  17. std::vector<Task> *tasks;
  18. void processTasks();
  19. public:
  20. CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
  21. void run();
  22. };
  23. template <typename T> inline void setData(T * data, std::function<T()> func)
  24. {
  25. *data = func();
  26. }
  27. void DLL_LINKAGE setThreadName(const std::string &name);
  28. #define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \
  29. (std::bind(&setData<TYPE>,&DESTINATION,FUNCTION_TO_GET))
  30. #define GET_SURFACE(SUR_DESTINATION, SUR_NAME) \
  31. (GET_DATA \
  32. (SDL_Surface*,SUR_DESTINATION,\
  33. std::function<SDL_Surface*()>(std::bind(&BitmapHandler::loadBitmap,SUR_NAME,true))))
  34. #define GET_DEF(DESTINATION, DEF_NAME) \
  35. (GET_DATA \
  36. (CDefHandler*,DESTINATION,\
  37. std::function<CDefHandler*()>(std::bind(CDefHandler::giveDef,DEF_NAME))))
  38. #define GET_DEF_ESS(DESTINATION, DEF_NAME) \
  39. (GET_DATA \
  40. (CDefEssential*,DESTINATION,\
  41. std::function<CDefEssential*()>(std::bind(CDefHandler::giveDefEss,DEF_NAME))))