bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Deleting a non-autofree thread


From: Chad Pettit
Subject: Re: Deleting a non-autofree thread
Date: Tue, 22 Oct 2002 16:07:54 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826

It happens on a linux box, running Mandrake 9.0.

I attached my code.  

The Dtran class basically controls both the transmitter and receiver connections, which are TCPSessions.  When isRunning() is false for one of the connections it deletes the connection.

Federico Montesino Pouzols wrote:
	This is strange, what system does this problem occur on? Could
you post your code?

On Mon, Oct 21, 2002 at 04:05:14PM -0400, Chad Pettit wrote:
  
Yes.  A thread that does not do a delete this in its final method.

Currently, I am doing a delete thread once the main thread retrieves 
some information off of the processing thread.  This; however, only 
works for the first 512 threads.  After that I am getting a resource 
allocation error.  To me, this indicates that the thread is not being 
completely destroyed, since the thread limit should not come into play 
as long as the threads are being cleaned up properly.

Federico Montesino Pouzols wrote:

    
On Mon, Oct 21, 2002 at 11:11:54AM -0400, Chad Pettit wrote:


      
What is the proper way to delete a non-autofree thread?  
  

        
	Do you mean a thread that does not do 'delete this' in its
final() method? Normally, threads can be deleted using 'delete
thread' if they are dynamically created.



      
I have tried 
several combo's with endStream(), terminate(), endSocket() and others 
and nothing appears to be working.  I get an out of resource error when 
I would try to allocate another thread.  I am using TCSession.



_______________________________________________
Bug-commoncpp mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/bug-commoncpp
  

        
_______________________________________________
Bug-commoncpp mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/bug-commoncpp



      

  
#ifndef CONNECTION_H
#define CONNECTION_H

#include <cc++/socket.h>
#include <cc++/thread.h>

typedef std::string string;

class Connection : public ost::TCPSession
{
        public:
                Connection(ost::TCPSocket &server);
                Connection(const ost::InetHostAddress &host, ost::tpport_t 
port, const string &message);
                
                ~Connection();
                bool isRunning();
        
        protected:
                void run();
                void final();
                void close();
        
                void runTransmitter();
                void runReceiver();
                        
        private:
                void init();
                        
                bool _type; // 0 Transmitter 1 Receiver
                string _message;
                bool _isRun;
                
};

#endif
#ifndef DTRAN_H
#define DTRAN_H

#include "Connection.h"
#include <cc++/socket.h>
#include <vector>

typedef std::string string;

class Dtran : public ost::TCPSocket
{
        public:
                Dtran(const ost::InetAddress &bind, ost::tpport_t port);
                ~Dtran();
        
                void serve();
                void send(const ost::InetHostAddress &host, const string 
&message);
                void status();
        
        protected:
                void statusReceiver();
                void statusTransmitter();
        
        private:
                int rec;
                ost::tpport_t _port;
                std::vector<Connection*> _receiverList;
                std::vector<Connection*> _transmitterList;
};

#endif

#include "Connection.h"
#include <iostream>

Connection::Connection(const ost::InetHostAddress &host, ost::tpport_t port, 
const string &message)
        : ost::TCPSession(host, port)
{
        _type = 0;
        _message = message;
        init();
        
}

Connection::Connection(ost::TCPSocket &server)
        : ost::TCPSession(server)
{
        _type = 1;
        init();
}

void Connection::init()
{
        _isRun = true;
}

Connection::~Connection()
{
        terminate();
}

bool Connection::isRunning()
{
        return _isRun;
}

void Connection::close()
{
        exit();
}

void Connection::run()
{
        _isRun = true;
        
        if (_type == 0)
                runTransmitter();
        else
                runReceiver();
        
        close();
}

void Connection::final()
{
        _isRun = false;
}

void Connection::runTransmitter()
{
        std::iostream* connection = tcp();
        *connection << _message.c_str();
}

void Connection::runReceiver()
{       
        char line[200];
        std::iostream* connection = tcp();

        while (!connection->eof())
        {
                *connection >> line;
                std::cerr << "Receive: " << line << std::endl;
        }
}

#include "Dtran.h"
#include <iostream>

Dtran::Dtran(const ost::InetAddress &bind, ost::tpport_t port)
                : ost::TCPSocket(bind, port)
{
        _port = port;
        rec = 0;
}

Dtran::~Dtran()
{
        for (int i = _receiverList.size() - 1; i >= 0; --i)
        {
                delete _receiverList[i];
        }
        for (int i = _transmitterList.size() - 1; i >= 0; --i)
        {
                delete _transmitterList[i];
        }

        _receiverList.clear();
        _transmitterList.clear();
}

void Dtran::serve()
{
        int error;
        
        while (isPendingConnection(200))
        {
                Connection *connection = new Connection(*this);
                
                if ((error = connection->start()) == 0)
                        _receiverList.push_back(connection);
                else
                        std::cerr << "**************START REC FAILED********** 
" << strerror(error) << std::endl;
                std::cerr << "Number of connections: " << rec++ << std::endl;
        }
}

void Dtran::send(const ost::InetHostAddress &host, const string &message)
{
        int error;
        
        Connection *connection = new Connection(host, _port, message);
        if ((error = connection->start()) == 0)
                _transmitterList.push_back(connection);
        else
                std::cerr << "**************START TRANS FAILED********** " << 
strerror(error) << std::endl;
        
}

void Dtran::status()
{
        statusReceiver();
        statusTransmitter();
}

void Dtran::statusReceiver()
{       
        for (int i = _receiverList.size() - 1; i >= 0; --i)
        {
                std::cerr << "recv(" << i << ") " << 
_receiverList[i]->isRunning() << std::endl;
                if (!_receiverList[i]->isRunning())
                {
                        // receiver is finished
                        delete _receiverList[i];
                        _receiverList.erase(_receiverList.begin() + i);
                }
        }
}

void Dtran::statusTransmitter()
{
        for (int i = _transmitterList.size() - 1; i >= 0; --i)
        {
                std::cerr << "trans(" << i << ") " << 
_transmitterList[i]->isRunning() << std::endl;
                if (!_transmitterList[i]->isRunning())
                {
                        // transmitter is finished
                        delete _transmitterList[i];
                        _transmitterList.erase(_transmitterList.begin() + i);
                }
        }
}


#include "Dtran.h"

int main(int argc, char *argv[])
{
        ost::tpport_t port;
        ost::tcpstream tcp;
        
        sscanf(argv[2], "%d", &port);
        
        Dtran *dtran = new Dtran(argv[1], port);
        
        for (;;)
        {
                dtran->serve();
                dtran->send(argv[3], argv[4]);
                dtran->status();
        }
                
        delete dtran;
        
        return 0;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]