Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-10-26 12:57:51


Phil Endecott wrote:

> Yes, that's true. I suppose I'm thinking this way because I'm also
> imagining that I can subclass this new Thread class to add state, e.g.
>
> class DaemonThread: public Thread {
> private:
> ... per-connection state ...
>
> public:
> DaemonThread(int connection_fd);
> };
>
> void mainloop() {
> while(1) {
> int fd = accept_connection_on_socket();
> DaemonThread* handler = new DaemonThread(fd);
> }
> }

Try something along the lines of:

class DaemonThread
{
private:

    int cfd_;

public:

    explicit DaemonThread( int connection_fd ): cfd_( connection_fd ) {}
    void run() { /* thread procedure */ }
};

void mainloop()
{
    while( 1 )
    {
        int fd = accept();
        boost::shared_ptr<DaemonThread> handler( new DaemonThread( fd ) );
        boost::thread th( boost::bind( &DaemonThread::run, handler ) );
    }
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net