linux c++ 高并发tcp服务器架构

来源:互联网 发布:淘宝店铺招牌是什么 编辑:程序博客网 时间:2024/05/18 23:57


 本文转载自博客:http://blog.csdn.net/opencpu/article/details/47175813


epoll 接受数据到队列,线程池处理队列里的数据
具体实现方式:(只使用使用std的的数据结构,未使用boost)


[cpp] view plain copy
  1. //============================================================================  
  2. // Name        : hmserver.cpp  
  3. // Author      : song  
  4. // Version     :  
  5. // Copyright   : Your copyright notice  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8. #include <stdio.h>  
  9. #include <iostream>  
  10. using namespace std;  
  11.   
  12. #include "thread_pool.h"  
  13. #include "command.h"  
  14.   
  15. int main()  
  16. {  
  17.     ThreadPool thread_pool;  
  18.     thread_pool.InitializeThreads();  
  19.     Command command;  
  20.     char arg[8] = {0};  
  21.     for(int i=1; i<=200; ++i)  
  22.     {  
  23.         command.set_cmd(i%3);  
  24.         sprintf(arg,"%d",i);  
  25.         command.set_arg(arg);  
  26.         thread_pool.AddWork(command);  
  27.     }  
  28.     sleep(30); // 用于测试线程池缩容  
  29.     thread_pool.ThreadDestroy();  
  30.     return 0;  
  31. }  

thread_pool.cpp
[cpp] view plain copy
  1. #include <pthread.h>  
  2. #include <stdlib.h>  
  3. #include "thread_pool.h"  
  4. #include "thread_process.h"  
  5. #include "command.h"  
  6. #include <unistd.h>  
  7. #include <stdio.h>  
  8.   
  9. bool ThreadPool::bshutdown_ = false;  
  10. int ThreadPool::icurr_thread_num_ = THREAD_NUM;  
  11. std::vector<Command> ThreadPool::command_;  
  12. std::map<pthread_t,int> ThreadPool::thread_id_map_;  
  13. pthread_mutex_t ThreadPool::command_mutex_ = PTHREAD_MUTEX_INITIALIZER;  
  14. pthread_cond_t ThreadPool::command_cond_ = PTHREAD_COND_INITIALIZER;  
  15.   
  16. void ThreadPool::InitializeThreads()  
  17. {  
  18.     for (int i = 0; i < THREAD_NUM ; ++i)  
  19.     {  
  20.         pthread_t tempThread;  
  21.         pthread_create(&tempThread, NULL, ThreadPool::Process, NULL);  
  22.         thread_id_map_[tempThread] = 0;  
  23.     }  
  24. }  
  25.   
  26. void* ThreadPool::Process(void* arg)  
  27. {  
  28.     ThreadProcess threadprocess;  
  29.     Command command;  
  30.     while (true)  
  31.     {  
  32.         pthread_mutex_lock(&command_mutex_);  
  33.         // 如果线程需要退出,则此时退出  
  34.         if (1 == thread_id_map_[pthread_self()])  
  35.         {  
  36.             pthread_mutex_unlock(&command_mutex_);  
  37.             printf("thread %u will exit\n", pthread_self());  
  38.             pthread_exit(NULL);  
  39.         }  
  40.         // 当线程不需要退出且没有需要处理的任务时,需要缩容的则缩容,不需要的则等待信号  
  41.         if (0 == command_.size() && !bshutdown_)  
  42.         {  
  43.             if(icurr_thread_num_ >  THREAD_NUM)  
  44.             {  
  45.                 DeleteThread();  
  46.                 if (1 == thread_id_map_[pthread_self()])  
  47.                 {  
  48.                     pthread_mutex_unlock(&command_mutex_);  
  49.                     printf("thread %u will exit\n", pthread_self());  
  50.                     pthread_exit(NULL);  
  51.                 }  
  52.             }  
  53.             pthread_cond_wait(&command_cond_,&command_mutex_);  
  54.         }  
  55.         // 线程池需要关闭,关闭已有的锁,线程退出  
  56.         if(bshutdown_)  
  57.         {  
  58.             pthread_mutex_unlock (&command_mutex_);  
  59.             printf ("thread %u will exit\n", pthread_self ());  
  60.             pthread_exit (NULL);  
  61.         }  
  62.         // 如果线程池的最大线程数不等于初始线程数,则表明需要扩容  
  63.         if(icurr_thread_num_ < command_.size())  
  64.         {  
  65.             AddThread();  
  66.         }  
  67.         // 从容器中取出待办任务  
  68.         std::vector<Command>::iterator iter = command_.begin();  
  69.         command.set_arg(iter->get_arg());  
  70.         command.set_cmd(iter->get_cmd());  
  71.         command_.erase(iter);  
  72.         pthread_mutex_unlock(&command_mutex_);  
  73.         // 开始业务处理  
  74.         switch(command.get_cmd())  
  75.         {  
  76.         case 0:  
  77.             threadprocess.Process0(command.get_arg());  
  78.             break;  
  79.         case 1:  
  80.             threadprocess.Process1(command.get_arg());  
  81.             break;  
  82.         case 2:  
  83.             threadprocess.Process2(command.get_arg());  
  84.             break;  
  85.         default:  
  86.             break;  
  87.         }  
  88.     }  
  89.     return NULL; // 完全为了消除警告(eclipse编写的代码,警告很烦人)  
  90. }  
  91.   
  92. void ThreadPool::AddWork(Command command)  
  93. {  
  94.     bool bsignal = false;  
  95.     pthread_mutex_lock(&command_mutex_);  
  96.     if (0 == command_.size())  
  97.     {  
  98.         bsignal = true;  
  99.     }  
  100.     command_.push_back(command);  
  101.     pthread_mutex_unlock(&command_mutex_);  
  102.     if (bsignal)  
  103.     {  
  104.         pthread_cond_signal(&command_cond_);  
  105.     }  
  106. }  
  107.   
  108. void ThreadPool::ThreadDestroy(int iwait)  
  109. {  
  110.     while(0 != command_.size())  
  111.     {  
  112.         sleep(abs(iwait));  
  113.     }  
  114.     bshutdown_ = true;  
  115.     pthread_cond_broadcast(&command_cond_);  
  116.     std::map<pthread_t,int>::iterator iter = thread_id_map_.begin();  
  117.     for (; iter!=thread_id_map_.end(); ++iter)  
  118.     {  
  119.         pthread_join(iter->first,NULL);  
  120.     }  
  121.     pthread_mutex_destroy(&command_mutex_);  
  122.     pthread_cond_destroy(&command_cond_);  
  123. }  
  124.   
  125. void ThreadPool::AddThread()  
  126. {  
  127.     if(((icurr_thread_num_*ADD_FACTOR) < command_.size())  
  128.             && (MAX_THREAD_NUM != icurr_thread_num_))  
  129.     {  
  130.         InitializeThreads();  
  131.         icurr_thread_num_ += THREAD_NUM;  
  132.     }  
  133. }  
  134.   
  135. void ThreadPool::DeleteThread()  
  136. {  
  137.     int size = icurr_thread_num_ - THREAD_NUM;  
  138.     std::map<pthread_t,int>::iterator iter = thread_id_map_.begin();  
  139.     for(int i=0; i<size; ++i,++iter)  
  140.     {  
  141.         iter->second = 1;  
  142.     }  
  143. }  

