Overview
Features
Download
Documentation
Community
Add-Ons & Services

pthreads problem by using Poco::Net::TCPServer

A general discussion forum.

pthreads problem by using Poco::Net::TCPServer

Postby IMAX » 28 Apr 2014, 12:45

Hello everyone,
Thanks for watching my thread.
I got some questions while using Poco::Net::TCPServer.

Let`s move to the codes directly.
Code: Select all
// Connecter.cpp
using Poco::Net::IPAddress;
using Poco::Net::SocketAddress;
using Poco::Net::Context;
using Poco::Net::TCPServer;
using Poco::Net::SecureServerSocket;
using Poco::Net::TCPServerConnectionFactoryImpl;
using Poco::AutoPtr;

// Called in main func
void Connecter::Run( )
{
  Context::Ptr pContext = new Context(
    Context::SERVER_USE,
    GETCONFIG.getprivateKeyFile(),
    GETCONFIG.getCertificateFile(),
    GETCONFIG.getcaLocation(),
    Context::VERIFY_NONE);

  SecureServerSocket svs(SocketAddress( IPAddress::wildcard(),GETCONFIG.getPort() ),64,pContext);

  _pServer = shared_ptr<Poco::Net::TCPServer>( new TCPServer(new TCPServerConnectionFactoryImpl<ConnectHandler>(), svs) );
  _pServer->start();// Start server socket listen
}

Code: Select all
//ConnectHandler.cpp
void ConnectHandler::run()  //Handle a client socket
{
  StreamSocket& ss = this->socket();
  ss.setKeepAlive(false);
  ss.setSendTimeout( Poco::Timespan(60*5,0) );
  ss.setReceiveTimeout( Poco::Timespan(60*5,0) );
  ss.setReceiveBufferSize(Config::S_revbuflen);
 
  size_t buflen = Config::S_revbuflen;
  boost::scoped_array<char> Buf(new char[buflen]);
  memset(Buf.get(),0,buflen);
  try
  {
    Rrp rrp;
    CmdManager manager(&rrp);
    while(_Sustained)// In the constructor: _Sustained = true;
    {
      if (ss.receiveBytes(Buf.get(),buflen) > 0)
      {
        string strRet =  executeCmd(Buf.get(),&manager);
        ss.sendBytes(strRet.c_str(),strRet.size());
         memset(Buf.get(),0,buflen);
      }
    }
  }
  catch (Poco::Exception& ex)
  {
    LOGERROR(ex.displayText() );
  }
  ss.close();
}

These all core codes about this server, and no more pthread related object.

Q1: I started this server, there was 4 living threads in the process before any clients setup a connection, why? I thought it would be only one listening thread in the Poco::Net::TCPServer
Code: Select all
//TCPServer.cpp
void TCPServer::run()
{
   while (!_stopped)
   {
      Poco::Timespan timeout(250000);
      if (_socket.poll(timeout, Socket::SELECT_READ))
      {
         try
         {
            StreamSocket ss = _socket.acceptConnection();
            // enabe nodelay per default: OSX really needs that
            ss.setNoDelay(true);
            _pDispatcher->enqueue(ss);
         }
         ...
      }
   }
}

Code: Select all
[root@localhost ~]# ps -efFLl | grep vhost
0 S root     64284 63603 64284  0    4  80   0 - 23617 futex_  5628   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64286  0    4  80   0 - 23617 futex_  5628   1 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64287  0    4  80   0 - 23617 futex_  5628   1 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64288  0    4  80   0 - 23617 ep_pol  5628   1 16:26 pts/1    00:00:00 ./vhost // I believed that is the TCPServer::run() thread.
0 S root     64290 63678 64290  0    1  80   0 - 25813 pipe_w   836   1 16:26 pts/3    00:00:00 grep vhost

Q2. I setup 2 client connections, there were still 4 threads in the proc, until the 3rd client connected. Then it increased to 5 threads. Why would it using first 2 thread instead of create new one.
Code: Select all
[root@localhost ~]# ps -efFLl | grep vhost
0 S root     64284 63603 64284  0    4  80   0 - 72769 futex_  6208   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64286  0    4  80   0 - 72769 sk_wai  6208   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64287  0    4  80   0 - 72769 sk_wai  6208   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64288  0    4  80   0 - 72769 ep_pol  6208   0 16:26 pts/1    00:00:00 ./vhost
0 R root     64319 63678 64319  0    1  80   0 - 25813 -        832   1 16:41 pts/3    00:00:00 grep vhost
[root@localhost ~]# ps -efFLl | grep vhost
0 S root     64284 63603 64284  0    5  80   0 - 91714 futex_  6296   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64286  0    5  80   0 - 91714 sk_wai  6296   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64287  0    5  80   0 - 91714 sk_wai  6296   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64288  0    5  80   0 - 91714 ep_pol  6296   0 16:26 pts/1    00:00:00 ./vhost
1 S root     64284 63603 64320  0    5  80   0 - 91714 sk_wai  6296   0 16:41 pts/1    00:00:00 ./vhost
0 R root     64322 63678 64322  0    1  80   0 - 25813 -        832   1 16:41 pts/3    00:00:00 grep vhost

Q3. I try to end up a ConnectHandler::run() thread by controlling the _Sustained var, and the GDB go through the ss.close() line, but it doesn`t work, the thread remained in the proc.Why?
Code: Select all
class Net_API TCPServerConnection: public Poco::Runnable
   /// The abstract base class for TCP server connections
   /// created by TCPServer.
   ///
   /// Derived classes must override the run() method
   /// (inherited from Runnable). Furthermore, a
   /// TCPServerConnectionFactory must be provided for the subclass.
   ///
   /// The run() method must perform the complete handling
   /// of the client connection. As soon as the run() method
   /// returns, the server connection object is destroyed and
   /// the connection is automatically closed.
   ///
   /// A new TCPServerConnection object will be created for
   /// each new client connection that is accepted by
   /// TCPServer.

Code: Select all
(gdb) info threads
  5 Thread 0x7f8d73f97700 (LWP 64286)  0x0000003d56c0e75d in read () from /lib64/libpthread.so.0
  4 Thread 0x7f8d73596700 (LWP 64287)  0x0000003d56c0e75d in read () from /lib64/libpthread.so.0
  3 Thread 0x7f8d72b95700 (LWP 64288)  0x0000003d564e9163 in epoll_wait () from /lib64/libc.so.6
  2 Thread 0x7f8d72194700 (LWP 64320)  0x0000003d56c0e75d in read () from /lib64/libpthread.so.0
* 1 Thread 0x7f8d741a77e0 (LWP 64284)  0x0000003d56c0b5bc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

Any suggestion will be helpful, thanks all!
IMAX
 
Posts: 1
Joined: 28 Apr 2014, 11:29

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron