【Linux】 線程池

線程池

什么是線程池?
一次預先申請一批線程,讓這批線程有任務,就處理任務;沒任務,就處于等待狀態。
為什么要有線程池?
以空間換時間,預先申請一批線程,當有任務到來,可以直接指派給線程執行。

// task.hpp
#pragma once#include <functional>using namespace std;typedef function<int(int, int)> calc_func_t;class Task
{
public:Task() {}Task(int x, int y, calc_func_t func): _x(x), _y(y), _calc_func(func){}// 加法計算的任務int operator()() { return _calc_func(_x, _y); }int get_x() { return _x; }int get_y() { return _y; }
private:int _x;int _y;calc_func_t _calc_func;
};
// log.hpp
#pragma once#include <string>
#include <stdarg.h>
#include <unordered_map>using namespace std;#define LOG_FILE "./threadpool.log"// 日志是有日志級別的
enum LogLevel
{DEBUG,NORMAL,WARNING,ERROR,FATAL
};// 針對枚舉類型的哈希函數
template <typename T>
class EnumHash
{
public:size_t operator()(const T& t) const { return static_cast<size_t>(t); }
};
unordered_map<LogLevel, string, EnumHash<LogLevel>> logLevelMap = {{DEBUG, "DEBUG"},{NORMAL, "NORMAL"},{WARNING, "WARNING"},{ERROR , "ERROR"},{FATAL, "FATAL"}
};// 完整的日志功能,至少有:日志等級 時間 支持用戶自定義
void logMessage(LogLevel log_level, const char* format, ...)
{
#ifndef DEBUG_SHOWif(log_level == DEBUG) return; // DEBUG_SHOW沒有定義,不展示DEBUG信息
#endif char stdBuffer[1024]; // 標準部分char logBuffer[1024]; // 自定義部分time_t timestamp = time(nullptr);struct tm* ploct = localtime(&timestamp);snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%04d-%02d-%02d %02d:%02d:%02d]", logLevelMap[log_level].c_str(),\1900 + ploct->tm_year, 1 + ploct->tm_mon, ploct->tm_mday, ploct->tm_hour, ploct->tm_min, ploct->tm_sec);va_list args;va_start(args, format);vsnprintf(logBuffer, sizeof logBuffer, format, args);va_end(args);FILE* log_file = fopen(LOG_FILE, "a");fprintf(log_file, "%s %s\n", stdBuffer, logBuffer);fclose(log_file);
}

va_*系列函數與vprintf系列函數配合使用可以格式化打印傳入的可變參數的內容。
在這里插入圖片描述
在這里插入圖片描述

