Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. ULONG WINAPI workThread(HANDLE hIOCP)
  2. {
  3.     ULONG dwBytes;
  4.     ULONG_PTR Key;
  5.     OVERLAPPED* Irp;
  6.     while(GetQueuedCompletionStatus(hIOCP, &dwBytes, &Key, &Irp, INFINITE))
  7.     {
  8.         DbgPrint("%x.%u>%p %p %x\n", GetCurrentThreadId(), GetTickCount(), Key, Irp, dwBytes);
  9.  
  10.         if (Irp)
  11.         {
  12.             delete Irp;
  13.             ULONG64 t = GetTickCount64() + 4000;// wait 4 sec
  14.             do
  15.             {
  16.             } while (GetTickCount64() < t);
  17.         }
  18.         else{
  19.             break;
  20.         }
  21.     }
  22.     return 0;
  23. }
  24.  
  25. ULONG StartThreads(HANDLE* phThreads, ULONG n, HANDLE hIOCP)
  26. {
  27.     ULONG m = 0;
  28.  
  29.     do
  30.     {
  31.         if (HANDLE hThread = CreateThread(0, 0x1000, workThread, hIOCP, 0, 0))
  32.         {
  33.             m++;
  34.             *phThreads++ = hThread;
  35.         }
  36.     } while (--n);
  37.  
  38.     return m;
  39. }
  40.  
  41. void testIOP()
  42. {
  43.     HANDLE hThreads[2], *phThreads;
  44.  
  45.     if (HANDLE hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 1))
  46.     {
  47.         int i = 4;
  48.         do
  49.         {
  50.             if (OVERLAPPED* Irp = new OVERLAPPED)
  51.             {
  52.                 if (!PostQueuedCompletionStatus(hIOCP, i, i, Irp))
  53.                 {
  54.                     delete Irp;
  55.                 }
  56.             }
  57.         } while (--i);
  58.  
  59.         PostQueuedCompletionStatus(hIOCP, 1, 1, 0);
  60.         PostQueuedCompletionStatus(hIOCP, 2, 2, 0);
  61.  
  62.         if (ULONG m = StartThreads(hThreads, RTL_NUMBER_OF(hThreads), hIOCP))
  63.         {
  64.             ULONGLONG t = GetTickCount64();
  65.  
  66.             WaitForMultipleObjects(m, hThreads, true, INFINITE);
  67.  
  68.             t = GetTickCount64() - t;
  69.  
  70.             DbgPrint("%I64u.%u\n", t / 1000, t % 1000);
  71.  
  72.             phThreads = hThreads + m;
  73.  
  74.             do
  75.             {
  76.                 CloseHandle(*--phThreads);
  77.             } while (--m);
  78.         }
  79.  
  80.         CloseHandle(hIOCP);
  81.     }
  82. }
  83. f1c.24551937>0000000000000004 000001F939E3E290 4
  84. f1c.24555937>0000000000000003 000001F939E3E150 3
  85. f1c.24559937>0000000000000002 000001F939E36460 2
  86. f1c.24563937>0000000000000001 000001F939E3A6D0 1
  87. f1c.24567937>0000000000000001 0000000000000000 1
  88. The thread 'Win64 Thread' (0xf1c) has exited with code 0 (0x0).
  89. e50.24567937>0000000000000002 0000000000000000 2
  90. The thread 'Win64 Thread' (0xe50) has exited with code 0 (0x0).
  91. 16.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement