muduo_net程式碼剖析之EventLoop::runInLoop()函式
阿新 • • 發佈:2018-11-26
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);
}
}