CThreadHelper.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "StdInc.h"
  2. #include "CThreadHelper.h"
  3. #ifdef _WIN32
  4. #include <windows.h>
  5. #endif
  6. /*
  7. * CThreadHelper.cpp, 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. CThreadHelper::CThreadHelper(std::vector<boost::function<void()> > *Tasks, int Threads)
  16. {
  17. currentTask = 0; amount = Tasks->size();
  18. tasks = Tasks;
  19. threads = Threads;
  20. }
  21. void CThreadHelper::run()
  22. {
  23. boost::thread_group grupa;
  24. for(int i=0;i<threads;i++)
  25. grupa.create_thread(boost::bind(&CThreadHelper::processTasks,this));
  26. grupa.join_all();
  27. }
  28. void CThreadHelper::processTasks()
  29. {
  30. int pom;
  31. while(true)
  32. {
  33. {
  34. boost::unique_lock<boost::mutex> lock(rtinm);
  35. if((pom = currentTask) >= amount)
  36. break;
  37. else
  38. ++currentTask;
  39. }
  40. (*tasks)[pom]();
  41. }
  42. }
  43. void setThreadName(long threadID, const std::string &name)
  44. {
  45. #ifdef _WIN32
  46. //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
  47. const DWORD MS_VC_EXCEPTION=0x406D1388;
  48. #pragma pack(push,8)
  49. typedef struct tagTHREADNAME_INFO
  50. {
  51. DWORD dwType; // Must be 0x1000.
  52. LPCSTR szName; // Pointer to name (in user addr space).
  53. DWORD dwThreadID; // Thread ID (-1=caller thread).
  54. DWORD dwFlags; // Reserved for future use, must be zero.
  55. } THREADNAME_INFO;
  56. #pragma pack(pop)
  57. THREADNAME_INFO info;
  58. info.dwType = 0x1000;
  59. info.szName = name.c_str();
  60. info.dwThreadID = threadID;
  61. info.dwFlags = 0;
  62. __try
  63. {
  64. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  65. }
  66. __except(EXCEPTION_EXECUTE_HANDLER)
  67. {
  68. }
  69. #else
  70. //*nix counterpart?
  71. #endif
  72. }