muduo_net代码剖析之EventLoop::runInLoop()函数

版权声明:guojawee https://blog.csdn.net/weixin_36750623/article/details/84447437

runInLoop()函数的有用之处

“EventLoop有一个非常有用的功能:在它的IO线程内执行某个用户任务回调,即EventLoop::runInLoop(const Functor& cb),其中Functor是boost::function<void()>。如果用户在当前IO线程调用这个函数,回调会同步进行;如果用户在其他线程调用runInLoop(),cb会被加入队列,IO线程会被唤醒来调用这个Functor。”

即我们可以在线程间方便地进行任务调配,而且可以在不用锁的情况下保证线程安全。

1、eventfd()唤醒线程

(1) eventfd()函数介绍:创建一个文件描述符efd,用于事件通知
(2) efd可以像普通的文件描述符一样,用epoll_wait进行监听:当epoll_wait检测到efd可读时,说明当前线程被其他线程通知notify
(2) efd的全部缓冲区大小只有定长8byte

先来看看eventfd函数的用法,直接上示例:

#include <stdio.h>  
#include <unistd.h>  
#include <sys/time.h>  
#include <stdint.h>  
#include <pthread.h>  
#include <sys/eventfd.h>  
#include <sys/epoll.h>  

int efd = -1;  

void *read_thread(void *dummy)  
{  
    int ret = 0;  
    uint64_t count = 0;  
    int ep_fd = -1;  
    struct epoll_event events[10];  

    if (efd < 0)  
    {  
        printf("efd not inited.\n");  
        goto fail;  
    }  

    ep_fd = epoll_create(1024);  
    if (ep_fd < 0)  
    {  
        perror("epoll_create fail: ");  
        goto fail;  
    }  

    {  
        struct epoll_event read_event;  

        read_event.events = EPOLLHUP | EPOLLERR | EPOLLIN;  
        read_event.data.fd = efd;  
        
        //将efd添加到epoll中,监听读事件
        ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);  
        if (ret < 0)  
        {  
            perror("epoll ctl failed:");  
            goto fail;  
        }  
    }  

    while (1)  
    {  
        //发生阻塞,直到efd可读事件到来
        ret = epoll_wait(ep_fd, &events[0], 10, 5000);  
        if (ret > 0)  
        {  
            int i = 0;  
            for (; i < ret; i++)  
            {  
                if (events[i].events & EPOLLHUP)  
                {  
                    printf("epoll eventfd has epoll hup.\n");  
                    goto fail;  
                }  
                else if (events[i].events & EPOLLERR)  
                {  
                    printf("epoll eventfd has epoll error.\n");  
                    goto fail;  
                }  
                else if (events[i].events & EPOLLIN)  
                {  
                    int event_fd = events[i].data.fd;  
                    ret = read(event_fd, &count, sizeof(count));  
                    if (ret < 0)  
                    {  
                        perror("read fail:");  
                        goto fail;  
                    }  
                    else  
                    {  
                        struct timeval tv;  

                        gettimeofday(&tv, NULL);  
                        printf("success read from efd, read %d bytes(%llu) at %lds %ldus\n",  
                               ret, count, tv.tv_sec, tv.tv_usec);  
                    }  
                }  
            }  
        }  
        else if (ret == 0)  
        {  
            /* time out */  
            printf("epoll wait timed out.\n");  
            break;  
        }  
        else  
        {  
            perror("epoll wait error:");  
            goto fail;  
        }  
    }  

fail:  
    if (ep_fd >= 0)  
    {  
        close(ep_fd);  
        ep_fd = -1;  
    }  

    return NULL;  
}  

int main(int argc, char *argv[])  
{  
    pthread_t pid = 0;  
    uint64_t count = 0;  
    int ret = 0;  
    int i = 0;  

    efd = eventfd(0, 0); //创建fd用于事件通知  
    if (efd < 0)  
    {  
        perror("eventfd failed.");  
        goto fail;  
    }  

    ret = pthread_create(&pid, NULL, read_thread, NULL);  
    if (ret < 0)  
    {  
        perror("pthread create:");  
        goto fail;  
    }  

    for (i = 0; i < 5; i++)  
    {  
        count = 4;  
        ret = write(efd, &count, sizeof(count));  
        if (ret < 0)  
        {  
            perror("write event fd fail:");  
            goto fail;  
        }  
        else  
        {  
            struct timeval tv;  

            gettimeofday(&tv, NULL);  
            printf("success write to efd, write %d bytes(%llu) at %lds %ldus\n",  
                   ret, count, tv.tv_sec, tv.tv_usec);  
        }  
        sleep(1);  
    }  

fail:  
    if (0 != pid)  
    {  
        pthread_join(pid, NULL);  
        pid = 0;  
    }  

    if (efd >= 0)  
    {  
        close(efd);  
        efd = -1;  
    }  
    return ret;  
}  

输出结果如下所示:

success write to efd, write 8 bytes(4) at 1328805612s 21939us 
success read from efd, read 8 bytes(4) at 1328805612s 21997us 
success write to efd, write 8 bytes(4) at 1328805613s 22247us 
success read from efd, read 8 bytes(4) at 1328805613s 22287us 
success write to efd, write 8 bytes(4) at 1328805614s 22462us 
success read from efd, read 8 bytes(4) at 1328805614s 22503us 
success write to efd, write 8 bytes(4) at 1328805615s 22688us 
success read from efd, read 8 bytes(4) at 1328805615s 22726us 
success write to efd, write 8 bytes(4) at 1328805616s 22973us 
success read from efd, read 8 bytes(4) at 1328805616s 23007us 
epoll wait timed out

上述例子,首先使用eventfd创建描述符efd,并在线程里面使用epoll管理这个描述符efd,当在主线程中write时,线程中的epoll返回,描述符可读。

不难发现,通过eventfd创建的描述符efd,读/写大小为sizeof(uint_64)数据,就可以完成两个线程间的唤醒。比如上述例子,由于epoll_wait()的等待,pthread_create出来的线程阻塞,在主线程中,通过往eventfd中write数据,使描述符可读,epoll返回,这就达到了唤醒的目的。

2、 EventLoop::runInLoop()函数

这个函数的效果就是在loop中执行某个用户回调

void EventLoop::runInLoop(const Functor& cb)
{
  if (isInLoopThread())//如果是在本线程中
  {
    cb();
  }
  else//
  {
    queueInLoop(cb);
  }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36750623/article/details/84447437
今日推荐