vengy
vengy

Reputation: 2257

Stress test all CPU cores to 100% utilization with THREAD_PRIORITY_TIME_CRITICAL stops Windows from handling ctr+alt+delete?

Trying to create a C program to stress test all CPU cores to 100% utilization. However, it works too well such that I have to reboot Windows 11 because everything is unresponsive, even Ctrl + Alt + Delete.

Ctrl+Alt+Delete is a Windows function, baked into the OS. So no matter how frozen a program may be as long as the OS is still working it will detect that key combo and override everything else.

Why doesn't the Windows 11 OS detect Ctrl + Alt + Delete and allow the stress test program to be terminated?

#include <windows.h>
#include <stdio.h>

DWORD WINAPI consume_cpu(LPVOID arg)
{
    while (1) {}
    return 0;
}

int main(int argc, char** argv)
{
    // Get the number of CPU cores
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    int num_cores = sysinfo.dwNumberOfProcessors;

    HANDLE* threads = (HANDLE*)malloc(num_cores * sizeof(HANDLE));

    // Create one thread per CPU core
    for (int i = 0; i < num_cores; i++)
    {
        threads[i] = CreateThread(NULL, 0, consume_cpu, NULL, 0, NULL);
        SetThreadPriority(threads[i], THREAD_PRIORITY_TIME_CRITICAL);
    }

    // Wait for the threads to finish
    WaitForMultipleObjects(num_cores, threads, TRUE, INFINITE);

    free(threads);
    return 0;
}

Notes

As suggested in the comments, removing THREAD_PRIORITY_TIME_CRITICAL allows for 100% CPU utilization plus the OS is now responsive.

cpu100%

Upvotes: 2

Views: 327

Answers (0)

Related Questions