qt 中使用pthread
qt有自己的线程实现方式,但有时候我们由于某些原因,会用到pthread来实现多线程。
我把自己看到过的pthread使用方法记下来。如果你有更好的方法,或觉得此方法有什么缺点,请在评论区指出。
这个类去掉了一些冗余代码,编译时可能出现小错误。
CRecvJson.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "ReceiveJson.h" 2 #include<sys/time.h> 3 #include<unistd.h> 4 5 void sleep_ms(unsigned int secs) 6 { 7 struct timeval tval; 8 tval.tv_sec = secs / 1000; 9 tval.tv_usec = (secs * 1000) % 1000000; 10 select(0, NULL, NULL, NULL, &tval); 11 } 12 13 unsigned long TimeElapseUs(timeval& start, timeval& end) 14 { 15 unsigned long un1 = 1000000*(end.tv_sec - start.tv_sec); 16 unsigned long un2 = (end.tv_usec - start.tv_usec); 17 unsigned long diff = un1 + un2; 18 19 return diff; 20 } 21 22 CReceiveJson::CReceiveJson(void) 23 { 24 } 25 26 27 CReceiveJson::~CReceiveJson(void) 28 { 29 } 30 31 void CReceiveJson::PushJsonData(char* pRecv, int nRecvLen) 32 { 33 char* pRecvData = new char[nRecvLen + 1]; 34 memset(pRecvData, 0, nRecvLen + 1); 35 memcpy(pRecvData, pRecv, nRecvLen); 36 37 m_lock.Lock(); 38 m_dequeJson.push_front(pRecvData); 39 m_lock.UnLock(); 40 } 41 42 void CReceiveJson::InitRecvThd() 43 { 44 m_bRecvRunning = true; 45 int nRet = pthread_create(&m_recvThdId, NULL, RecieveThd, this); 46 if(nRet == 0) 47 { 48 49 } 50 } 51 52 void CReceiveJson::UnInitParaseThd() 53 { 54 m_bRecvRunning = false; 55 } 56 57 void *CReceiveJson::RecieveThd(void* lParam) 58 { 59 CReceiveJson* pThis = (CReceiveJson*)lParam; 60 pThis->DoRecieve(); 61 62 return NULL; 63 } 64 65 void CReceiveJson::DoRecieve() 66 { 67 while(m_bRecvRunning) 68 { 69 if (m_dequeJson.size() > 0) 70 { 71 m_lock.Lock(); 72 char* pRecieve = m_dequeJson.back(); 73 74 PopJsonData(); 75 m_lock.UnLock(); 76 } 77 78 sleep_ms(ThdSleepTime); 79 } 80 81 pthread_detach(m_recvThdId); 82 } 83 84 void CReceiveJson::PopJsonData() 85 { 86 char* p = m_dequeJson.back(); 87 delete[] p; 88 p = NULL; 89 m_dequeJson.pop_back(); 90 }
CRecvJson.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma once 2 #include "TLock.h" 3 #include <deque> 4 #include<map> 5 #include<pthread.h> 6 #include<string> 7 8 using namespace std; 9 10 class CReceiveJson 11 { 12 public: 13 CReceiveJson(void); 14 ~CReceiveJson(void); 15 public: 16 void PushJsonData(char* pRecv, int nRecvLen); 17 void UnInitParaseThd(); 18 static void* RecieveThd(void* lParam); 19 void InitRecvThd(); 20 private: 21 TLock m_lock; 22 deque<char*> m_dequeJson; 23 bool m_bRecvRunning; 24 pthread_t m_recvThdId; 25 26 27 void DoRecieve(); 28 void PopJsonData(); 29 30 //bool IsHeartBeatTimeOut( struct lws* wsi, map<lws*, timeval>& mapHb ); 31 };
TLock.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @file 3 * @brief 对锁的封装 4 * @Author 5 * @date 6 */ 7 #ifndef __SIROTLOCK_H__ 8 #define __SIROTLOCK_H__ 9 #pragma once 10 11 #if 0 12 #include <windows.h> 13 14 #define T_MUTEX CRITICAL_SECTION //利用临界区实现的锁变量 15 #define T_MUTEX_INIT(m) InitializeCriticalSection(m) 16 #define T_MUTEX_LOCK(m) EnterCriticalSection(m) 17 #define T_MUTEX_UNLOCK(m) LeaveCriticalSection(m) 18 #define T_MUTEX_DESTORY(m) DeleteCriticalSection(m) 19 20 #else 21 #include<pthread.h> 22 #define T_MUTEX pthread_mutex_t 23 #define T_MUTEX_INIT(m) pthread_mutex_init(m,NULL) 24 #define T_MUTEX_LOCK(m) pthread_mutex_lock(m) 25 #define T_MUTEX_UNLOCK(m) pthread_mutex_unlock(m) 26 #define T_MUTEX_DESTORY(m) pthread_mutex_destroy(m) 27 28 #endif 29 30 31 class TLock 32 { 33 public: 34 TLock(void) 35 { 36 T_MUTEX_INIT(&m_Lock); 37 } 38 39 ~TLock(void) 40 { 41 T_MUTEX_DESTORY(&m_Lock); 42 } 43 44 public: 45 void Lock() { T_MUTEX_LOCK(&m_Lock); } 46 void UnLock() { T_MUTEX_UNLOCK(&m_Lock); } 47 private: 48 T_MUTEX m_Lock; 49 }; 50 51 #endif