当前位置:网站首页>libevent与多线程
libevent与多线程
2022-07-24 05:16:00 【lifei_0001】
首先在进程中,libevent的一个base实际上就是一个while循环,当系统内核捕捉到事件发生时,它就会按照事件的优先级将这些捕捉到的事件,用注册时的回调函数处理。怎样和多线程一起用呢?我认为有两个解决方案。
一、将监听accept和连接connect注册到一个base上,当有事件触发时,开新的线程处理接受到的数据。
二、使用1+N个线程,每个线程一个base。
1个监听线程,他只负责监听连接。有新的客户端连接进来时,将连接的socket交个某个线程处理。这里可以采用轮询策略。
N个处理客户端业务的线程,每个线程只负责自己的客户端时间。线程间互不干扰。每个线程都应该有一对管道,业务线程监听读时间,监听线程则在有新的客户端连接时,将socket写到某个业务线程的队列里,并向该线程的管道写操作,以触发该线程的管道多事件,进而线程知道有个新的客户端需要自己服务,从队列里取出socket,并监听读事件。
边栏推荐
- C语言从入门到入土——操作符超详细总结
- 【STL】Map &unordered_map
- Problems encountered in configuring Yum source
- T 11-20
- 统计学之样本和总体的关系: 样本成功比例+中心极限定理(样本均值)
- C语言进阶篇 五.动态内存管理
- Create and delete databases using databases
- gdb调试core/dump
- 1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
- What are the core strengths of a knowledge base that supports customers quickly?
猜你喜欢

C语言进阶篇 一.数据的存储
![Embedded system transplantation [3] - uboot burning and use](/img/36/69daec5f1fe41bd3d0433d60816bba.png)
Embedded system transplantation [3] - uboot burning and use

C语言入门篇 一.分支和循环语句

A collation of the basic usage of numpy

Handwritten ORM framework

JDBC encapsulates a parent class to reduce code duplication

关于numpy基础用法的一个整理

Skills of BeanShell dealing with JSON

How can NFT, whose stars enter the market against the market, get out of the independent market?

【深度学习】(三)图像分类
随机推荐
C语言入门篇 五.初识指针 六.初识结构体
Web development
MySQL深入了解
【【【递归】】】
C语言进阶篇 七.程序的编译和预处理
【sklearn】tree.DecisionTreeClassifier
[deep learning] (III) image classification
【sklearn】PCA
1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
VS 调试
PPPoE gateway simulation environment setup
AiN 0722 签到
反射的介绍
Integration of SSM
This is the first article
CentOS 7安装Mysql5.6.37
Execution sequence of finally and return
九大排序实现与比较(万字总结)
generator生成器,只生成两个方法
MySQL 远程连接错误解决方法