Skip to content

Instantly share code, notes, and snippets.

@jcsteh
Created February 10, 2022 05:38
Show Gist options
  • Save jcsteh/2bb681d253a74da5a875e46ad6726a34 to your computer and use it in GitHub Desktop.
Save jcsteh/2bb681d253a74da5a875e46ad6726a34 to your computer and use it in GitHub Desktop.
Spin all threads of a CPU.
/*
* 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