linux eventfd 应用案例


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

int evfd;

void * f(void *p)
{
    int ret = 0,j = 100;
    while(ret >= 0)
    {
         uint64_t i = 0;
         fd_set readfds,allfds;
         FD_SET(evfd,&readfds);
         FD_SET(evfd,&allfds);
         int fds = select(evfd + 1,&readfds,0,0,0);
         if(FD_ISSET(evfd,&allfds))
         {
             ret = read(evfd,&i,sizeof(int64_t));
             printf("%d,%d,%llu\n",pthread_self(),ret,i);
         }
    }
    printf("pid=%d exit\n",pthread_self());
}

int main(int argc, char *argv[])
{
    //evfd = eventfd(1000,EFD_NONBLOCK|EFD_CLOEXEC);
    evfd = eventfd(1000,0);
    pthread_t pid1 = 0,pid2 = 0,pid3 = 0;
    pthread_create(&pid1,0,f,0);
    //pthread_create(&pid2,0,f,0);
    //pthread_create(&pid3,0,f,0);
    printf("pid=%d,%d,%d\n",pid1,pid2,pid3);
    sleep(1);
    uint64_t j = 100;
    int ret = write(evfd,&j,sizeof(uint64_t));
    printf("write j=%d,%d\n",j,ret);
    sleep(1);
    j++;
    ret = write(evfd,&j,sizeof(uint64_t));
    printf("write j=%d,%d\n",j,ret);
    //这句注释,得出的结论差异很大.
    //应该多次(连续两次)写,读方一次读,8 字节的内核计数缓冲区写乱了
    //所以,证明write 能触发读操作,但是具体触发几次读操作,还需要读
    //操作自己控制,否则写次数 != 读次数,理论上要完全匹配也很难
    //操作上应该是触发读后,读操作后的动作需要合理的判断结束点
    //sleep(1);
    j++;j++;
    ret = write(evfd,&j,sizeof(uint64_t));
    printf("write j=%d,%d\n",j,ret);
    sleep(1);
    j++;j++;
    ret = write(evfd,&j,sizeof(uint64_t));
    printf("write j=%d,%d\n",j,ret);
    sleep(1);
    close(evfd);
    return 0;

}


输出:


 gcc cc.c -o cc -lpthread;./cc
pid=-1553656064,0,0
-1553656064,8,1000
write j=100,8
-1553656064,8,100
write j=101,8
write j=103,8
-1553656064,8,204
write j=105,8
-1553656064,8,105


这里,连续写了两次,读了一次,导致缓冲区乱了。

write j=101,8
write j=103,8
-1553656064,8,204



你可能感兴趣的:(C++,技术)