// thread.hpp
#pragma once#include <string>
#include <cstdio>
#include <pthread.h>using namespace std;// 對應創建線程時的routine函數的類型
typedef void*(*func_t)(void*);class ThreadData
{
public:void* _ptpool; // 指向線程池對象string _name;
};class Thread
{
public:Thread(int num, func_t callBack, void* _ptpool): _func(callBack){char nameBuffer[64];snprintf(nameBuffer, sizeof(nameBuffer), "Thread_%d", num);_tdata._name = nameBuffer;_tdata._ptpool = _ptpool;}void start() { pthread_create(&_tid, nullptr, _func, (void*)&_tdata); }void join() { pthread_join(_tid, nullptr); }const string& name() { return _tdata._name; }
private:pthread_t _tid; // 線程IDfunc_t _func; // 線程routineThreadData _tdata; // 線程數據
};
// threadPool.hpp
#pragma once#include <vector>
#include <queue>
#include "thread.hpp"
#include "lockGuard.hpp"
#include "log.hpp"const int g_thread_num = 3;// 線程池:本質是生產消費模型
template<class T>
class threadPool
{
private:threadPool(int thread_num = g_thread_num): _thread_num(thread_num){pthread_mutex_init(&_lock, nullptr);pthread_cond_init(&_cond, nullptr);for(int i = 0; i < _thread_num; ++i){_threads.push_back(new Thread(i + 1/*線程編號*/, routine, this/*可以傳this指針*/));}}threadPool(const threadPool<T>&) = delete;const threadPool<T>& operator=(const threadPool<T>&) = delete;
public:// 考慮多個線程使用單例的情況static threadPool<T>* getThreadPool(int thread_num = g_thread_num){if(nullptr == _pthread_pool){lockGuard lock_guard(&_pool_lock);// 在單例創建好后,鎖也就沒有意義了// 將來任何一個線程要獲取單例,仍必須調用getThreadPool接口// 這樣一定會存在大量的申請和釋放鎖的行為// 所以外層if判斷,用于在單例創建的情況下,攔截大量的線程因請求單例而訪問鎖的行為if(nullptr == _pthread_pool){_pthread_pool = new threadPool<T>(thread_num);}}return _pthread_pool;}void run(){for(auto& pthread : _threads){pthread->start();logMessage(NORMAL, "%s %s", (pthread->name()).c_str(), "啟動成功");}}void pushTask(const T& task){lockGuard lock_guard(&_lock);_task_queue.push(task);pthread_cond_signal(&_cond);}~threadPool(){for(auto& pthread : _threads){pthread->join();delete pthread;}pthread_mutex_destroy(&_lock);pthread_cond_destroy(&_cond);}
public:pthread_mutex_t* getMutex(){return &_lock;}bool isEmpty(){return _task_queue.empty();}void waitCond(){pthread_cond_wait(&_cond, &_lock);}T& getTask(){T& task = _task_queue.front();_task_queue.pop();return task;}
private:// 消費過程static void* routine(void* args){ThreadData* tdata = (ThreadData*)args;threadPool<T>* tpool = (threadPool<T>*)tdata->_ptpool;while(true){T task;{lockGuard lock_guard(tpool->getMutex());while (tpool->isEmpty()) tpool->waitCond();task = tpool->getTask();}logMessage(WARNING, "%s 處理完成: %d + %d = %d", (tdata->_name).c_str(), task.get_x(), task.get_y(), task());}}
private:vector<Thread*> _threads; // 數組存放創建的線程的地址int _thread_num; // 創建的線程個數queue<T> _task_queue; // 阻塞式任務隊列pthread_mutex_t _lock; // 針對任務隊列的鎖pthread_cond_t _cond; // 隊列空滿情況的條件變量static threadPool<T>* _pthread_pool; // 餓漢式線程池static pthread_mutex_t _pool_lock; // 針對線程池的鎖
};template<class T>
threadPool<T>* threadPool<T>::_pthread_pool = nullptr;
template<class T>
pthread_mutex_t threadPool<T>::_pool_lock = PTHREAD_MUTEX_INITIALIZER;
// test.cc
#include "task.hpp"
#include "threadPool.hpp"
#include <unistd.h>
#include <ctime>void test1()
{srand((unsigned int)time(nullptr) ^ getpid());threadPool<Task>::getThreadPool()->run();while(true){// 生產的過程 - 制作任務的時候要花時間的int x = rand() % 100 + 1;usleep(2023);int y = rand() % 50 + 1;Task task(x, y, [](int x, int y){ return x + y; });logMessage(DEBUG, "制作任務完成: %d + %d = ?", x, y);// 推送任務到線程池threadPool<Task>::getThreadPool()->pushTask(task);sleep(1);}
}
# Makefile
test:test.ccg++ -o $@ $^ -std=c++11 -lpthread -DDEBUG_SHOW
.PHONY:clean
clean:rm -f test

運行結果:
在這里插入圖片描述

自旋鎖

自旋鎖:本質是通過不斷檢測鎖的狀態,來確定資源是否就緒的方案。
什么時候使用自旋鎖?這個由臨界資源就緒的時間長短決定。
自旋鎖的初始化 & 銷毀:
在這里插入圖片描述
自旋鎖的加鎖:
在這里插入圖片描述
自旋鎖的解鎖:
在這里插入圖片描述

讀者寫者問題

寫者與寫者:互斥關系
讀者與寫者:互斥 & 同步關系
讀者與讀者:共享關系

讀者寫者問題和生產消費模型的本質區別在于,消費者會拿走數據(做修改),而讀者不會。
讀寫鎖的初始化 & 銷毀:
在這里插入圖片描述
讀寫鎖之讀者加鎖:
在這里插入圖片描述
讀寫鎖之寫者加鎖:
在這里插入圖片描述
讀寫鎖的解鎖:
在這里插入圖片描述
關于是讀者還是寫者優先的問題,拋開應用場景去談技術細節就是耍流氓。
而pthread庫中的讀寫鎖默認采用讀者優先,這類的應用場景主要是:數據被讀取的頻率非常高,被修改的頻率非常低。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/214130.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/214130.shtml
英文地址,請注明出處:http://en.pswp.cn/news/214130.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將rtsp視頻流發送到AWS Kinesis Video Streams的方案——使用Gstreamer(C++) Command Line

大綱 1 創建Kinesis Video Streams1.1 創建視頻流1.2 記錄Creation Time 2 創建策略2.1 賦予權限2.2 限制資源2.3 Json格式描述&#xff08;或上面手工設置&#xff09;2.4 注意事項 3 創建IAM用戶3.1 生成密鑰對3.2 附加策略3.3 記錄訪問密鑰對 4 編譯C 創建者庫5 發送6 檢查參…

JavaScript <關于逆向RSA非對稱加密算法的案例(代碼剖析篇)>--案例(五點一)

引用上文: CSDNhttps://mp.csdn.net/mp_blog/creation/editor/134857857 剖析: var bitsPerDigit16; // 每個數組元素可以表示的二進制位數// 數組復制函數&#xff0c;將源數組部分復制到目標數組的指定位置 function arrayCopy(src, srcStart, dest, destStart, n) {var m…

國內地址地區智能解析,無需完整地址也能正確匹配

頁面直接引入使用 已打包成單文件dist/bundle.js 可以直接通過標簽引用 <script src="./bundle.js"></script> <script>var results = AddressParse.parse(福建省福州市福清市石竹街道義明綜合樓3F,15000000000,asseek);console.log(results);…

OD機考真題搜集:服務失效判斷

題目 某系統中有眾多服務,每個服務用字符串(只包含字母和數字,長度<=10)唯一標識,服務間可能有依賴關系,如A依賴B,則當B故障時導致A也故障。 依賴具有傳遞性,如A依賴B,B依賴C,當C故障時導致B故障,也導致A故障。 給出所有依賴關系,以及當前已知故障服務,要求輸…

git提交代碼報錯Git: husky > pre-commit

目錄 git提交代碼報錯原因解決方法&#xff08;三種&#xff09;1、第一種2、第二種3、第三種 git提交代碼報錯原因 這個問題是因為當你在終端輸入git commit -m “XXX”,提交代碼的時候,pre-commit(客戶端)鉤子&#xff0c;它會在Git鍵入提交信息前運行做代碼風格檢查。如果代…

Kotlin 中密封類、枚舉類與密封接口的對比分析

在 Kotlin 編程語言中&#xff0c;密封類&#xff08;Sealed Classes&#xff09;、枚舉類&#xff08;Enum Classes&#xff09;和密封接口&#xff08;Sealed Interfaces&#xff09;是處理一組固定類型的強大工具。它們在 Kotlin 中扮演著特殊的角色&#xff0c;特別是在創建…

【小白專用】MySQL創建數據庫和創建數據表

1.在Windows開始搜索輸入Mysql,并選擇第一個打開。 2.輸入安裝時的密碼 3.說明安裝成功。 二、創建數據庫 1. 連接 MySQL 輸入 mysql -u root -p 命令&#xff0c;回車&#xff0c;然后輸入 MySQL 的密碼(不要忘記了密碼)&#xff0c;再回車&#xff0c;就連接上 MySQL 了。 …

數據庫常用鎖

數據庫鎖是一種用于管理并發訪問的機制&#xff0c;以確保數據的一致性和完整性。在并發訪問的情況下&#xff0c;多個事務可能同時嘗試訪問相同的數據&#xff0c;而數據庫鎖能夠協調這些訪問&#xff0c;防止數據不一致的問題。以下是一些常見的數據庫鎖及其詳細解釋&#xf…

C語言-統計素數并求和

本題要求統計給定整數M和N區間內素數的個數并對它們求和。 輸入格式: 輸入在一行中給出兩個正整數M和N&#xff08;1≤M≤N≤500&#xff09;。 輸出格式: 在一行中順序輸出M和N區間內素數的個數以及它們的和&#xff0c;數字間以空格分隔。 輸入樣例: 10 31輸出樣例: 7…

深入Redis過程-持久化

目錄 redis實現持久化 RDB 觸發機制-定期方法 定期-手動觸發 save bgsave 定期-自動觸發 AOF 開啟AOF功能 刷新緩沖區策略 重寫機制 混合持久化 Redis事務 事務相關的命令 MULTI EXEC DISCARD WATCH redis實現持久化 RDB RDB叫做Redis數據備份文件&#xf…

強大的公式編輯器 —— MathType最新版本安裝與使用

強大的公式編輯器 —— MathType最新版本安裝與使用 由于使用了很長時間的機械硬盤出現壞道&#xff0c;安裝在其中的MathType6.9&#xff08;精簡版&#xff09;也沒辦法使用了&#xff0c;本來想安裝個高版本的MathType&#xff0c;比如MathType7.4&#xff0c;但在網上苦苦…

如何更改Jupyter Notebook中的環境?

1.首先&#xff0c;打開終端 2.接著&#xff0c;分別輸入以下命令 conda env list 把EXPose替換為自己的環境變量 conda activate EXPose 3.接下來安裝‘ ipykernel ’軟件包 conda install ipykernel 4. 將該環境添加到Jupyter Notebook中&#xff1b;在Jupyter Notebook…

HTB Surveillance

Surveillance 2023年12月10日 12:13:35User nmap Starting Nmap 7.80 ( https://nmap.org ) at 2023-12-10 12:15 CST Stats: 0:00:37 elapsed; 0 hosts completed (1 up), 1 undergoing Connect Scan Connect Scan Timing: About 59.83% done

小白第一次開私服怎么吸引玩家

大家好&#xff0c;我是咕嚕-凱撒&#xff0c;在現在這個網絡社會很多人為了放松一下會選擇打打游戲&#xff0c;私服也就成為了許多玩家為了尋找新鮮體驗的熱門選擇&#xff0c;很多小白就發現了這個契機但是吸引玩家加入自己的服務器也就成了一個比較頭疼的問題&#xff0c;下…

Wrong number of values of control parameter 2(Halcon 錯誤代碼:1402)

threshold (ImageReduced1, Region, 0,min2(75,Min)) 程序運行到這一句&#xff0c;出現錯誤 原因是其中的參數Min為空數組 解決方案&#xff1a;判斷了下可以輸出Min的區域是否存在&#xff0c;不存在跳過這一步。

八叉樹bt文件轉為grid文件的代碼及編譯流程

目的 點云文件轉為八叉樹文件 代碼 在一個文件夾中新建兩個文件&#xff0c;pcd2bt.cpp和CMakeLists.txt&#xff0c;分別寫入&#xff1a; grid3d_node.cpp #include <ros/ros.h> #include <string> #include "grid3d.hpp"int main(int argc, char…

【Maven技術專題】「實戰開發系列」盤點Maven項目中打包需要注意到的那點事兒

Maven項目打包需要注意到的那點事兒 Maven是什么Maven打包插件的作用Maven打包后經常出現的問題maven構建可運行Jar包 Maven打包的三種方式Maven打包的最簡單的方法maven-jar-pluginMANIFEST.MF文件部分MANIFEST.MF的文件內容jar包的拷貝機制在pom.xml中配置 maven-jar-plugin的…

mybatis多表映射-分步查詢

1、建庫建表 create database mybatis-example; use mybatis-example; create table t_book (bid varchar(20) primary key,bname varchar(20),stuid varchar(20) ); insert into t_book values(b001,Java,s001); insert into t_book values(b002,Python,s002); insert into …

C++大型項目經驗

1 附加包含目錄 在Visual Studio中&#xff0c;“附加包含目錄”&#xff08;Additional Include Directories&#xff09;是一個編譯器設置&#xff0c;它指示編譯器在查找包含文件&#xff08;通常是頭文件&#xff0c;擴展名為.h或.hpp&#xff09;時去哪些額外的文件夾路徑…

函數的棧幀

我們每次在調用函數的時候&#xff0c;都說會進行傳參。每次創建函數&#xff0c;或者進行遞歸的時候&#xff0c;也會說會進行壓棧。 那么&#xff0c;今天我們就來具體看看函數到底是如何進行壓棧&#xff0c;傳參的操作。 什么是棧&#xff1f; 首先我們要知道&#xff0c;…