🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems

#4122
From: Remy Lebeau (TeamB)
Date: Fri, 08 Apr 2005 17:33
58 lines
2612 bytes
"Andy Blackmore" <andy@number18.co.uk> wrote in message
news:425669ba@newsgroups.borland.com...

> But I found that the variable listed here (Count) is global to all
threads.

Yes.

> But I don't know how I link this new class into the IdTCPServer component.

You need to assign the server's ThreadClass propety before activating the
server, such as in the form's constructor.

    __fastcall TFArt::TFArt(TComponent *Owner)
        : TForm(Owner)
    {
        IdTCPServer1->ThreadClass = __classid(TMyIdThread);
    }

Then you can cast the TIdPeerThread pointer in your event handler:

    void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        //...
        static_cast<TMyIdThread*>(AThread)->Count++;
        //...
    }

Alternatively, if the Count is the only value you want stored with each
thread, you can make use of the Thread's existing Data property instead, ie:

    void __fastcall TFArt::IdTCPServer1Connect(TIdPeerThread *AThread)
    {
        AThread->Data = NULL;
    }

    void __fastcall TFArt::IdTCPServer1Disconnect(TIdPeerThread *AThread)
    {
        // IMPORTANT TO DO SO HERE!!!  The thread owns the Data property.
        // If the property is not NULL when the thread terminates, it will
treat the
        // property value as an object and try to free it, which will crash
since we are
        // not storing actual objects in it to begin with.
        AThread->Data = NULL;
    }

    void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        //...
        int count = reinterpret_cast<int>(AThread->Data);
        AThread->Data = reinterpret_cast<TObject*>(++count);
        //...
    }


Gambit
Technical Details
Message-ID: <4256bffa@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <425669ba@newsgroups.borland.com>
Subject: Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems
Date: Fri, 8 Apr 2005 10:33:26 -0700
Lines: 60
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: <4256bffa@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1112981498 68.69.17.41 (8 Apr 2005 10:31:38 -0800)
Path: arc1-ca.pugleaf.net!g2news1.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