Created
February 10, 2022 05:38
-
-
Save jcsteh/2bb681d253a74da5a875e46ad6726a34 to your computer and use it in GitHub Desktop.
Spin all threads of a CPU.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* spinCpu: Spin all threads of a CPU. | |
* This is useful for testing how things behave when the CPU is fully utilised. | |
* Copyright 2022 James Teh | |
* License: Mozilla Public License version 2.0 | |
*/ | |
#include <iostream> | |
#include <thread> | |
#include <vector> | |
using namespace std; | |
void spin() { | |
for (; ;) { | |
} | |
} | |
int main() { | |
auto nThreads = thread::hardware_concurrency(); | |
cout << "Using " << nThreads << " threads" << endl; | |
--nThreads; // Main thread is 1 thread. | |
vector<thread> threads; | |
for (int t = 0; t < nThreads; ++t) { | |
threads.push_back(thread(spin)); | |
} | |
spin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment