找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4557|回复: 2

写了一个很简单的reactor,为什么段错误啊,百思不得其解

[复制链接]
发表于 2012-5-8 22:23:37 | 显示全部楼层 |阅读模式
  1. #include <ace/Task.h>
  2. #include <ace/streams.h>
  3. #include <ace/Time_Value.h>
  4. #include <ace/Handle_Set.h>
  5. #include <ace/Log_Msg.h>
  6. #include <ace/INET_Addr.h>
  7. #include <ace/SOCK_Connector.h>
  8. #include <ace/SOCK_Acceptor.h>
  9. #include <ace/SOCK_Stream.h>
  10. #include <ace/Thread_Manager.h>
  11. #include <ace/Thread.h>
  12. #include <ace/Synch.h>
  13. #include <ace/Condition_T.h>
  14. #include <ace/Message_Block.h>
  15. #include <ace/Reactor.h>
  16. #include <ace/Dev_Poll_Reactor.h>
  17. #include <ace/Event_Handler.h>
  18. //typedef ACE_Acceptor<Sock_Handler, ACE_SOCK_Acceptor> AcceptHandler;
  19. class server : public ACE_Event_Handler
  20. {
  21. public:
  22.     server(int clientCount = 10000);
  23.     ~server();
  24.     bool start(int port);
  25.     ACE_HANDLE get_handle(void) const { return acceptor.get_handle(); }
  26.     int handle_input(ACE_HANDLE handle) { /* ... */ }
  27.     int handle_close(ACE_HANDLE handle) { /* ... */ }
  28. private:
  29.     ACE_SOCK_Acceptor acceptor;
  30.     ACE_Reactor *reactor;;
  31. };
  32. ACE_THR_FUNC_RETURN runThreadReactor(void *arg)
  33. {
  34.     ACE_TRACE("runThreadReactor\n");
  35.     ACE_Reactor *reactor = (ACE_Reactor *)arg;
  36.     reactor->run_reactor_event_loop();
  37.     return NULL;
  38. }
  39. server::server(int clientCount)
  40. {
  41.     ACE_Dev_Poll_Reactor *ePoll_reactor = new ACE_Dev_Poll_Reactor(clientCount);
  42.     reactor = new ACE_Reactor(ePoll_reactor);
  43. }
  44. server::~server()
  45. {
  46. }
  47. bool server::start(int port,
  48.                    bool isPacket)
  49. {
  50.     ACE_INET_Addr addr(port);
  51.     if (acceptor.open(addr, 1) == -1)
  52.     {
  53.         ACE_ERROR((LM_ERROR, "failed to open port:%d to linsten\n", port));
  54.         return false;
  55.     }
  56.     if (reactor->register_handler(this, ACE_Event_Handler::ACCEPT_MASK) == -1)
  57.     {
  58.         ACE_ERROR((LM_ERROR, "failed to register handler\n"));
  59.         if (-1 == acceptor.close())
  60.             ACE_ERROR((LM_ERROR, "failed to close acceptor\n"));
  61.         return false;
  62.     }
  63.     ACE_Thread_Manager::instance()->spawn_n(10, runThreadReactor, &reactor);
  64.     ACE_DEBUG((LM_DEBUG, "server(%d) start completed\n", port));
  65.     return true;
  66. }
  67. int main()
  68. {
  69.     server *srv;
  70.     ACE_NEW_NORETURN(srv, server(1000));
  71.     srv->start(9688);
  72.     while(1) usleep(1);
  73.     return 0;
  74. }
复制代码
一运行到reactor->run_reactor_event_loop()就段错误,就算什么都不做,reactor什么事件都不注册。
找了很久都没想明白。
 楼主| 发表于 2012-5-9 10:25:18 | 显示全部楼层
太粗心了  ACE_Thread_Manager::instance()->spawn_n(10, runThreadReactor, &reactor);
reactor传的是指针的地址 我晕
发表于 2012-5-13 22:22:12 | 显示全部楼层
这算自问自答么:)
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-21 20:58 , Processed in 0.020771 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表