Fastream Technologies wrote:
> Q: Do one need to protect Terminated variable of TThread and
> Terminate() as well?

No, read/write of property Terminated should be atomic, so you
won't get an error but a critical section may be usefull.

if not Thread.Terminated then
    Thread.Terminate;

If you do this from multiple threads it may happen this:

if not Thread.Terminated then
    // thread switch occures and another
    // thread calls Terminate so
    // Terminated is now TRUE!!    
    Thread.Terminate;
  

---
Arno Garrels [TeamICS]
http://www.overbyte.be/eng/overbyte/teamics.html


> 
> Best Regards,
> 
> Gorkem Ates
> 
> ----- Original Message -----
> From: "Arno Garrels" <[EMAIL PROTECTED]>
> To: "ICS support mailing" <twsocket@elists.org>
> Sent: Thursday, February 01, 2007 4:31 PM
> Subject: Re: [twsocket] THttpMTServer problem with Dup
> 
> 
>> Fastream Technologies wrote:
>> 
>>>> I don't know this exception message, do you mean "List index out of
>>>> bounds()"?
>>> 
>>> Yes, exactly.
>> 
>> So most likely you hit a synchronization bug in your code, hard to
>> say what's wrong. Critical stuff is access to the client list,
>> add/remove clients and iterate. Those things need to be
>> protected very carefully. Only one thread may access the list
>> at the same time. Also reading property ClientCount is unreliable
>> since right after a read its value may already have changed.
>> Especially take care what happens when a client disconnects as
>> well as handle all possible exceptions inside the threads,
>> including component exceptions. It may help if you use a TThreadList
>> instead of current TList, at least it's easier to code thread-safe.
>> 
>> ---
>> Arno Garrels [TeamICS]
>> http://www.overbyte.be/eng/overbyte/teamics.html
>> 
>> 
>>> 
>>>> 
>>>> According to procedure Dup, the component state does not seem to be
>>>> wsClosed.
>>>> 
>>>> procedure TCustomWSocket.Dup(NewHSocket : TSocket);
>>>> var
>>>>    iStatus : Integer;
>>>>    optlen  : Integer;
>>>> begin
>>>>    ..
>>>>    if FState <> wsClosed then begin
>>>>        iStatus := WSocket_Synchronized_closesocket(FHSocket);
>>>>        FHSocket := INVALID_SOCKET;
>>>>        if iStatus <> 0 then begin
>>>>            SocketError('Dup (closesocket)');
>>>>            Exit;
>>>>        end;
>>>> 
>>>>        ChangeState(wsClosed);
>>>>    end;
>>>>    FHsocket := NewHSocket;
>>>> ..
>>>> 
>>>> ---
>>>> Arno Garrels [TeamICS]
>>>> http://www.overbyte.be/eng/overbyte/teamics.html
>>>> 
>>>> 
>>>> 
>>>> Fastream Technologies wrote:
>>>>> Hello,
>>>>> 
>>>>> I have the following function called from worker thread context
>>>>> (NewHSocket passed with a message):
>>>>> 
>>>>> function TCustomWSocketMTServer.AfterClientCreated(
>>>>>     Client : TWSocketMTClient; Error: Word; NewHSocket: integer) :
>>>>> boolean; var
>>>>>     currentClientCount: integer;
>>>>>     currentMaxClients: integer;
>>>>> begin
>>>>>     Result := TRUE;
>>>>> 
>>>>>     FCriticalSection.Enter;
>>>>>     Inc(FClientNum);
>>>>>     Client.Name            := Name + 'Client' +
>>>>> IntToStr(FClientNum);     FClientList.Add(Client);
>>>>>     currentClientCount     := FClientList.Count;
>>>>>     Client.Banner          := FBanner;
>>>>>     Client.Server          := Self;
>>>>>     currentMaxClients      := FMaxClients;
>>>>>     FCriticalSection.Leave;
>>>>> 
>>>>>     {$IFNDEF NO_DEBUG_LOG}
>>>>>     Client.IcsLogger       := IcsLogger;                         
>>>>> { V5.04 }     {$ENDIF}
>>>>> 
>>>>>     Client.Dup(NewHSocket);  // SOMETIMES WHEN HIGH LOAD IS IN
>>>>> PLACE, THIS FUNCTION MAKES CLIENT.HSOCKET = -1 AND RAISES THE
>>>>> EXCEPTION "LIST ITEM OUT OF INDEX"
>>>>>     Client.OnDataSent := nil;
>>>>> 
>>>>>     TriggerClientConnect(Client, Error);
>>>>> 
>>>>>     { Ok, the client is still there, process with the connection }
>>>>>     if (currentMaxClients > 0) and (currentClientCount >
>>>>> currentMaxClients) then begin
>>>>>         { Sorry, too much clients }
>>>>>         Client.Banner := BannerTooBusy;
>>>>>         Client.OnDataAvailable := nil; // should be set previously
>>>>> in initClient for non-503
>>>>>         Client.StartConnection;
>>>>>         Client.CloseDelayed;
>>>>> 
>>>>>         Result := false;
>>>>>     end;
>>>>> end;
>>>>> 
>>>>> Any idea?
>>>>> 
>>>>> Best Regards,
>>>>> 
>>>>> SZ
>>>> --
>>>> To unsubscribe or change your settings for TWSocket mailing list
>>>> please goto http://www.elists.org/mailman/listinfo/twsocket
>>>> Visit our website at http://www.overbyte.be
>> --
>> To unsubscribe or change your settings for TWSocket mailing list
>> please goto http://www.elists.org/mailman/listinfo/twsocket
>> Visit our website at http://www.overbyte.be
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to