Article View: borland.public.cppbuilder.internet.socket
Article #4028Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems
From: Andy Blackmore
Date: Fri, 08 Apr 2005 16:19
Date: Fri, 08 Apr 2005 16:19
112 lines
4041 bytes
4041 bytes
Hello Again
Thanks for the quick responce. The reason I needed a static is because I
needed to maintain a count value for each independant thread. Your second
solution looks A1 though, I will
give it a try later.
Thanks again
Andy Blackmore
"Omar Zelaya" <ozelaya2010@hotmail.com> wrote in message
news:4256a163@newsgroups.borland.com...
> Use the static storage class specifier with a local variable to preserve
> the last value between successive calls to that function. A static
> variable acts like a local variable but has the lifetime of an external
> variable.
>
> I think that if you remove static from the Count variable declaration, it
> will work as you expect.
>
> You can also do this
>
> class TMyClass
> {
> public:
> int Count;
> protected:
> private:
> };
>
> On the connect event of the IdTCPServer1 create the class and assign it to
> the Data property of TIdPeerThread and destroy it on the disconnect event
> something like this:
>
> void __fastcall TFArt::IdTCPServer1Connect(TIdPeerThread *AThread)
> { TMyClass* MyClass;
>
> MyClass = new TMyClass();
> MyClass->Count = 0;
> AThread->Data = MyClass;
> }
>
> void __fastcall TFArt::IdTCPServer1Disconnect(TIdPeerThread *AThread)
> { TMyClass* MyClass;
>
> MyClass = (TMyClass*) AThread->Data;
> delete MyClass;
> AThread->Data = NULL;
> }
>
> void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
> { TMyClass* MyClass;
> AnsiString ReplyStr;
>
> MyClass = (TMyClass*) AThread->Data;
>
> ReplyStr = AThread->Connection->ReadLn();
>
> if(ReplyStr == "c")
> {
> MyClass->Count++;
> AThread->Connection->WriteLn("Count = " + IntToStr(MyClass->Count));
> }
> else
> if(ReplyStr == 'q')
> AThread->Connection->Disconnect();
> }
>
> "Andy Blackmore" <andy@number18.co.uk> wrote in message
> news:425669ba@newsgroups.borland.com...
>> Hello
>> I am trying to create a multi threaded server with a local variable to
>> each thread and not a global one. I first tried the code listed below :-
>>
>> void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
>> {
>> static int Count = 0;
>> AnsiString ReplyStr;
>>
>> ReplyStr = AThread->Connection->ReadLn();
>>
>> if(ReplyStr == "c")
>> {
>> Count++;
>> AThread->Connection->WriteLn("Count = " + IntToStr(Count));
>> }
>> else
>> if(ReplyStr == 'q')
>> AThread->Connection->Disconnect();
>> }
>>
>> But I found that the variable listed here (Count) is global to all
>> threads. So I looked at creating a new class derived from TIdPeerThread
>> and called TMyIdThread where I can declair a local variable
>>
>> class TMyIdThread : public TIdPeerThread {
>> public:
>> int Count;
>> protected:
>> private:
>> ];
>>
>> But I don't know how I link this new class into the IdTCPServer
>> component. Please can someone help me with this problem
>>
>> Thanks in advance
>>
>>
>> Andy Blackmore
>>
>
>
Technical Details
Message-ID:
<425a3a86@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<425669ba@newsgroups.borland.com> <4256a163@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3ba22e438513e68b X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Andy Blackmore" <andy@number18.co.uk> Newsgroups: borland.public.cppbuilder.internet.socket References: <425669ba@newsgroups.borland.com> <4256a163@newsgroups.borland.com> Subject: Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems Date: Fri, 8 Apr 2005 17:19:09 +0100 Lines: 114 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 193.195.12.161 Message-ID: <425a3a86@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1113209478 193.195.12.161 (11 Apr 2005 01:51:18 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail