eventfd

static int fd;
static uint64_t buffer;

static void threadFunc(void)   //线程函数
{
	int t;
	eventfd_t value;
	int ret = -1;
	while(1)
	{
	
#if 0 
	     t = read(fd,&buffer,sizeof(buffer));       //阻塞等待fd可读,及通知事件发生
	     if(sizeof(buffer) < 8)
	     {
	        printf("buffer错误\n");
	     }
	     printf("t = %llu   buffer = %llu\n",t,buffer);
	     if(t == 8)
	     {
	        printf("唤醒成功\n");
	     }
#endif  
		//eventfd_read(int fd, eventfd_t value);
		///ret = eventfd_read(fd,value);
		//printf("ret is %d\n",ret);
		//printf("value is %d\n",value);
		t = eventfd_read(fd,&value);
		printf("t is %d, value is %d\n",t,value);
		
	}    
}

int main(int argc, char * argv [ ])
{
    uint64_t buf = 1;
    int ret;
	eventfd_t value;
    pthread_t tid;
    if((fd = eventfd(0,0)) == -1)   //创建事件驱动的文件描述符
    {
        printf("创建失败\n");
    }
 
    //创建线程
    if(pthread_create(&tid,NULL,threadFunc,NULL) < 0)
    {
        printf("线程创建失败\n");
    }
 
    while(1)
    {
    
#if 0
	    ret = write(fd,&buf,sizeof(buf));  //通过往fd里写东西来进行事件通知
	    if(ret != 8)
	    {
	        printf("写错误\n");
	    }
		else 
		{
			printf("ret  is 11111 0x%d\n",ret);
		}
		
		printf("ret is 0x%d\n",ret);
	    sleep(2);   //没2s通知一次 
#endif 

#if 1 
		//int eventfd_write(int fd, eventfd_t value);
		value = 1 ;
		ret = eventfd_write(10,value);  // 这个success return 0 不是sizeof(eventfd_t) failure -1 
		printf("ret is eventfd write %d\n",ret);
		sleep(2);
#endif 
		
		
    }
 
    return 0;
}

  

posted @ 2020-12-11 13:37  卷哭你  阅读(252)  评论(0编辑  收藏  举报