多线程中使用libevent

这篇博客探讨了在多线程环境中如何使用libevent库,提到了关键API如evthread_use_pthreads()用于线程配置,以及evbuffer_enable_locking()用于锁定bufferevent及其关联的evbuffers,确保线程安全。示例代码展示了如何使用gcc编译并链接libevent_core、pthread和libevent_pthreads库。
摘要由CSDN通过智能技术生成

涉及到的API:


1) evthread_use_pthreads()

2)

 struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);

void bufferevent_lock(struct bufferevent *bufev);
void bufferevent_unlock(struct bufferevent *bufev);

Locking the bufferevent with this function will lock its associated evbuffersas well.

3)

evbuffer_enable_locking(bev->output,null)

evbuffer_enable_locking(bev->input,null)

void evbuffer_lock(struct evbuffer *buf);
void evbuffer_unlock(struct evbuffer *buf);

/* epthreads.c */
#include <stdlib.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/thread.h>
#include <event2/util.h>
#include <pthread.h>

int main(int argc, const char *argv[])
{
  
    struct event_base *base = event_base_new();
    int options = 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值