Libevent 使用多线程

来源:互联网 发布:超链接调用js 编辑:程序博客网 时间:2024/05/01 02:43

1 、evthread_use_pthreads();

int CTcpServer::Start()
{
    if(m_bStartFlag)
    {
        printf("repeat start");
        return -2;
    }


    if (NULL == m_pEventBase)
    {
        evthread_use_pthreads();         // 1
        m_pEventBase = event_base_new(); /* 初始化event_base */
        if(NULL  == m_pEventBase)
        {
            printf("m_pEventBase is NULL");
            return -1;
        }
        evthread_make_base_notifiable(m_pEventBase);        //2
    }


2、 evthread_make_base_notifiable(m_pEventBase);


3、void CTcpServer::accept_conn_cb(struct evconnlistener *listener,evutil_socket_t fd, struct sockaddr *address, int socklen)
{
    struct event_base *base = evconnlistener_get_base(listener);
    if(NULL == base)
    {
        printf("base is NULL");
        return ;
    }

    struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE);   //3

//bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE);
中 加上   BEV_OPT_THREADSAFE

0 0
原创粉丝点击