当前位置: 首页 > wzjs >正文

门窗营销型网站百家号官网

门窗营销型网站,百家号官网,台州h5建站,pexels免费素材网目标 实现一个服务器程序,能够同时和多个客户端建立连接。 使用线程池,使用可以复用的线程,避免创建和销毁线程时的资源浪费,优化连接速度。 线程池实现 原理 线程池会提前创建出几个工作线程, 需要执行的任务&am…

目标

实现一个服务器程序,能够同时和多个客户端建立连接。
使用线程池,使用可以复用的线程,避免创建和销毁线程时的资源浪费,优化连接速度。

线程池实现

原理

线程池会提前创建出几个工作线程

需要执行的任务,会被放入线程池的任务队列

这几个工作线程会等待条件唤醒(pthread_cond),一旦任务队列中存在任务,那么就会唤醒,同时执行任务队列中元素的函数指针。

另外根据工作负载的不同,线程池会动态的添加会减小线程数量以应对不同情况(为了简化,本篇文章不涉及动态添加和销毁线程)。

实现

  • 数据结构
typedef struct ThreadPool{int max;int min;Task* TaskQueue;int queueFront;int queueRear;int queueSize;int queueCap;pthread_t* ThreadID;pthread_t manager_ThreadID;pthread_mutex_t threadpool_mutex;pthread_cond_t notFull;pthread_cond_t notEmpty;}ThreadPool;
  • 任务队列
    任务队列实际上是一个环形队列,队列的每个元素包括要执行的函数指针和传入函数的参数指针
void queueAdd(ThreadPool* threadpool,Task* task){pthread_mutex_lock(&threadpool->threadpool_mutex);if(threadpool->queueSize == threadpool->queueCap){pthread_cond_wait(&threadpool->notFull,&threadpool->threadpool_mutex);}threadpool->TaskQueue[threadpool->queueRear].function = task->function;threadpool->TaskQueue[threadpool->queueRear].arg = task->arg;threadpool->queueRear =  (threadpool->queueRear + 1)%threadpool->queueCap;threadpool->queueSize++;pthread_mutex_unlock(&threadpool->threadpool_mutex);pthread_cond_signal(&threadpool->notEmpty);
}
  • 工作线程
void* worker(void* arg){ThreadPool* threadpool = (ThreadPool*)arg;while(1){Task task;pthread_mutex_lock(&threadpool->threadpool_mutex);while(threadpool->queueSize == 0){pthread_cond_wait(&threadpool->notEmpty,&threadpool->threadpool_mutex);}task.function = threadpool->TaskQueue[threadpool->queueFront].function;task.arg = threadpool->TaskQueue[threadpool->queueFront].arg;threadpool->queueFront = (threadpool->queueFront + 1)%threadpool->queueCap;threadpool->queueSize--;pthread_mutex_unlock(&threadpool->threadpool_mutex);task.function(task.arg);pthread_cond_signal(&threadpool->notFull);}return NULL;
}

socket服务端实现

流程:

  • 创建套接字
  • 配置属性
  • 绑定端口和ip
  • 监听端口
  • 等待连接
  • read/write

初始化

void server_init(Socket_Info* info){info->lfd=socket(AF_INET, SOCK_STREAM, 0);if(info->lfd == -1){perror("socket");exit(0);}info->sock = (struct sockaddr*)malloc(sizeof(struct sockaddr_in));info->sock->sin_family = AF_INET;info->sock->sin_port = htons(10003);   // 大端端坣info->sock->sin_addr.s_addr = INADDR_ANY;int ret = bind(info->lfd, info->sock, sizeof(struct sockaddr_in));if(ret == -1){perror("bind");exit(0);}// 3. 设置监坬ret = listen(info->lfd, LISTEN_NUM);if(ret == -1){perror("listen");exit(0);}
}

连接和处理

    while(1){SubSocketInfo* subinfo;for(int i=0; i<MAX_CONNECT; ++i){if(infos[i].cfd == -1){subinfo = &infos[i];}}int cfd = accept(socket->lfd, (struct sockaddr*)&subinfo->addr, &clilen);subinfo->cfd = cfd;if(cfd == -1){perror("accept");break;}Task task;task.arg = subinfo;task.function = server_progress;queueAdd(pool,&task);}

void* server_progress(void* arg){SubSocketInfo* infos_ptr = (SubSocketInfo*)arg;char ip[24] = {0};printf("thread id: %ld 客户端的IP地址: %s, 端口: %d\n",pthread_self(),inet_ntop(AF_INET, &infos_ptr->addr.sin_addr.s_addr, ip, sizeof(ip)),ntohs(infos_ptr->addr.sin_port));while(1){char buf[1024];memset(buf, 0, sizeof(buf));int len = read(infos_ptr->cfd, buf, sizeof(buf));if(len > 0){printf("thread id: %ld 客户端say: %s\n",pthread_self(), buf);write(infos_ptr->cfd, buf, len);}else if(len  == 0){printf("客户端断开了连接...\n");break;}else{perror("read");break;}}close(infos_ptr->cfd);infos_ptr->cfd = -1;
}

配合实现

每次建立连接后,将服务端的处理函数和参数添加进线程池的任务队列中即可完成最简易的基于线程池的并发socket服务端函数。

http://www.dtcms.com/wzjs/122031.html

相关文章:

  • 张家港营销型网站建设seo公司上海牛巨微
  • 三里屯网站建设公司外包公司
  • 建站网站那个好营销企业
  • 网站互动方式百度竞价推广出价技巧
  • 南昌电影网站开发百度指数如何分析数据
  • 织梦系统网站打开速度慢百度网盘搜索引擎网站
  • 企业网站自己可以做nba最新比赛直播
  • 厦门网站推广台湾搜索引擎
  • 临沂市建设局网站勘察设计制作网站教学
  • 怎么把自己做的网站弄到域名上百度怎么推广自己的网站
  • 巨鹿建设银行网站首页推广途径有哪些
  • wamp做的网站上传最新网络营销方式有哪些
  • 烟台建站程序国内it培训机构排名
  • 学信网 的企业网站给你做认证友链出售
  • 丹东有做公司网站的吗百度百度一下你就知道
  • 淮安新网站制作谷歌浏览器下载安装2023最新版
  • 网站建设公司南宁在线磁力搜索引擎
  • 东莞东坑网站建设宣传方式
  • 漳州手机网站开发竞价推广是什么工作
  • 网站跟信息推广有哪些信息化建设无代码免费web开发平台
  • 无锡网站优化建站一媒体app软件下载老版本
  • 使用动易模版制作网站网站建站在线制作
  • 无网站网络营销西安网站制作工作室
  • 北京住房和建设城乡委员会网站google官网入口手机版
  • 烟台优化网站网络营销推广策划书
  • 宁波网站设计公司济南搜索引擎优化网站
  • 在小说网站做责编有没有免费的写文案的软件
  • 巴顿品牌设计公司seo关键技术有哪些
  • 重庆市城乡建设委员会门户网站电商代运营公司十强
  • 长春长春网站建设网搜索排行榜