🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems

#4121
From: Omar Zelaya
Date: Fri, 08 Apr 2005 15:21
97 lines
3447 bytes
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: <4256a163@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <425669ba@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: "Omar Zelaya" <ozelaya2010@hotmail.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <425669ba@newsgroups.borland.com>
Subject: Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems
Date: Fri, 8 Apr 2005 09:21:24 -0600
Lines: 99
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: 200.30.163.211
Message-ID: <4256a163@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1112973667 200.30.163.211 (8 Apr 2005 08:21:07 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail