Advertisement
Guest User

Untitled

a guest
May 20th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. struct TASK_DATA
  2. {
  3.     LONG _dwTaskCount, _dwRefCount, _dwReadyCount;
  4.     HANDLE _hEvent[2];
  5.     ULONG _iLoop;
  6.     bool _bExit;
  7.  
  8.     TASK_DATA() : _dwRefCount(1), _dwTaskCount(0), _hEvent{}, _bExit(false), _dwReadyCount(0), _iLoop(0) {}
  9.  
  10.     ~TASK_DATA() {
  11.         if (_hEvent[1]) CloseHandle(_hEvent[1]);
  12.         if (_hEvent[0]) CloseHandle(_hEvent[0]);
  13.     }
  14.  
  15.     void AddRef()
  16.     {
  17.         InterlockedIncrementNoFence(&_dwRefCount);
  18.     }
  19.  
  20.     void Release()
  21.     {
  22.         if (!InterlockedDecrement(&_dwRefCount))
  23.         {
  24.             delete this;
  25.         }
  26.     }
  27.  
  28.     ULONG Start(ULONG i)
  29.     {
  30.         ULONG n = 0;
  31.         if ((_hEvent[0] = CreateEvent(0, TRUE, FALSE, 0)) &&
  32.             (_hEvent[1] = CreateEvent(0, TRUE, FALSE, 0)))
  33.         {
  34.             do
  35.             {
  36.                 n += AddThread();
  37.             } while (--i);
  38.         }
  39.  
  40.         if (n)
  41.         {
  42.             SetEvent(_hEvent[0]);
  43.         }
  44.         return n;
  45.     }
  46.  
  47.     ULONG AddThread()
  48.     {
  49.         AddRef();
  50.         if (HANDLE hThread = CreateThread(0, 0, s_CalcThread, this, 0, 0))
  51.         {
  52.             _dwTaskCount++;
  53.             CloseHandle(hThread);
  54.             return 1;
  55.         }
  56.         Release();
  57.         return 0;
  58.     }
  59.  
  60.     void EndTask(bool bReadyForNewTask)
  61.     {
  62.         if (bReadyForNewTask)
  63.         {
  64.             InterlockedIncrementNoFence(&_dwReadyCount);
  65.         }
  66.  
  67.         if (!InterlockedDecrement(&_dwTaskCount))
  68.         {
  69.             MessageBoxW(0, L"do some calc synthesis stuff.", L"", MB_ICONWARNING);
  70.  
  71.             if (_dwTaskCount = _dwReadyCount)
  72.             {
  73.                 _dwReadyCount = 0;
  74.                 ResetEvent(_hEvent[_iLoop]);
  75.                 SetEvent(_hEvent[_iLoop = 1 - _iLoop]);
  76.             }
  77.         }
  78.     }
  79.  
  80.     void CalcThread()
  81.     {
  82.         bool bReadyForNewTask = true;
  83.  
  84.         WCHAR sz[64], wz[64];
  85.         swprintf_s(wz, L"[%x]", GetCurrentThreadId());
  86.  
  87.         int i = 1, j = 0;
  88.         do
  89.         {
  90.             if (WaitForSingleObject(_hEvent[i = 1 - i], INFINITE) == WAIT_OBJECT_0 &&  !_bExit)
  91.             {
  92.                 swprintf_s(sz, L"do some calc stuff #%u", j++);
  93.                 MessageBoxW(0, sz, wz, MB_ICONINFORMATION);
  94.             }
  95.             else
  96.             {
  97.                 bReadyForNewTask = false;
  98.             }
  99.  
  100.             EndTask(bReadyForNewTask);
  101.  
  102.         } while (bReadyForNewTask);
  103.  
  104.         Release();
  105.     }
  106.  
  107.     static ULONG WINAPI s_CalcThread(void* This)
  108.     {
  109.         reinterpret_cast<TASK_DATA*>(This)->CalcThread();
  110.         return 0;
  111.     }
  112. };
  113.  
  114. void demo()
  115. {
  116.     if (TASK_DATA* p = new TASK_DATA)
  117.     {
  118.         if (p->Start(3))
  119.         {
  120.             MessageBoxW(0, L"Task Start", 0, MB_ICONWARNING);
  121.             p->_bExit = true;
  122.         }
  123.         p->Release();
  124.  
  125.         MessageBoxW(0, L"Task End", 0, MB_ICONWARNING);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement