klenin / Spawner

Cross-platform sandbox runner
4 stars 8 forks source link

Processor usage value problem #10

Open ShigiDono opened 9 years ago

ShigiDono commented 9 years ago

Multithread program with while (true); in each thread (w/o optimization) producing >100% on multicore machine.

ShigiDono commented 9 years ago
#include <windows.h>

DWORD WINAPI ThreadProc(CONST LPVOID lpParam) {
  while (true);
  ExitThread(0);
}

INT main() {
  TCHAR szMessage[256];
  DWORD dwTemp, i;
  HANDLE hThreads[2];

  for(i = 0; i < 2; i++) {
    hThreads[i] = CreateThread(NULL, 0, &ThreadProc, NULL, 0, NULL);
  }

  WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);
  for(i = 0; i < 2; i++) {
    CloseHandle(hThreads[i]);
  }
  ExitProcess(0);
}

Producing ~ >150 % on 2-core machine

ShigiDono commented 9 years ago

Checked SetProcessAffinityMask(process_info.hProcess, 1);. Gives ~100%

ShigiDono commented 9 years ago

Check run.exe with same program/approach. This is the output of run.exe on this specific program. Running "another.exe", press ESC to terminate...

Program successfully terminated
  exit code:     1
  time consumed: 14.96 sec
  time passed:   8.66 sec
  peak memory:   5038080 bytes

run exe