linux c 高併發tcp伺服器架構

2021-07-25 20:43:26 字數 3502 閱讀 8052

from:

epoll 接受資料到佇列,執行緒池處理佇列裡的資料

具體實現方式:(只使用使用std的的資料結構,未使用boost)

//thread_pool.cpp

#include

#include

#include "thread_pool.h"

#include "thread_process.h"

#include "command.h"

#include

#include

bool threadpool::bshutdown_ = false;

int threadpool::icurr_thread_num_ = thread_num;

std::vectorthreadpool::command_;

std::mapthreadpool::thread_id_map_;

pthread_mutex_t threadpool::command_mutex_ = pthread_mutex_initializer;

pthread_cond_t threadpool::command_cond_ = pthread_cond_initializer;

void threadpool::initializethreads()

}void* threadpool::process(void* arg)

// 當執行緒不需要退出且沒有需要處理的任務時,需要縮容的則縮容,不需要的則等待訊號

if (0 == command_.size() && !bshutdown_)

}pthread_cond_wait(&command_cond_,&command_mutex_);

}// 執行緒池需要關閉,關閉已有的鎖,執行緒退出

if(bshutdown_)

// 如果執行緒池的最大執行緒數不等於初始執行緒數,則表明需要擴容

if(icurr_thread_num_ < command_.size())

// 從容器中取出待辦任務

std::vector::iterator iter = command_.begin();

command.set_arg(iter->get_arg());

command.set_cmd(iter->get_cmd());

command_.erase(iter);

pthread_mutex_unlock(&command_mutex_);

// 開始業務處理

switch(command.get_cmd())

}return null; // 完全為了消除警告(eclipse編寫的**,警告很煩人)

}void threadpool::addwork(command command)

command_.push_back(command);

pthread_mutex_unlock(&command_mutex_);

if (bsignal)

}void threadpool::threaddestroy(int iwait)

bshutdown_ = true;

pthread_cond_broadcast(&command_cond_);

std::map::iterator iter = thread_id_map_.begin();

for (; iter!=thread_id_map_.end(); ++iter)

pthread_mutex_destroy(&command_mutex_);

pthread_cond_destroy(&command_cond_);

}void threadpool::addthread()

}void threadpool::deletethread()}//

thread_poll.h

#ifndef thread_pool_h_

#define thread_pool_h_

#include

#include

#include "command.h"

#define max_thread_num 50 // 該值目前需要設定為初始執行緒數的整數倍

#define add_factor 4 // 該值表示乙個執行緒可以處理的最大任務數

#define thread_num 10 // 初始執行緒數

class threadpool

;static void initializethreads();

void addwork(command command);

void threaddestroy(int iwait = 2);

private:

static void* process(void* arg);

static void addthread();

static void deletethread();

static bool bshutdown_;

static int icurr_thread_num_;

static std::mapthread_id_map_;

static std::vectorcommand_;

static pthread_mutex_t command_mutex_;

static pthread_cond_t command_cond_;

};#endif /* thread_pool_h_ */

thread_process.cpp:

#include

#include

#include "thread_process.h"

#include

void threadprocess::process0(void* arg)

void threadprocess::process1(void* arg)

void threadprocess::process2(void* arg)

thread_process.h:

#ifndef thread_process_h_

#define thread_process_h_

class threadprocess

;#endif /* thread_process_h_ */ /

command.cpp:

#include

#include "command.h"

int command::get_cmd()

char* command::get_arg()

void command::set_cmd(int cmd)

void command::set_arg(char* arg)

strncpy(arg_,arg,64);

arg_[64] = '\0';}/

command.h:

#ifndef command_h_

#define command_h_

class command

;#endif /* command_h_ */

TCP的高併發伺服器模型

單客戶端單程序,統一accept 原型介紹 此併發伺服器模型並不預先分叉程序,而是主程序統一處理客戶端的連線,當客戶端的請求到達時,才臨時fork 程序,由子程序處理客戶端請求。利用socket 函式建立套接字,呼叫bind 函式繫結位址,呼叫listen 函式來監聽佇列長度,然後進入主處理過程,等...

TCP併發伺服器

int main int recvcnt 0 struct sockaddr in sock server struct sockaddr in sock client int len sizeof struct sockaddr socketfd socket pf inet,sock strea...

Linux C語言 TCP伺服器程式設計

include include include include include include include include include int sockfd 斷開訊號處理函式 void sig handler int signo 伺服器端輸出客戶端的資訊 void out addr stru...