神农架网站建设,绍兴市网站建设,网站开发必须要做前端吗,net网站建设教程背景
有时候为了C测试程序在cpu资源紧张情况下是否正常#xff0c;需要人为创造cpu资源紧张
编译方法
g -o dummp_worker dummp_worker.cpp -stdc11 -pthread
使用方法
./dummp_worker 4 0.2
占用4个cpu核的20%比例的cpu资源
源码
// dummp_worker.cpp
#include c…背景
有时候为了C测试程序在cpu资源紧张情况下是否正常需要人为创造cpu资源紧张
编译方法
g -o dummp_worker dummp_worker.cpp -stdc11 -pthread
使用方法
./dummp_worker 4 0.2
占用4个cpu核的20%比例的cpu资源
源码
// dummp_worker.cpp
#include cassert
#include chrono
#include iostream
#include thread
#include vectorusing namespace std;
using namespace chrono;void busy_wait(size_t nanosec) {auto t0 high_resolution_clock::now();while (duration_castnanoseconds(high_resolution_clock::now() - t0).count() nanosec) {}
}[[noreturn]] void work(float percentage) {assert(percentage 0.f percentage 1.f);constexpr float period 1000000.0f;while (true) {// busy_wait(900000);// this_thread::sleep_for(100000ns);busy_wait(static_castsize_t(period * percentage));this_thread::sleep_for(nanoseconds(static_castsize_t(period * (1.f - percentage))));}
}int main(int argc, char* argv[]) {if (argc 3) {cout Args: worker_num occupation_rate.\n;return 0;}const int num stoi(argv[1]);const float percentage stof(argv[2]);if (num 1) {cout Error: num of workers less than 1.\n;return 0;}if (percentage 0.f || percentage 1.f) {cout Error: occupation rate should be between [0.0, 1.0].\n;return 0;}cout num of workers: num \n occupation rate: percentage \n;// do the workvectorthread tds;for (size_t i 0; i num; i) {tds.emplace_back(work, percentage);}tds[0].join();
}参考
https://github.com/mightbxg/DummyWorker