thread_poll.h
[cpp] view plain copy
  1. #ifndef THREAD_POOL_H_  
  2. #define THREAD_POOL_H_  
  3. #include <map>  
  4. #include <vector>  
  5. #include "command.h"  
  6.   
  7. #define MAX_THREAD_NUM 50 // 该值目前需要设定为初始线程数的整数倍  
  8. #define ADD_FACTOR 4 // 该值表示一个线程可以处理的最大任务数  
  9. #define THREAD_NUM 10 // 初始线程数  
  10.   
  11. class ThreadPool  
  12. {  
  13. public:  
  14.     ThreadPool() {};  
  15.     static void InitializeThreads();  
  16.     void AddWork(Command command);  
  17.     void ThreadDestroy(int iwait = 2);  
  18. private:  
  19.     static void* Process(void* arg);  
  20.     static void AddThread();  
  21.     static void DeleteThread();  
  22.     static bool bshutdown_;  
  23.     static int icurr_thread_num_;  
  24.     static std::map<pthread_t,int> thread_id_map_;  
  25.     static std::vector<Command> command_;  
  26.     static pthread_mutex_t command_mutex_;  
  27.     static pthread_cond_t command_cond_;  
  28. };  
  29.   
  30.   
  31. #endif /* THREAD_POOL_H_ */  

thread_process.cpp
[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include "thread_process.h"  
  4. #include <pthread.h>  
  5.   
  6. void ThreadProcess::Process0(void* arg)  
  7. {  
  8.     printf("thread %u is starting process %s\n",pthread_self(),arg);  
  9.     usleep(100*1000);  
  10. }  
  11. void ThreadProcess::Process1(void* arg)  
  12. {  
  13.     printf("thread %u is starting process %s\n",pthread_self(),arg);  
  14.     usleep(100*1000);  
  15. }  
  16.   
  17. void ThreadProcess::Process2(void* arg)  
  18. {  
  19.     printf("thread %u is starting process %s\n",pthread_self(),arg);  
  20.     usleep(100*1000);  
  21. }  
thread_process.h
[cpp] view plain copy
  1. #ifndef THREAD_PROCESS_H_  
  2. #define THREAD_PROCESS_H_  
  3.   
  4. class ThreadProcess  
  5. {  
  6. public:  
  7.     void Process0(void* arg);  
  8.     void Process1(void* arg);  
  9.     void Process2(void* arg);  
  10. };  
  11.   
  12. #endif /* THREAD_PROCESS_H_ */  

command.cpp
[cpp] view plain copy
  1. #include <string.h>  
  2. #include "command.h"  
  3.   
  4.   
  5. int Command::get_cmd()  
  6. {  
  7.     return cmd_;  
  8. }  
  9.   
  10. char* Command::get_arg()  
  11. {  
  12.     return arg_;  
  13. }  
  14.   
  15. void Command::set_cmd(int cmd)  
  16. {  
  17.     cmd_ = cmd;  
  18. }  
  19.   
  20. void Command::set_arg(char* arg)  
  21. {  
  22.     if(NULL == arg)  
  23.     {  
  24.         return;  
  25.     }  
  26.     strncpy(arg_,arg,64);  
  27.     arg_[64] = '\0';  
  28. }  

command.h
[cpp] view plain copy
  1. #ifndef COMMAND_H_  
  2. #define COMMAND_H_  
  3.   
  4. class Command  
  5. {  
  6. public:  
  7.     int get_cmd();  
  8.     char* get_arg();  
  9.     void set_cmd(int cmd);  
  10.     void set_arg(char* arg);  
  11. private:  
  12.     int cmd_;  
  13.     char arg_[65];  
  14. };  
  15.   
  16. #endif /* COMMAND_H_ */  

0 0
原创粉丝点击