BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with ThreadPooling ...

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Noel
Guest





PostPosted: Tue Feb 07, 2006 4:02 pm    Post subject: Help with ThreadPooling ... Reply with quote



Hello All !

I am writing a HTTP server using Broland C++ Builder 6 and indy 9.
For better performance i would like to use Thread Pooling with my
TIdHTTPServer component.

Here an excerpt from help:
"To use TIdThreadMgrPool with TIdTCPServer, create an instance of
TIdThreadMgrPool, and assign the object reference to
TIdTCPServer.ThreadMgr."

and

"Assign an instance of TIdThreadClass to ThreadClass before using the
thread manager to allocate new threads using CreateNewThread."

I am also using MySQL and would like to follow Chad's guidelines

"When each client needs a resource that is expensive, resource-wise, to
create, you should consider pooling these resources. Specifically, for a
high-load version of this server, the database connections should be
pooled. An easy way to pool database connections is to use a thread
pool, creating the connection in each thread’s constructor,
and releasing the connection in the corresponding destructor."

+--------------------------------------------------------+

Now my problem is that for some unknown reason ...
the ThreadMgr is not using the TIdThread descendant i want to use.

Here my code:

This is the definition of the TidThread descendant i want to use:

class TIdDS_Thread : public TIdThread
{
private:
int ThreadIndex;

public:
__fastcall TIdDS_Thread (bool CreateSuspended);
__fastcall ~TIdDS_Thread (void);
};

.... creator
__fastcall TIdDS_Thread::TIdDS_Thread (bool CreateSuspended)
: TIdThread (CreateSuspended)
{
ThreadIndex = 0;
}


I create and initialize ...

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
...
IdThreadMgrPool = new TIdThreadMgrPool (this);
IdThreadMgrPool -> ThreadClass = __classid(TIdDS_Thread);

IdHTTPServer = new TIdHTTPServer (this);
IdHTTPServer -> ThreadMgr = IdThreadMgrPool;
...


+-----------------------------------------------------------+

The creator of my TIdDS_Thread is never called ... (?)

What am i missing ?

IdThreadMgrPool -> ThreadClass = __classid(TIdDS_Thread);

Is this the proper way to tell the Thread manager to use my
TIdThread descendant ?


Thank you for your precious help !!!

Noël
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Feb 07, 2006 9:02 pm    Post subject: Re: Help with ThreadPooling ... Reply with quote



"Noel" <noel (AT) teledata (DOT) qc.ca> wrote in message
news:43e8b9a7$1 (AT) newsgroups (DOT) borland.com...

Quote:
Now my problem is that for some unknown reason ...
the ThreadMgr is not using the TIdThread descendant
i want to use.

Your code is wrong. See below.

Quote:
class TIdDS_Thread : public TIdThread

You must derive from TIdPeerThread instead. TIdTCPServer's internals expect
the threads to always be TIdPeerThread instances. If they are not, then
unexpected things can happen.

Quote:
void __fastcall TMainForm::FormCreate(TObject *Sender)

DO NOT use the OnCreate event in C++. It is a Delphi idiom that produces
illegal behavior in C++ as it can be triggered before the constructor. Use
the actual constructor instead.

Quote:
IdThreadMgrPool -> ThreadClass = __classid(TIdDS_Thread);

You need to set the ThreadClass property on the TIdHTTPServer, not on
TIdThreadMgrPool directly. TIdTCPServer assigns the ThreadMgr->ThreadClass
when it is activated. Since TIdTCPServer's default ThreadClass is
TIdPeerThread, your assignment of the ThreadMgr->ThreadClass will get
overwritten automatically.

__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
...
IdHTTPServer = new TIdHTTPServer(this);
IdHTTPServer->ThreadMgr = new TIdThreadMgrPool(IdHTTPServer);
IdHTTPServer->ThreadClass = __classid(TIdDS_Thread);
...
}


Gambit
Back to top
Noel
Guest





PostPosted: Tue Feb 07, 2006 11:02 pm    Post subject: Re: Help with ThreadPooling ... Reply with quote



Hello Gambit !

You are an expert !
Works ok now !

Quote:
TIdTCPServer's internals expect the threads to always be
TIdPeerThread instances. If they are not, then > unexpected
things can happen.

It did produce an infinite loop .. :-(

Quote:
DO NOT use the OnCreate event in C++ ...
as it can be triggered before the constructor.

I did notice that in another program ! Ok now i know why !

Thank you so much for your time and expertise !

Noël

Remy Lebeau (TeamB) wrote:
Quote:
"Noel" <noel (AT) teledata (DOT) qc.ca> wrote in message
news:43e8b9a7$1 (AT) newsgroups (DOT) borland.com...


Now my problem is that for some unknown reason ...
the ThreadMgr is not using the TIdThread descendant
i want to use.


Your code is wrong. See below.


class TIdDS_Thread : public TIdThread


You must derive from TIdPeerThread instead. TIdTCPServer's internals expect
the threads to always be TIdPeerThread instances. If they are not, then
unexpected things can happen.


void __fastcall TMainForm::FormCreate(TObject *Sender)


DO NOT use the OnCreate event in C++. It is a Delphi idiom that produces
illegal behavior in C++ as it can be triggered before the constructor. Use
the actual constructor instead.


IdThreadMgrPool -> ThreadClass = __classid(TIdDS_Thread);


You need to set the ThreadClass property on the TIdHTTPServer, not on
TIdThreadMgrPool directly. TIdTCPServer assigns the ThreadMgr->ThreadClass
when it is activated. Since TIdTCPServer's default ThreadClass is
TIdPeerThread, your assignment of the ThreadMgr->ThreadClass will get
overwritten automatically.

__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
...
IdHTTPServer = new TIdHTTPServer(this);
IdHTTPServer->ThreadMgr = new TIdThreadMgrPool(IdHTTPServer);
IdHTTPServer->ThreadClass = __classid(TIdDS_Thread);
...
}


Gambit

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.