线程间通信之eventfd

线程间通信之eventfd

man手册中的解释:
eventfd()创建了一个“eventfd对象”, 通过它能够实现用户态程序间(我觉得这里主要指线程而非进程)的等待/通知机制,以及内核态向用户态通知的机制(未考证)。
此对象包含了一个被内核所维护的计数(uint64_t), 初始值由initval来决定。


int eventfd(unsigned int initval, int flags);创建一个eventfd文件描述符
int eventfd_read(int fd, eventfd_t *value); 向eventfd中写入一个值
int eventfd_write(int fd, eventfd_t value); 从eventfd中读出一个值

例一、子线程多次写入多个值,主线程一次读出所有值的和

复制代码

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include   
 6 
 7 int main(int argc, char**argv[])
 8 {
 9     int efd, j;
10     uint64_t u;
11     ssize_t s;
12     
13     if (argc < 2)
14     {
15         printf("number of argc is wrong!\n");
16         return 0;
17     }
18     
19     efd = eventfd(0,0);
20     if (-1 == efd)
21     {
22         printf("failed to create eventfd\n");
23     }
24     
25     switch(fork())
26     {
27         case 0:
28         {
29             for(j=1; j 
  

复制代码

 

例二、eventfd可以被epoll监控, 一旦有状态变化,可以触发通知

复制代码

  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include   
  6 #include   
  7 #include   
  8 #include   
  9 
 10 int g_iEvtfd = -1;
 11 
 12 void *eventfd_child_Task(void *pArg)
 13 {
 14     uint64_t uiWrite = 1;
 15     
 16     while(1)
 17     {
 18         sleep(2);
 19         if (0 != eventfd_write(g_iEvtfd, uiWrite))
 20         {
 21             printf("child write iEvtfd failed\n");
 22         }    
 23     }
 24 
 25     return;
 26 }
 27 
 28 int main(int argc, char**argv[])
 29 {
 30     int iEvtfd, j;
 31     uint64_t uiWrite = 1;
 32     uint64_t uiRead;
 33     ssize_t s;
 34     int iEpfd;
 35     struct epoll_event stEvent;
 36     int iRet = 0;
 37     struct epoll_event stEpEvent;
 38     pthread_t stWthread;
 39     
 40     iEpfd = epoll_create(1);
 41     if (-1 == iEpfd)
 42     {
 43         printf("Create epoll failed.\n");
 44         return 0;
 45     }
 46     
 47     iEvtfd = eventfd(0,0);
 48     if (-1 == iEvtfd)
 49     {
 50         printf("failed to create eventfd\n");
 51         return 0;
 52     }
 53     
 54     g_iEvtfd = iEvtfd;
 55     
 56     memset(&stEvent, 0, sizeof(struct epoll_event));
 57     stEvent.events = (unsigned long) EPOLLIN;
 58     stEvent.data.fd = iEvtfd;
 59     iRet = epoll_ctl(iEpfd, EPOLL_CTL_ADD, g_iEvtfd, &stEvent);
 60     if (0 != iRet)
 61     {
 62         printf("failed to add iEvtfd to epoll\n");
 63         close(g_iEvtfd);
 64         close(iEpfd);
 65         return 0;
 66     }
 67     
 68     iRet = pthread_create(&stWthread, NULL, eventfd_child_Task, NULL);
 69     if (0 != iRet)
 70     {
 71         close(g_iEvtfd);
 72         close(iEpfd);
 73         return;
 74     }
 75     
 76     for(;;)
 77     {
 78         iRet = epoll_wait(iEpfd, &stEpEvent, 1, -1);
 79         if (iRet > 0)
 80         {
 81             s = eventfd_read(iEvtfd, &uiRead);
 82             if (s != 0)
 83             {
 84                 printf("read iEvtfd failed\n");
 85                 break;
 86             }
 87             printf("Read %llu (0x%llx) from iEvtfd\n", uiRead, uiRead);
 88         }
 89     }
 90     
 91     close(g_iEvtfd);
 92     close(iEpfd);
 93     return 0;
 94 }
 95 运行结果
 96 kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out
 97 Read 1 (0x1) from iEvtfd
 98 Read 1 (0x1) from iEvtfd
 99 Read 1 (0x1) from iEvtfd
100 Read 1 (0x1) from iEvtfd
101 Read 1 (0x1) from iEvtfd
102 Read 1 (0x1) from iEvtfd
103 Read 1 (0x1) from iEvtfd
104 Read 1 (0x1) from iEvtfd
105 ^C

复制代码

例三、被epoll监控的eventfd,如果在子线程中被多次写入,在主线程中是怎么读的?

复制代码

#include 
#include 
#include 
#include 
#include   
#include   
#include   
#include   

int g_iEvtfd = -1;

void *eventfd_child_Task(void *pArg)
{
    uint64_t uiWrite = 1;
    
        while(1)
        {
            sleep(2);
            eventfd_write(g_iEvtfd, uiWrite);
            eventfd_write(g_iEvtfd, uiWrite);
        }

    return;
}

int main(int argc, char**argv[])
{
    int iEvtfd, j;
    uint64_t uiWrite = 1;
    uint64_t uiRead;
    ssize_t s;
    int iEpfd;
    struct epoll_event stEvent;
    int iRet = 0;
    struct epoll_event stEpEvent;
    pthread_t stWthread;
    
    iEpfd = epoll_create(1);
    if (-1 == iEpfd)
    {
        printf("Create epoll failed.\n");
        return 0;
    }
    
    iEvtfd = eventfd(0,0);
    if (-1 == iEvtfd)
    {
        printf("failed to create eventfd\n");
        return 0;
    }
    
    g_iEvtfd = iEvtfd;
    
    memset(&stEvent, 0, sizeof(struct epoll_event));
    stEvent.events = (unsigned long) EPOLLIN;
    stEvent.data.fd = iEvtfd;
    iRet = epoll_ctl(iEpfd, EPOLL_CTL_ADD, g_iEvtfd, &stEvent);
    if (0 != iRet)
    {
        printf("failed to add iEvtfd to epoll\n");
        close(g_iEvtfd);
        close(iEpfd);
        return 0;
    }
    
    iRet = pthread_create(&stWthread, NULL, eventfd_child_Task, NULL);
    if (0 != iRet)
    {
        close(g_iEvtfd);
        close(iEpfd);
        return;
    }
    
    for(;;)
    {
        iRet = epoll_wait(iEpfd, &stEpEvent, 1, -1);
        if (iRet > 0)
        {
            s = eventfd_read(iEvtfd, &uiRead);
            if (s != 0)
            {
                printf("read iEvtfd failed\n");
                break;
            }
            printf("Read %llu (0x%llx) from iEvtfd\n", uiRead, uiRead);
        }
    }
    
    close(g_iEvtfd);
    close(iEpfd);
    return 0;
}

运行结果:
kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out
Read 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfd

Read 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfd

Read 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfd

Read 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfd

^C

复制代码

例一中并没有epoll做监控,
因此在read前,如果eventfd被写多次,在read的时候也是一次全部读出。

 

注:eventfd中的SEMAPHORE标志用法

* If EFD_SEMAPHORE was not specified and the eventfd counter
has a nonzero value, then a read(2) returns 8 bytes contain‐
ing that value, and the counter's value is reset to zero.

* If EFD_SEMAPHORE was specified and the eventfd counter has a
nonzero value, then a read(2) returns 8 bytes containing the
value 1, and the counter's value is decremented by 1.

通过测试发现。如果eventfd在创建的时候传入EFD_SEMAPHORE 标志,则会按上面man手册中提到的那样,每次在eventfd_read的时候只减一,并不是把值一次性全部读出。见下例 :

你可能感兴趣的:(eventfd)