最近看了一下JAWS的代码,thread_per_request模式下:- ...
- for (;;)
- {
- if (this->acceptor_.accept (stream) == -1)
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
- ACE_TEXT ("HTTP_Acceptor::accept")), -1);
- Thread_Per_Request_Task *t;
- // Pass grp_id as a constructor param instead of into open.
- ACE_NEW_RETURN (t, Thread_Per_Request_Task (stream.get_handle (),
- this->tm_,
- grp_id,
- factory),
- -1);
- if (t->open () != 0)
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
- ACE_TEXT ("Thread_Per_Request_Task::open")),
- -1);
- // Throttling is not allowing too many threads to run away.
- // Should really use some sort of condition variable here.
- if (!this->throttle_)
- continue;
- // This works because each task has only one thread.
- while (this->tm_.num_tasks_in_group (grp_id) > this->threads_)
- this->tm_.wait (&wait_time);
- }
- ...
复制代码 这种方式在多个并发请求的时候是否会丢失?
还有Synch_Thread_Pool_Task模式下:- ...
- for (;;)
- {
- ACE_SOCK_Stream stream;
- // Lock in this accept. When it returns, we have a connection.
- if (this->acceptor_.accept (stream) == -1) /// 这里其中第一个线程会等待accept,其余的等待锁,直到一个新的连接到来
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%p\n"),
- ACE_TEXT ("HTTP_Acceptor::accept")), -1);
- ACE_Message_Block *mb = 0;
- ACE_NEW_RETURN (mb, ACE_Message_Block (HTTP_Handler::MAX_REQUEST_SIZE + 1), -1);
- // Create an HTTP Handler to handle this request
- HTTP_Handler *handler = this->factory_.create_http_handler ();
- handler->open (stream.get_handle (), *mb);
- // Handler is destroyed when the I/O puts the Handler into the
- // done state.
- mb->release ();
- ACE_DEBUG ((LM_DEBUG,ACE_TEXT (" (%t) in Synch_Thread_Pool_Task::svc, recycling\n")));
- }
- ...
复制代码 这边以线程池模式存在,而处理一个新连接时会为每个连接创建Handler,这里如果使用一个Handler Pool效果会不会更好些? |