CThreadHelper.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. rtinm.lock();
  34. if((pom=currentTask) >= amount)
  35. {
  36. rtinm.unlock();
  37. break;
  38. }
  39. else
  40. {
  41. ++currentTask;
  42. rtinm.unlock();
  43. (*tasks)[pom]();
  44. }
  45. }
  46. }
  47. void setThreadName(long threadID, const std::string &name)
  48. {
  49. #ifdef _WIN32
  50. //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
  51. const DWORD MS_VC_EXCEPTION=0x406D1388;
  52. #pragma pack(push,8)
  53. typedef struct tagTHREADNAME_INFO
  54. {
  55. DWORD dwType; // Must be 0x1000.
  56. LPCSTR szName; // Pointer to name (in user addr space).
  57. DWORD dwThreadID; // Thread ID (-1=caller thread).
  58. DWORD dwFlags; // Reserved for future use, must be zero.
  59. } THREADNAME_INFO;
  60. #pragma pack(pop)
  61. THREADNAME_INFO info;
  62. info.dwType = 0x1000;
  63. info.szName = name.c_str();
  64. info.dwThreadID = threadID;
  65. info.dwFlags = 0;
  66. __try
  67. {
  68. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  69. }
  70. __except(EXCEPTION_EXECUTE_HANDLER)
  71. {
  72. }
  73. #else
  74. //*nix counterpart?
  75. #endif
  76. }