🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2416

Re: Using TClientSocket in multi-threaded app

#2416
From: Remy Lebeau (TeamB)
Date: Wed, 26 Apr 2006 17:14
68 lines
3935 bytes
"Mark Atkin" <matkin@windrock.com> wrote in message
news:4447ee51$1@newsgroups.borland.com...

> The next time the thread checks it's status, the thread calls new
> TClientSocket to allocate a new socket connection to use for
> the communication, retrieves the data, then deletes it's instance
> of TClientSocket.

A better approach would be to create the TClientSocket instance only once
when the thread is created and then free the TClientSocket when the thread
is being freed.  You don't need to create a new TClientSocket instance over
and over during the thread's lifetime.  Just Connect/Disconnect it when
needed.

> When I tried it on a client's computer, I began getting AVs when
> the thread was calling new TClientSocket.

That is because of the way you are managing the TClientSocket.  Keep
reading...

> I was creating my instances of TClientSocket as follows:
>
> TClientSocket *sock = new TClientSocket(MainForm);

That is not thread-safe.  You are telling the TClientSocket to be owned by a
TForm that resides in another thread.  All of the ownership management that
the VCL does internally only works when used in a single thread.  In this
case, you should be using NULL instead to assign no owner to the
TClientSocket at all.  The thread manages the TClientSocket's lifetime, so
there is no need to assign any owner anyway.

> My theory as to why I would get AVs is that maybe all three threads
> are calling new TClientSocket(MainForm) almost simultaneously and
> basically, some method and/or property from my MainForm is being
> accessed by all three threads to add the TClientSocket component to
> the ComponentList array and increment the ComponentCount for the
> MainForm.

That is very likely, yes.  Which is exactly why specifying an Owner is not
thread-safe at all.

> I changed the socket instantiation to new TClientSocket(NULL) and
> the problem has gone away.

There you go then.

> The biggest hole in my theory that I can see is that it doesn't really
> explain why it would work on my PC and not my clients.

A fluke, nothing more.  It is likely just a timing issue, which are very
difficult to debug.  Maybe your computer is slower then the client's
computer and thus the problem doesn't show up as much on your machine since
the main thread properties/members are not access quite as simultaneously.

> Also, the only advantage that I can see to having MainForm being
> the owner of the TClientSocket is that if something unexpected occurs
> and the TClientSocket doesn't get deleted somehow, the MainForm
> will free up the memory when the form gets destroyed.

That is a very bad design when threads are involved.  You should not be
assigning ownership across threads.  Each thread should own its own objects.
If something goes bad and your main thread does not terminate the threads,
the threads will still be running when the main thread frees the objects
that the threads are using.


Gambit
Technical Details
Message-ID: <444fab11$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4447ee51$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,f074aec44b23f651
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4447ee51$1@newsgroups.borland.com>
Subject: Re: Using TClientSocket in multi-threaded app
Date: Wed, 26 Apr 2006 10:14:19 -0700
Lines: 70
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <444fab11$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1146071825 68.69.17.41 (26 Apr 2006 10:17:05 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail