simple cpp pool to limit the amount of thread running currently

simple cpp pool to limit the amount of thread running currently to make the computer more responsive.

short story

  • wrapping the target function with lambda function.
  • condition variable to block the wrapping function, limiting running thread amount.

long story

#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <string>
#include <chrono>
#include <cstdlib>
using namespace std;
class ThreadPoolAA
{
public:
mutex mtx;
mutex mtxCounter;
condition_variable condV;
int runingThreadN = 0;
int runingThreadMax = 16;
vector<thread> thds;
ThreadPoolAA(int N)
{
runingThreadMax = N;
}
template<typename Fn, typename ...Args>
int add(Fn fx, Args... argx)
{
//Lambda expressions (since C++11) - cppreference.com
//https://en.cppreference.com/w/cpp/language/lambda
//& (implicitly capture the used automatic variables by reference) and
//= (implicitly capture the used automatic variables by copy).
//The current object (*this) can be implicitly captured if either capture default is present.
//If implicitly captured, it is always captured by reference, even if the capture default is =
thds.push_back(thread([=, this]() {
{
unique_lock<mutex> lck(mtx);
//cout << "free" << endl;
condV.wait(lck, [this]() {return runingThreadN < runingThreadMax; });
lock_guard<mutex> lckCounter(mtxCounter);
runingThreadN++;
}
//cout << "runing thread n = " << runingThreadN << endl;
this_thread::sleep_for(chrono::milliseconds(500));
fx(argx...);
{
lock_guard<mutex> lckCounter(mtxCounter);
runingThreadN--;
//cout << "runing thread bb n = " << runingThreadN << endl;
condV.notify_one();
}
}));
//cout << "thread created." << endl;
return 0;
}
int join()
{
for (int idx = 0; idx < thds.size(); idx++)
{
thds[idx].join();
cout << "joined " << idx << endl;
}
return 0;
}
};
int fnc0(string aa)
{
this_thread::sleep_for(chrono::milliseconds(rand() % 1000));
cout << "fnc0 " << aa << endl;
return 0;
}
int fnc1(string aa, string ab)
{
this_thread::sleep_for(chrono::milliseconds(rand() % 1000));
cout << "fnc1 " << aa << ab << endl;
return 0;
}
int main()
{
fnc0("aa");
fnc1("ba", "bb");
cout << "================================" << endl;
ThreadPoolAA poolAA(4);
for (int idx = 0; idx < 11; idx++)
{
poolAA.add(fnc0, "ea");
poolAA.add(fnc1, "fa", "fb");
}
poolAA.join();
cout << "all done." << endl;
return 0;
}
//ref
//vit-vit/CTPL: Modern and efficient C++ Thread Pool Library
//https://github.com/vit-vit/CTPL
//Lambda expressions (since C++11) - cppreference.com
//https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture
//c++ - Understanding the dots in variadic template function - Stack Overflow
//https://stackoverflow.com/questions/23485643/understanding-the-dots-in-variadic-template-function

posted on   yusisc  阅读(30)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示