/************************************************************************* > File Name : test.cpp > Author : TL Song > EMail : songtianlun@frytea.com > Created Time : Thu 30 Jul 2020 01:34:32 PM CST ************************************************************************/ #include #include #include "include/CMessageQueue.h" #include "include/CTypedef.h" #include "include/CLog.h" #include "include/CMessageQueue.h" using std::cout; using std::endl; void check() { cout << "------------------ Check Message Queue--------------------" << endl; cout << "---- M_MessageQueue :"; cout << "队列空: " << CMessageQueue::Instance()->empty() << " ," << "长度: " << CMessageQueue::Instance()->size() << " ," << "队头: " << CMessageQueue::Instance()->front() << " ," << "队尾: " << CMessageQueue::Instance()->back() << endl; cout << "---- MQT_SYSTEM_STATE_E :"; cout << "队列空: " << CMessageQueue::Instance()->empty(MQT_SYSTEM_STATE_E) << " ," << "长度: " << CMessageQueue::Instance()->size(MQT_SYSTEM_STATE_E) << " ," << "队头: " << CMessageQueue::Instance()->front(MQT_SYSTEM_STATE_E) << " ," << "队尾: " << CMessageQueue::Instance()->back(MQT_SYSTEM_STATE_E) << endl; cout << "---- MQT_PROCESS_STATE_E :"; cout << "队列空: " << CMessageQueue::Instance()->empty(MQT_PROCESS_STATE_E) << " ," << "长度: " << CMessageQueue::Instance()->size(MQT_PROCESS_STATE_E) << " ," << "队头: " << CMessageQueue::Instance()->front(MQT_PROCESS_STATE_E) << " ," << "队尾: " << CMessageQueue::Instance()->back(MQT_PROCESS_STATE_E) << endl; cout << "---- MQT_FILE_STATE_E :"; cout << "队列空: " << CMessageQueue::Instance()->empty(MQT_FILE_STATE_E) << " ," << "长度: " << CMessageQueue::Instance()->size(MQT_FILE_STATE_E) << " ," << "队头: " << CMessageQueue::Instance()->front(MQT_FILE_STATE_E) << " ," << "队尾: " << CMessageQueue::Instance()->back(MQT_FILE_STATE_E) << endl; cout << "---- MQT_FILE_NUM_E :"; cout << "队列空: " << CMessageQueue::Instance()->empty(MQT_FILE_NUM_E) << " ," << "长度: " << CMessageQueue::Instance()->size(MQT_FILE_NUM_E) << " ," << "队头: " << CMessageQueue::Instance()->front(MQT_FILE_NUM_E) << " ," << "队尾: " << CMessageQueue::Instance()->back(MQT_FILE_NUM_E) << endl; cout << "---- MQT_SYSTEM_LOG_E :"; cout << "队列空: " << CMessageQueue::Instance()->empty(MQT_SYSTEM_LOG_E) << " ," << "长度: " << CMessageQueue::Instance()->size(MQT_SYSTEM_LOG_E) << " ," << "队头: " << CMessageQueue::Instance()->front(MQT_SYSTEM_LOG_E) << " ," << "队尾: " << CMessageQueue::Instance()->back(MQT_SYSTEM_LOG_E) << endl; cout << "------------------------- FINISH -------------------------" << endl; } void* testPush(void *args) { int i=0; while(true) { cout << "PushTid1=" << std::this_thread::get_id(); CMessageQueue::Instance()->push(i,MQT_FILE_NUM_E); cout << " -> Push: " << i << ", Size After Push : " << CMessageQueue::Instance()->size(MQT_FILE_NUM_E) << endl; sleep(1); i++; } return NULL; } void* testPop(void *args) { while(!CMessageQueue::Instance()->empty(MQT_FILE_NUM_E)) { cout << "PopTid2" << std::this_thread::get_id(); // cout << " -> Before Pop : " << CMessageQueue::Instance()->front() << endl; CMessageQueue::Instance()->pop(MQT_FILE_NUM_E); // cout << " -> After Pop : " << CMessageQueue::Instance()->front() << endl; cout << " -> Queue Front: " << CMessageQueue::Instance()->front(MQT_FILE_NUM_E) <<", Size After Pop: " << CMessageQueue::Instance()->size(MQT_FILE_NUM_E) << endl; sleep(1); } cout << "Message queue empty, return!" << endl; return NULL; } void clean() { while(!CMessageQueue::Instance()->empty(MQT_FILE_NUM_E)) { CMessageQueue::Instance()->pop(MQT_FILE_NUM_E); } } int main() { cout << "step1: 默认队列入队3元素: 1 2 3" << endl; for(int i=1;i<4;i++) { CMessageQueue::Instance()->push(i); } check(); sleep(1); cout << "step2: MQT_SYSTEM_STATE_E 队列入队3元素: 4 5 6" << endl; for(int i=4;i<7;i++) { CMessageQueue::Instance()->push(i,MQT_SYSTEM_STATE_E); } check(); // sleep(1); cout << "step3: MQT_PROCESS_STATE_E 队列入队3元素: 7 8 9" << endl; for(int i=7;i<10;i++) { CMessageQueue::Instance()->push(i, MQT_PROCESS_STATE_E); } check(); // sleep(1); cout << "step4: MQT_FILE_STATE_E 队列入队3元素: 10 11 12" << endl; for(int i=10;i<13;i++) { CMessageQueue::Instance()->push(i, MQT_FILE_STATE_E); } check(); // sleep(1); cout << "step5: MQT_FILE_NUM_E 队列入队3元素: 13 14 15" << endl; for(int i=13;i<16;i++) { CMessageQueue::Instance()->push(i, MQT_FILE_NUM_E); } check(); // sleep(1); cout << "step6: MQT_SYSTEM_LOG_E 队列入队3元素: 16 17 18" << endl; for(int i=16;i<19;i++) { CMessageQueue::Instance()->push(i, MQT_SYSTEM_LOG_E); } check(); // sleep(1); clean(); cout << "队列已清空!" << endl; cout << "step7: 多线程同时出、入" << endl; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); //设置线程可分离 cout << "Start while() test Push---------------------" << endl; pthread_t ttestPush; pthread_create(&ttestPush, NULL, testPush, NULL); sleep(10); cout << "Start while() test Pop---------------------" << endl; pthread_t ttestPop; pthread_create(&ttestPop, NULL, testPop, NULL); pthread_join(ttestPush,NULL); // 等待线程结束 pthread_join(ttestPop,NULL); // 等待线程结束 return 0; }