当前位置:网站首页>Multithreaded application (thread pool, singleton mode)
Multithreaded application (thread pool, singleton mode)
2022-07-07 11:07:00 【Exy-】
Thread pool
A bunch of threads handle tasks , It is mainly aimed at scenarios that need to be handled by a large number of tasks , Using multiple execution streams can improve processing efficiency
If a task arrives, create a thread to handle it, which has great disadvantages :
- cost : Total time = Thread creation time + Task processing time + Thread destruction time , If the task processing time is short , Then a lot of time is consumed by thread creation and destruction
- risk : If a large number of threads come in , The system may collapse under peak pressure
thought : Thread pool is actually a pile of created threads and a task queue , When a task comes, it is thrown into the thread pool , Allocate a thread to process
There is a maximum number of threads and task nodes in the thread pool , Avoid resource consumption .
Realization :
typedef void (*handler_t)(int data);
class ThreadTask{
private:
int _data;// Data to process
handler_t _handler;// Functions that process data
public:
ThreadTask() {}
ThreadTask(int data, handler_t handler):_data(data),
_handler(handler){}
void Run(){
_handler(_data);
}
};
class BlockQueue
{
private:
std::queue<ThreadTask> _queue;
int _capacity;
pthread_mutex_t _mutex;
pthread_cond_t _cond_pro;
pthread_cond_t _cond_con;
public:
BlockQueue(int maxq = MAXQ):_capacity(maxq){
pthread_mutex_init(&_mutex, NULL);
pthread_cond_init(&_cond_pro, NULL);
pthread_cond_init(&_cond_con, NULL);
}
~BlockQueue() {
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond_pro);
pthread_cond_destroy(&_cond_con);
}
bool Push(const ThreadTask &data){
pthread_mutex_lock(&_mutex);
while(_queue.size() == _capacity) {
pthread_cond_wait(&_cond_pro, &_mutex);
}
_queue.push(data);
pthread_mutex_unlock(&_mutex);
pthread_cond_signal(&_cond_con);
return true;
}
bool Pop(ThreadTask *data) {
pthread_mutex_lock(&_mutex);
while(_queue.empty() == true) {
pthread_cond_wait(&_cond_con, &_mutex);
}
*data = _queue.front();
_queue.pop();
pthread_mutex_unlock(&_mutex);
pthread_cond_signal(&_cond_pro);
return true;
}
};
class Threadpool{
private:
int _max_thread;// Maximum number of threads
int _max_queue;// The maximum number of nodes in the task queue
BlockQueue _queue;
public:
Threadpool(int max_thr=MAX_THREAD, int max_q=MAXQ):
_max_thread(max_thr),
_max_queue(max_q), _queue(max_q){
pthread_t tid;
int ret;
for (int i = 0; i < max_thr; i++) {
ret = pthread_create(&tid, NULL, thr_entry, this);
if (ret != 0) {
printf("thread create error\n");
exit(-1);
}
pthread_detach(tid);
}
}
static void *thr_entry(void *arg){
Threadpool *pool = (Threadpool*)arg;
while(1) {
ThreadTask task;
pool->_queue.Pop(&task);
task.Run();
}
}
bool TaskPush(const ThreadTask &task) {
_queue.Push(task);
}
};The singleton pattern
Aimed at the scene : A class can only instantiate one object , Provide an access interface , In other words, a resource can only have one copy in memory
Purpose : To save memory ; Prevent data ambiguity ;
Concrete realization
Hungry and lazy mode
All hungry resources are loaded in advance and initialized , Use it directly when you use it ( Space for time )
- Constructor privatization , Cannot instantiate object outside class
- Instantiate globally unique objects within classes , Resources are shared separately , Pre run initialization , The initialization process does not consider thread safety issues
lazy : Load when resources are used ( It's used a lot )
- Constructor private
- Lock the access interface to protect the resource initialization loading process
- Second test , Prevent lock conflicts , Increase of efficiency
- Prevent the compiler from over Optimizing , Use volatile Modify pointer member variables
// Starving model
class Singleton
{
public:
static Singleton* GetInstance()
{
return &_instance;
}
private:
Singleton(){};
Singleton(Singleton const&);
static Singleton _instance;// Global unique object
};// The sluggard model
class Singleton
{
public:
volatile static Singleton* GetInstance()
{
if (_instance == nullptr)
{
m_mutex.lock();
if (_instance == nullptr)
{
_instance = new Singleton();
}
m_mutex.unlock();
}
return _instance;
}
private:
Singleton() {};// Constructor private
volatile static Singleton* _instance;// Singleton object pointer
static mutex m_mutex;// The mutex
};边栏推荐
- mif 文件格式记录
- 中级软件评测师考什么
- How to successfully pass the senior system architecture designer in the second half of the year?
- Opencv installation and environment configuration - vs2017
- How much review time does it usually take to take the intermediate soft exam?
- 【OneNote】无法连接到网络,无法同步问题
- Kitex retry mechanism
- 【STM32】实战3.1—用STM32与TB6600驱动器驱动42步进电机(一)
- Transaction rolled back because it has been marked as rollback only
- Use of dotween
猜你喜欢

Opencv installation and environment configuration - vs2017
![[pyqt] the cellwidget in tablewidget uses signal and slot mechanism](/img/0e/02265f7195ca0add4155694530822a.png)
[pyqt] the cellwidget in tablewidget uses signal and slot mechanism
![[untitled]](/img/f9/18b85ad17d4c560f2b9d95a53ee72a.jpg)
[untitled]

Transaction rolled back because it has been marked as rollback only
![1321: [example 6.3] deletion problem (noip1994)](/img/bd/b605ec7b901079a9ebaca446fad7fb.png)
1321: [example 6.3] deletion problem (noip1994)
![[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk](/img/41/3a9450a84291ba04caee65241bce5d.png)
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk

高级软考(网络规划设计师)该如何备考?

SQL Server knowledge gathering 9: modifying data

Unity script generates configurable files and loads

【推荐系统 01】Rechub
随机推荐
Wallhaven wallpaper desktop version
A case of compiling QT file qmake compiling script
How much review time does it usually take to take the intermediate soft exam?
变量的解构赋值
POJ1821 Fence 题解报告
软考中级,软件设计师考试那些内容,考试大纲什么的?
Simple and easy to modify spring frame components
【pyqt】tableWidget里的cellWidget使用信号与槽机制
【OneNote】无法连接到网络,无法同步问题
P2788 math 1 - addition and subtraction
【推薦系統 01】Rechub
[untitled]
2022.7.4DAY596
Some online academic report websites and machine learning videos
[recommendation system 02] deepfm, youtubednn, DSSM, MMOE
Deconstruction and assignment of variables
2022.7.6DAY598
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
ADB utility commands (network package, log, tuning related)
Deep understanding of Apache Hudi asynchronous indexing mechanism