Article View: borland.public.cppbuilder.internet.socket
Article #2418Re: Using TClientSocket in multi-threaded app
From: Mark Atkin
Date: Wed, 26 Apr 2006 17:35
Date: Wed, 26 Apr 2006 17:35
99 lines
4749 bytes
4749 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:<444fab11$1@newsgroups.borland.com>... > > "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. > I have thought about doing this. I still might. > > 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. > I don't know much about the internals of the VCL, but that was the only conclusion I could come up with. I'm glad to know you came up with the same conclusion. > > 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. > Unfortunately for me, this is the first experience I've had with writing multi-threaded applications. It's definitely been a learning experience. > > 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. > Everything always works fine on the development system, right? :P > > 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. > Again, I'm learning more all the time about using threads. > > Gambit > > Thanks Gambit.
Technical Details
Message-ID:
<444faf4f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4447ee51$1@newsgroups.borland.com> <444fab11$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: "Mark Atkin" <matkin@windrock.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <4447ee51$1@newsgroups.borland.com> <444fab11$1@newsgroups.borland.com> Subject: Re: Using TClientSocket in multi-threaded app Date: Wed, 26 Apr 2006 13:35:19 -0400 Lines: 101 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 69.2.92.6 Message-ID: <444faf4f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1146072911 69.2.92.6 (26 Apr 2006 10:35:11 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail