CThreadHelper.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. si32 DLL_EXPORT getMyPid();
  28. template <typename T> inline void setData(T * data, boost::function<T()> func)
  29. {
  30. *data = func();
  31. }
  32. void DLL_EXPORT setThreadName(long threadID, const std::string &name);
  33. #define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \
  34. (boost::bind(&setData<TYPE>,&DESTINATION,FUNCTION_TO_GET))
  35. #define GET_SURFACE(SUR_DESTINATION, SUR_NAME) \
  36. (GET_DATA \
  37. (SDL_Surface*,SUR_DESTINATION,\
  38. boost::function<SDL_Surface*()>(boost::bind(&BitmapHandler::loadBitmap,SUR_NAME,true))))
  39. #define GET_DEF(DESTINATION, DEF_NAME) \
  40. (GET_DATA \
  41. (CDefHandler*,DESTINATION,\
  42. boost::function<CDefHandler*()>(boost::bind(CDefHandler::giveDef,DEF_NAME))))
  43. #define GET_DEF_ESS(DESTINATION, DEF_NAME) \
  44. (GET_DATA \
  45. (CDefEssential*,DESTINATION,\
  46. boost::function<CDefEssential*()>(boost::bind(CDefHandler::giveDefEss,DEF_NAME))))
  47. #endif // __CTHREADHELPER_H__