boost线程池

</pre><p style="margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 14px; line-height: 25px; padding-top: 0px; padding-bottom: 0px; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;"><a target=_blank href="http://www.cnblogs.com/toosuo/archive/2011/12/13/2286219.html">http://www.cnblogs.com/toosuo/archive/2011/12/13/2286219.html</a></p><p style="margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 14px; line-height: 25px; padding-top: 0px; padding-bottom: 0px; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;"></p><p style="margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 14px; line-height: 25px; padding-top: 0px; padding-bottom: 0px; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;">使用io_service作为处理工作的work pool,可以看到,就是通过io_service.post投递一个Handler到io_service的队列,Handler在这个io_service.run内部得到执行,有可能你会发现,io_services.dispatch的接口也和io_service.post一样,但不同的是它是直接调用而不是经过push到队列然后在io_services.run中执行,而在这个示例当中,显然我们需要把工作交到另一个线程去完成,这样才不会影响网络接收线程池的工作以达到高效率的接收数据,这种设计与前面的<span style="font-family: monospace; padding: 0px; margin: 0px; line-height: normal;">netsever其实相同,这就是典型的Half Sync/Half Async。二者的区别就是</span><span style="font-family: monospace; padding: 0px; margin: 0px; line-height: normal;">netsever自己实现了工作队列,而不是直接使用io_service,</span><span style="font-family: monospace; padding: 0px; margin: 0px; line-height: normal;">这种设计实际上在win下是使用了iocp作为工作队列。</span></p><p style="margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 14px; line-height: 25px; padding-top: 0px; padding-bottom: 0px; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;"><span style="font-family: monospace; padding: 0px; margin: 0px; line-height: normal;">不过我更倾向于前一种设计,因为那样做,代码一切都在自己的掌握中,而io_service则是经过许多封装代码,并且本身设计只是用于处理网络完成事件的。</span></p><p style="margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 14px; line-height: 25px; padding-top: 0px; padding-bottom: 0px; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;"><span style="font-family: monospace; padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; line-height: normal; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;">无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE。</span></span></p><pre name="code" class="cpp">
#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

using namespace std;

int filedes[2];
int main( void )
{
    char buf[80];
    pid_t pid;
    
    pipe( filedes );
    fcntl(filedes[0], F_SETFL, O_NONBLOCK);
    fcntl(filedes[1], F_SETFL, O_NONBLOCK);
    int epoll_fd = epoll_create(20);
    if (epoll_fd!= -1)
	fcntl(epoll_fd, F_SETFD, FD_CLOEXEC);
     epoll_event ev = { 0, { 0 } };
    ev.events = EPOLLIN | EPOLLERR | EPOLLET;
    ev.data.fd = filedes[0];
    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, filedes[0], &ev);
    pid=fork();        
    if (pid > 0)
    {
        printf( "This is in the father process,here write a string to the pipe.\n" );
        char s[] = "Hello world , this is write by pipe.\n";
        write( filedes[1], s, sizeof(s) );
	while(1){
		sleep(10);
		epoll_event ev = { 0, { 0 } };
  		ev.events = EPOLLIN | EPOLLERR | EPOLLET;
  		ev.data.fd = filedes[0];
  		epoll_ctl(epoll_fd, EPOLL_CTL_MOD, filedes[0], &ev);
		printf( "interrupt send\n" );

	}
    }
    else if(pid == 0)
    {
        printf( "This is in the child process,here read a string from the pipe.\n" );
	while(1){
		epoll_event events[128];
		int num_events = epoll_wait(epoll_fd, events, 20, 10000);

		for (int i = 0; i < num_events; ++i)
		{	
			int  fd = events[i].data.fd;
			if (fd == filedes[0])
			{
				printf("interrupt recv\n");
			}
		}
	}
       
    }
     close( filedes[0] );
     close( filedes[1] );
      close(epoll_fd);
    //waitpid( pid, NULL, 0 );
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值