CThreadHelper.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #define VCMI_DLL
  2. #include "CThreadHelper.h"
  3. #include <boost/thread.hpp>
  4. #include <boost/bind.hpp>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #endif
  8. /*
  9. * CThreadHelper.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. CThreadHelper::CThreadHelper(std::vector<boost::function<void()> > *Tasks, int Threads)
  18. {
  19. currentTask = 0; amount = Tasks->size();
  20. tasks = Tasks;
  21. threads = Threads;
  22. }
  23. void CThreadHelper::run()
  24. {
  25. boost::thread_group grupa;
  26. for(int i=0;i<threads;i++)
  27. grupa.create_thread(boost::bind(&CThreadHelper::processTasks,this));
  28. grupa.join_all();
  29. }
  30. void CThreadHelper::processTasks()
  31. {
  32. int pom;
  33. while(true)
  34. {
  35. rtinm.lock();
  36. if((pom=currentTask) >= amount)
  37. {
  38. rtinm.unlock();
  39. break;
  40. }
  41. else
  42. {
  43. ++currentTask;
  44. rtinm.unlock();
  45. (*tasks)[pom]();
  46. }
  47. }
  48. }
  49. void setThreadName(long threadID, const std::string &name)
  50. {
  51. #ifdef _WIN32
  52. //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
  53. const DWORD MS_VC_EXCEPTION=0x406D1388;
  54. #pragma pack(push,8)
  55. typedef struct tagTHREADNAME_INFO
  56. {
  57. DWORD dwType; // Must be 0x1000.
  58. LPCSTR szName; // Pointer to name (in user addr space).
  59. DWORD dwThreadID; // Thread ID (-1=caller thread).
  60. DWORD dwFlags; // Reserved for future use, must be zero.
  61. } THREADNAME_INFO;
  62. #pragma pack(pop)
  63. THREADNAME_INFO info;
  64. info.dwType = 0x1000;
  65. info.szName = name.c_str();
  66. info.dwThreadID = threadID;
  67. info.dwFlags = 0;
  68. __try
  69. {
  70. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  71. }
  72. __except(EXCEPTION_EXECUTE_HANDLER)
  73. {
  74. }
  75. #else
  76. //*nix counterpart?
  77. #endif
  78. }