CThreadHelper.h 1.6 KB

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