├── readme.txt ├── CLinuxOperatingSystem.cpp ├── CCountingSem.cpp ├── COperatingSystem.cpp ├── CMutex.cpp ├── CMsgQueue.cpp ├── TestThread.h ├── TestThreadB.h ├── main.cpp ├── CLinuxMsgQueue.h ├── CLinuxCountingSem.cpp ├── TestThread.cpp ├── TestThreadB.cpp ├── COperatingSystem.h ├── COperatingSystemFactory.cpp ├── Makefile ├── CCountingSem.h ├── CMutex.h ├── COperatingSystemFactory.h ├── CLinuxMutex.h ├── CMsgQueue.h ├── CLinuxCountingSem.h ├── CLinuxOperatingSystem.h ├── CThread.cpp ├── CLinuxMsgQueue.cpp ├── CLinuxMutex.cpp └── CThread.h /readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh267/Cplusplus_Thread_Lib/HEAD/readme.txt -------------------------------------------------------------------------------- /CLinuxOperatingSystem.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh267/Cplusplus_Thread_Lib/HEAD/CLinuxOperatingSystem.cpp -------------------------------------------------------------------------------- /CCountingSem.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "CCountingSem.h" 4 | 5 | 6 | 7 | 8 | 9 | CCountingSem::CCountingSem() 10 | { 11 | 12 | } 13 | 14 | 15 | 16 | CCountingSem::~CCountingSem() 17 | { 18 | 19 | 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /COperatingSystem.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "COperatingSystem.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | 14 | 15 | COperatingSystem::COperatingSystem() 16 | { 17 | 18 | } 19 | 20 | 21 | 22 | COperatingSystem::~COperatingSystem() 23 | { 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /CMutex.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "CMutex.h" 5 | 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | 15 | 16 | CMutex::CMutex(const char *pName ) 17 | { 18 | if(pName!=NULL) 19 | { 20 | mutex_name=(char *)malloc(sizeof(strlen(pName))); 21 | strcpy(mutex_name,pName); 22 | } 23 | } 24 | 25 | 26 | 27 | CMutex::~CMutex() 28 | { 29 | 30 | } 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /CMsgQueue.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "CMsgQueue.h" 5 | 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | 16 | CMsgQueue::CMsgQueue(const char *pName) 17 | { 18 | if(pName!=NULL) 19 | { 20 | msg_queue_name=(char *)malloc(sizeof(strlen(pName))); 21 | strcpy(msg_queue_name,pName); 22 | } 23 | } 24 | 25 | 26 | 27 | CMsgQueue::~CMsgQueue() 28 | { 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /TestThread.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _TestThread_h_ 3 | #define _TestThread_h_ 4 | 5 | 6 | #include "CThread.h" 7 | #include "CMsgQueue.h" 8 | 9 | class TestThread:public CThread 10 | { 11 | 12 | public: 13 | TestThread(const char *m_name); 14 | ~TestThread(); 15 | 16 | virtual void mainLoop(); 17 | 18 | 19 | void setMsgQueue(CMsgQueue *q); 20 | 21 | private: 22 | CMsgQueue *p_msg_rev; 23 | 24 | 25 | 26 | 27 | }; 28 | 29 | 30 | 31 | 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /TestThreadB.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _Test_thread_b_h_ 4 | #define _Test_thread_b_h_ 5 | 6 | 7 | #include "CThread.h" 8 | #include "CMsgQueue.h" 9 | #include "TestThread.h" 10 | 11 | class TestThreadB:public CThread 12 | { 13 | 14 | public: 15 | TestThreadB(const char *m_name); 16 | ~TestThreadB(); 17 | 18 | virtual void mainLoop(); 19 | 20 | void setMsgQueue(CMsgQueue *q); 21 | 22 | private: 23 | 24 | CMsgQueue *p_msg_send; 25 | 26 | 27 | }; 28 | 29 | 30 | 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "TestThread.h" 5 | #include "TestThreadB.h" 6 | #include "CMsgQueue.h" 7 | #include "COperatingSystemFactory.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | int main() 17 | { 18 | 19 | CMsgQueue *q=COperatingSystemFactory::newMsgQueue("B to A message Queue"); 20 | 21 | 22 | TestThread *a=new TestThread("A"); 23 | TestThreadB *b=new TestThreadB("B"); 24 | 25 | a->setMsgQueue(q); 26 | b->setMsgQueue(q); 27 | 28 | a->run(); 29 | b->run(); 30 | 31 | 32 | while(1) 33 | ; 34 | 35 | 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /CLinuxMsgQueue.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CLinuxMsg_h_ 4 | #define _CLinuxMsg_h_ 5 | #include 6 | #include "CMsgQueue.h" 7 | #include "CMutex.h" 8 | #include "CCountingSem.h" 9 | 10 | 11 | class CLinuxMsgQueue : public CMsgQueue 12 | { 13 | public: 14 | CLinuxMsgQueue(const char *pName=NULL); 15 | ~CLinuxMsgQueue(); 16 | 17 | virtual bool recvMsg(unsigned int &m_msg_code,void *&p_msg); 18 | virtual bool sendMsg(unsigned int m_msg_code,void *p_msg); 19 | 20 | private: 21 | 22 | std::deque m_queue; 23 | 24 | 25 | CMutex *p_mutex; 26 | CCountingSem *p_sem; 27 | 28 | }; 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | #endif 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /CLinuxCountingSem.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "CLinuxCountingSem.h" 3 | #include 4 | #include 5 | 6 | 7 | 8 | CLinuxCountingSem::CLinuxCountingSem(unsigned int init_sem_count): 9 | CCountingSem() 10 | { 11 | int result; 12 | result = sem_init(&sem, 0, init_sem_count); 13 | 14 | if (result != 0) { 15 | printf("CLinuxCountingSem: error\n"); 16 | 17 | } 18 | 19 | 20 | } 21 | 22 | 23 | CLinuxCountingSem::~CLinuxCountingSem() 24 | { 25 | 26 | 27 | } 28 | 29 | 30 | bool CLinuxCountingSem::Get(Mode mode , unsigned long ) 31 | { 32 | 33 | if(sem_wait(&sem)==0) 34 | return true; 35 | else 36 | return false; 37 | 38 | 39 | } 40 | 41 | 42 | bool CLinuxCountingSem::Post() 43 | { 44 | sem_post(&sem); 45 | return true; 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /TestThread.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "TestThread.h" 5 | #include "COperatingSystemFactory.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | TestThread::TestThread(const char *m_name): 15 | CThread(m_name) 16 | { 17 | 18 | //add your code here 19 | 20 | 21 | 22 | } 23 | 24 | 25 | 26 | TestThread::~TestThread() 27 | { 28 | 29 | } 30 | 31 | void TestThread::mainLoop() 32 | { 33 | unsigned int code; 34 | void *p_msg; 35 | while(1) 36 | { 37 | 38 | p_msg_rev->recvMsg(code, p_msg); 39 | printf("<<<<<<<%s is Running....recv data from message queue: code is : [%d] data is : [%s] \n",p_thread_name,code,(char *)p_msg); 40 | 41 | } 42 | 43 | 44 | } 45 | 46 | 47 | 48 | void TestThread::setMsgQueue(CMsgQueue *q) 49 | { 50 | p_msg_rev=q; 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /TestThreadB.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "TestThreadB.h" 4 | #include "COperatingSystemFactory.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | TestThreadB::TestThreadB(const char *m_name): 14 | CThread(m_name) 15 | { 16 | 17 | //add your code here 18 | 19 | 20 | 21 | 22 | } 23 | 24 | 25 | 26 | TestThreadB::~TestThreadB() 27 | { 28 | 29 | } 30 | 31 | void TestThreadB::mainLoop() 32 | { 33 | unsigned int code=0; 34 | char *p_msg="hello Thread A"; 35 | while(1) 36 | { 37 | printf(">>>>>>>%s is Running....send data to message queue...\n",p_thread_name); 38 | p_msg_send->sendMsg(code, (void *)p_msg); 39 | code++; 40 | p_opration_system->sleepSec(1); 41 | } 42 | 43 | 44 | } 45 | 46 | 47 | 48 | void TestThreadB::setMsgQueue(CMsgQueue *q) 49 | { 50 | p_msg_send=q; 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /COperatingSystem.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _COperatingSystem_H_ 5 | #define _COperatingSystem_H_ 6 | 7 | /******************************************************************************************** 8 | * 9 | *File name: COperatingSystem.h 10 | *Author: Wu Yinghao 11 | *Version: 0.0.1 12 | *Date: 2013.5.20 13 | *E-Mail: wyh817@gmail.com 14 | *Description: this file is base class of OSs 15 | * 16 | ********************************************************************************************/ 17 | 18 | class CThread; 19 | 20 | 21 | class COperatingSystem 22 | { 23 | public: 24 | COperatingSystem(); 25 | ~COperatingSystem(); 26 | 27 | 28 | //create thread 29 | virtual bool createThread(CThread *mThread,unsigned long stack_size=8*1024)=0; 30 | 31 | //system sleep function 32 | virtual void sleepSec(unsigned long sec)=0; 33 | 34 | 35 | protected: 36 | 37 | CThread *p_thread; 38 | 39 | 40 | }; 41 | 42 | 43 | 44 | #endif 45 | 46 | 47 | -------------------------------------------------------------------------------- /COperatingSystemFactory.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "COperatingSystem.h" 3 | #include "CLinuxOperatingSystem.h" 4 | 5 | #include "CCountingSem.h" 6 | #include "CLinuxCountingSem.h" 7 | 8 | 9 | #include "CMutex.h" 10 | #include "CLinuxMutex.h" 11 | 12 | 13 | #include "CMsgQueue.h" 14 | #include "CLinuxMsgQueue.h" 15 | 16 | #include "COperatingSystemFactory.h" 17 | 18 | 19 | 20 | 21 | 22 | COperatingSystem *COperatingSystemFactory::newOperatingSystem() 23 | { 24 | 25 | return new CLinuxOperatingSystem(); 26 | 27 | } 28 | 29 | 30 | CCountingSem *COperatingSystemFactory::newCountingSem(unsigned int init) 31 | { 32 | 33 | return new CLinuxCountingSem(init); 34 | 35 | } 36 | 37 | 38 | CMutex *COperatingSystemFactory::newMutex(const char *pName) 39 | { 40 | return new CLinuxMutex(pName); 41 | } 42 | 43 | 44 | CMsgQueue *COperatingSystemFactory::newMsgQueue(const char *pName) 45 | { 46 | return new CLinuxMsgQueue(pName); 47 | } 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | ARCH = arm 3 | 4 | CROSS_DIR = 5 | CROSS_COMPILE = $(CROSS_DIR) 6 | 7 | CC = $(CROSS_COMPILE)g++ 8 | CCP= $(CROSS_COMPILE)g++ 9 | LD = $(CROSS_COMPILE)ld 10 | STRIP=$(CROSS_COMPILE)strip 11 | AR = $(CROSS_COMPILE)ar 12 | AS = $(CROSS_COMPILE)as 13 | 14 | 15 | SRCS+= \ 16 | main.cpp \ 17 | CThread.cpp \ 18 | COperatingSystem.cpp \ 19 | COperatingSystemFactory.cpp \ 20 | CLinuxOperatingSystem.cpp \ 21 | TestThread.cpp \ 22 | CCountingSem.cpp \ 23 | CLinuxCountingSem.cpp \ 24 | CMutex.cpp \ 25 | CLinuxMutex.cpp \ 26 | CMsgQueue.cpp \ 27 | CLinuxMsgQueue.cpp \ 28 | TestThreadB.cpp 29 | 30 | 31 | 32 | OBJS = $(SRCS:.cpp=.o) 33 | 34 | 35 | 36 | TARGET=operation 37 | 38 | $(TARGET):$(OBJS) 39 | @echo "Makeing $(TARGET)..." 40 | $(CC) $(OBJS) -o $(TARGET) $(LIB) -lpthread 41 | 42 | 43 | %.o:%.cpp 44 | @echo "=======>>Makeing $(CC) -w -c -o $<" 45 | @$(CC) $(CFLAGS) -c $< -w -o $@ 46 | 47 | 48 | clean: 49 | rm -f *.o 50 | 51 | -------------------------------------------------------------------------------- /CCountingSem.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CCountingSem_h_ 4 | #define _CCountingSem_h_ 5 | 6 | /******************************************************************************************** 7 | * 8 | *File name: CCountingSem.h 9 | *Author: Wu Yinghao 10 | *Version: 0.0.1 11 | *Date: 2013.5.20 12 | *E-Mail: wyh817@gmail.com 13 | *Description: this file is base class of CountingSem 14 | * 15 | ********************************************************************************************/ 16 | 17 | class CCountingSem 18 | { 19 | public: 20 | typedef enum { 21 | kTimeout, 22 | kForever 23 | }Mode; 24 | 25 | 26 | CCountingSem(); 27 | ~CCountingSem(); 28 | 29 | //get the sem and sem -1 30 | virtual bool Get(Mode mode = kForever, unsigned long timeoutMS = 0) = 0; 31 | 32 | //post the sem and sem +1 33 | virtual bool Post(void) = 0; 34 | 35 | }; 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | #endif 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /CMutex.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CMutex_H_ 4 | #define _CMutex_H_ 5 | #include 6 | 7 | /******************************************************************************************** 8 | * 9 | *File name: CMutex.h 10 | *Author: Wu Yinghao 11 | *Version: 0.0.1 12 | *Date: 2013.5.20 13 | *E-Mail: wyh817@gmail.com 14 | *Description: this file is base class of CMutexs 15 | * 16 | ********************************************************************************************/ 17 | 18 | 19 | class CMutex 20 | { 21 | 22 | public: 23 | CMutex(const char *pName = NULL); 24 | ~CMutex(); 25 | 26 | 27 | //Lock Mutex 28 | virtual bool Lock()=0; 29 | 30 | //Release Mutex 31 | virtual bool UnLock()=0; 32 | 33 | 34 | const char * getName(void) const { 35 | return mutex_name; 36 | } 37 | 38 | protected: 39 | 40 | char *mutex_name; 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | }; 50 | 51 | 52 | 53 | 54 | 55 | 56 | #endif 57 | 58 | 59 | -------------------------------------------------------------------------------- /COperatingSystemFactory.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _COprationSsytemFactory_h_ 3 | #define _COprationSsytemFactory_h_ 4 | /******************************************************************************************** 5 | * 6 | *File name: COperatingSystemFactory.h 7 | *Author: Wu Yinghao 8 | *Version: 0.0.1 9 | *Date: 2013.5.20 10 | *E-Mail: wyh817@gmail.com 11 | *Description: this file is the simple factory class for building COperratinSystem CMutex etc... 12 | * 13 | ********************************************************************************************/ 14 | #include 15 | 16 | class COperatingSystem; 17 | class CCountingSem; 18 | class CMutex; 19 | class CMsgQueue; 20 | 21 | class COperatingSystemFactory 22 | { 23 | public: 24 | 25 | static COperatingSystem *newOperatingSystem(); 26 | 27 | 28 | static CCountingSem *newCountingSem(unsigned int init); 29 | 30 | static CMutex *newMutex(const char *pName=NULL); 31 | 32 | 33 | static CMsgQueue *newMsgQueue(const char *pName=NULL); 34 | 35 | 36 | }; 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | #endif 51 | 52 | 53 | -------------------------------------------------------------------------------- /CLinuxMutex.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CLinuxMutex_h_ 4 | #define _CLinuxMutex_h_ 5 | 6 | /******************************************************************************************** 7 | * 8 | *File name: CLinuxMutex.h 9 | *Author: Wu Yinghao 10 | *Version: 0.0.1 11 | *Date: 2013.5.20 12 | *E-Mail: wyh817@gmail.com 13 | *Description: this file is linux mutex class which inheritance from CMutex 14 | * 15 | ********************************************************************************************/ 16 | 17 | 18 | 19 | 20 | #include 21 | #include "CMutex.h" 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | 29 | class CLinuxMutex:public CMutex 30 | { 31 | public: 32 | CLinuxMutex(const char *pName=NULL); 33 | ~CLinuxMutex(); 34 | 35 | virtual bool Lock(); 36 | 37 | virtual bool UnLock(); 38 | 39 | private: 40 | 41 | pthread_t m_thread; 42 | 43 | 44 | pthread_mutex_t m_mutex; 45 | 46 | 47 | pthread_mutexattr_t m_mtex_attr; 48 | 49 | }; 50 | 51 | 52 | 53 | 54 | 55 | 56 | #endif 57 | 58 | 59 | -------------------------------------------------------------------------------- /CMsgQueue.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CMsgQueue_H_ 4 | #define _CMsgQueue_H_ 5 | 6 | /******************************************************************************************** 7 | * 8 | *File name: CMsgQueue.h 9 | *Author: Wu Yinghao 10 | *Version: 0.0.1 11 | *Date: 2013.5.20 12 | *E-Mail: wyh817@gmail.com 13 | *Description: this file is base class of msgqueues 14 | * 15 | ********************************************************************************************/ 16 | 17 | 18 | #include 19 | 20 | 21 | typedef struct { 22 | unsigned int msg_code; 23 | void *p_message; 24 | } Elements; 25 | 26 | 27 | 28 | 29 | class CMsgQueue 30 | { 31 | public: 32 | CMsgQueue(const char *pName=NULL); 33 | ~CMsgQueue(); 34 | 35 | 36 | //revice data from message queue 37 | virtual bool recvMsg(unsigned int &m_msg_code,void *&p_msg)=0; 38 | //send data to message queue 39 | virtual bool sendMsg(unsigned int m_msg_code,void *p_msg)=0; 40 | 41 | 42 | 43 | const char * getName(void) const { 44 | return msg_queue_name; 45 | } 46 | 47 | private: 48 | char *msg_queue_name; 49 | 50 | 51 | }; 52 | 53 | 54 | 55 | 56 | 57 | 58 | #endif 59 | 60 | 61 | -------------------------------------------------------------------------------- /CLinuxCountingSem.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _CLinuxCountingSem_h_ 5 | #define _CLinuxCountingSem_h_ 6 | 7 | /******************************************************************************************** 8 | * 9 | *File name: CLinuxCountingSem.h 10 | *Author: Wu Yinghao 11 | *Version: 0.0.1 12 | *Date: 2013.5.20 13 | *E-Mail: wyh817@gmail.com 14 | *Description: this file is linux sem class which inheritance from CCountingSem 15 | * 16 | ********************************************************************************************/ 17 | 18 | #include "CCountingSem.h" 19 | 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | 30 | class CLinuxCountingSem:public CCountingSem 31 | { 32 | public: 33 | CLinuxCountingSem(unsigned int init_sem_count); 34 | ~CLinuxCountingSem(); 35 | 36 | 37 | virtual bool Get(Mode mode = kForever, unsigned long timeoutMS = 0) ; 38 | 39 | virtual bool Post(void) ; 40 | 41 | 42 | 43 | 44 | private: 45 | 46 | sem_t sem; 47 | 48 | 49 | }; 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | #endif 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /CLinuxOperatingSystem.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CLinuxOprationSystem_h_ 4 | #define _CLinuxOprationSystem_h_ 5 | /******************************************************************************************** 6 | * 7 | *File name: CLinuxOperatingSystem.h 8 | *Author: Wu Yinghao 9 | *Version: 0.0.1 10 | *Date: 2013.5.20 11 | *E-Mail: wyh817@gmail.com 12 | *Description: this file is linux OS class which inheritance from COperatingSystem 13 | * 14 | ********************************************************************************************/ 15 | 16 | #include 17 | #include "COperatingSystem.h" 18 | 19 | class CCountingSem; 20 | class CLinuxOperatingSystem : public COperatingSystem 21 | { 22 | public: 23 | 24 | CLinuxOperatingSystem(); 25 | 26 | ~CLinuxOperatingSystem(); 27 | 28 | 29 | 30 | virtual bool createThread(CThread *mThread,unsigned long stack_size=8*1024); 31 | 32 | virtual void sleepSec(unsigned long sec); 33 | 34 | 35 | private: 36 | 37 | //sem_t mSuspendSem; 38 | 39 | 40 | pthread_t mThreadId; 41 | 42 | 43 | pthread_attr_t mThreadAttr; 44 | 45 | 46 | CCountingSem *p_sem; 47 | 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | #endif 55 | 56 | 57 | -------------------------------------------------------------------------------- /CThread.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include "CThread.h" 6 | #include "COperatingSystemFactory.h" 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | CThread::CThread(const char *m_thread_name) 17 | { 18 | p_thread_name=(char *)malloc(sizeof(strlen(m_thread_name))); 19 | strcpy(p_thread_name,m_thread_name); 20 | 21 | 22 | p_opration_system=COperatingSystemFactory::newOperatingSystem(); 23 | 24 | 25 | 26 | if(p_opration_system==NULL) 27 | { 28 | printf(" OS Create Fail...\n"); 29 | exit(0); 30 | } 31 | 32 | 33 | 34 | } 35 | 36 | CThread::~CThread() 37 | { 38 | 39 | 40 | } 41 | 42 | 43 | void CThread::threadEntry(CCountingSem *pSemaphore) 44 | { 45 | 46 | bool res; 47 | 48 | res=initializeThread(); 49 | 50 | if(res) 51 | { 52 | pSemaphore->Post(); 53 | 54 | mainLoop(); 55 | 56 | } 57 | 58 | 59 | 60 | } 61 | 62 | 63 | 64 | 65 | bool CThread::initializeThread() 66 | { 67 | 68 | return true; 69 | 70 | } 71 | 72 | 73 | 74 | 75 | bool CThread::run() 76 | { 77 | if(!p_opration_system->createThread(this)) 78 | { 79 | printf("Create Thread Fail....\n"); 80 | exit(0); 81 | } 82 | 83 | 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /CLinuxMsgQueue.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "CLinuxMsgQueue.h" 4 | #include "COperatingSystemFactory.h" 5 | 6 | 7 | CLinuxMsgQueue::CLinuxMsgQueue(const char *pName): 8 | CMsgQueue(pName) 9 | { 10 | 11 | p_mutex=COperatingSystemFactory::newMutex("Msg Mutex"); 12 | p_sem=COperatingSystemFactory::newCountingSem(0); 13 | 14 | 15 | } 16 | 17 | 18 | 19 | CLinuxMsgQueue::~CLinuxMsgQueue() 20 | { 21 | 22 | } 23 | 24 | 25 | 26 | 27 | 28 | bool CLinuxMsgQueue::recvMsg(unsigned int &m_msg_code,void *&p_msg) 29 | { 30 | 31 | bool result; 32 | Elements queue_element; 33 | 34 | p_sem->Get(); 35 | 36 | p_mutex->Lock(); 37 | 38 | if (m_queue.empty()) { 39 | 40 | p_mutex->UnLock(); 41 | return false; 42 | 43 | } 44 | 45 | 46 | queue_element = m_queue.front(); 47 | m_queue.pop_front(); 48 | 49 | 50 | m_msg_code = queue_element.msg_code; 51 | p_msg = queue_element.p_message; 52 | 53 | p_mutex->UnLock(); 54 | 55 | return true; 56 | 57 | 58 | } 59 | 60 | 61 | 62 | 63 | bool CLinuxMsgQueue::sendMsg(unsigned int m_msg_code,void *p_msg) 64 | { 65 | bool result; 66 | Elements queue_element; 67 | 68 | 69 | 70 | queue_element.msg_code=m_msg_code; 71 | queue_element.p_message=p_msg; 72 | 73 | p_mutex->Lock(); 74 | 75 | m_queue.push_back(queue_element); 76 | 77 | 78 | p_mutex->UnLock(); 79 | 80 | p_sem->Post(); 81 | 82 | 83 | return true; 84 | 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /CLinuxMutex.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include "CLinuxMutex.h" 6 | 7 | 8 | 9 | 10 | 11 | CLinuxMutex::CLinuxMutex(const char *pName): 12 | CMutex(pName) 13 | { 14 | 15 | m_thread = (pthread_t) 0; 16 | 17 | pthread_mutexattr_init(&m_mtex_attr); 18 | if (pthread_mutexattr_settype(&m_mtex_attr, PTHREAD_MUTEX_ERRORCHECK_NP)) 19 | printf("CLinuxMutex::CnlLinuxMutex() : Failed to set mutex attibutes\n"); 20 | 21 | 22 | pthread_mutex_init(&m_mutex, &m_mtex_attr); 23 | 24 | 25 | 26 | } 27 | 28 | 29 | CLinuxMutex::~CLinuxMutex() 30 | { 31 | 32 | } 33 | 34 | bool CLinuxMutex::Lock() 35 | { 36 | 37 | bool locked = false; 38 | int result; 39 | 40 | 41 | result = pthread_mutex_lock(&m_mutex); 42 | 43 | 44 | switch (result) { 45 | case 0: 46 | m_thread = pthread_self(); 47 | 48 | locked = true; 49 | break; 50 | case EDEADLK: 51 | locked = true; 52 | break; 53 | default: 54 | break; 55 | } 56 | 57 | return locked; 58 | 59 | } 60 | 61 | bool CLinuxMutex::UnLock() 62 | { 63 | int result; 64 | bool unlocked = true; 65 | 66 | if (m_thread!= pthread_self()) { 67 | unlocked = false; 68 | printf("CnlLinuxMutex::Unlock() : Error - Mutex not locked or not owned by the thread! \n"); 69 | 70 | } else { 71 | result = pthread_mutex_unlock(&m_mutex); 72 | if(result<0) 73 | unlocked=false; 74 | } 75 | return unlocked; 76 | 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /CThread.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CTHREAD_H_ 4 | #define _CTHREAD_H_ 5 | 6 | /******************************************************************************************** 7 | * 8 | *File name: CThread.h 9 | *Author: Wu Yinghao 10 | *Version: 0.0.1 11 | *Date: 2013.5.20 12 | *E-Mail: wyh817@gmail.com 13 | *Description: this file is base class of threads 14 | * 15 | ********************************************************************************************/ 16 | 17 | #include "COperatingSystem.h" 18 | #include "CCountingSem.h" 19 | 20 | class CThread 21 | { 22 | public: 23 | CThread(const char *m_thread_name); 24 | 25 | ~CThread(); 26 | 27 | //Thread Entry 28 | void threadEntry(CCountingSem *pSemaphore); 29 | 30 | //the thread will be run if you call this function 31 | bool run(); 32 | 33 | 34 | protected: 35 | 36 | /********************************************************* 37 | * 38 | * init the thread informations 39 | * input: none 40 | * output: true if success 41 | * 42 | **********************************************************/ 43 | bool initializeThread(); 44 | 45 | 46 | /********************************************************* 47 | * 48 | * 49 | * 50 | * 51 | * 52 | **********************************************************/ 53 | virtual void mainLoop()=0; 54 | 55 | 56 | 57 | COperatingSystem *p_opration_system; 58 | 59 | char *p_thread_name; 60 | 61 | 62 | 63 | 64 | 65 | 66 | }; 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | #endif 83 | 84 | 85 | 86 | 87 | 88 | 89 | --------------------------------------------------------------------------------