Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:13:50+00:00 2026-06-13T13:13:50+00:00

can I use the Indy Command TIdThreadSafe to make a class MyPrivateClass threadsafe if

  • 0

can I use the Indy Command TIdThreadSafe to make a class MyPrivateClass threadsafe if I define a new Class

   MyNewIndyClass = Class(TIdThreadSafe)
         FLocal :  MyPrivateClass 
         create
          ......
          end;

My MyPrivateClass is not Threadsafe as I access TList and TBitmap items inside this class

If I change the TCPServer.Onexecutde Code to to following style

    ......

     aNewIndyClass  :=  MyNewIndyClass.Create; 

     aNewIndyClass.FLocal.CallFuntionA; 

     aNewIndyClass.FLocal.CallFuntionB; 

      ......

Idea for this approach : Keep MyPrivateClass code unchange, just add the request for the Indy Server onexecute in a separate class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:13:51+00:00Added an answer on June 13, 2026 at 1:13 pm

    You should use Lock() and Unlock() methods of TIdThreadSafe class. For example in TCPServer.OnExecute() call aNewIndyClass’s methods like this:

      aNewIndyClass  :=  MyNewIndyClass.Create; 
    
      aNewIndyClass.Lock;   // This will enter TIdThreadSafe internal's Critical Section object
      try
        with aNewIndyClass do  // The code between try/finally will be "atomic"
        begin                  // i.e. no other thread will be able to do anything with your object
          ...
          FLocal.CallFuntionA; 
          FLocal.CallFuntionB; 
          ...
        end;
      finally                 // The use of TRY/FINALLY is MANDATORY when working with critical sections!
        aNewIndyClass.Unlock; // This will leave the CS
      end;
    

    Also it’s better to use properties (i.e. getter/setter) for accessing private or protected members of your MyNewIndyClass class.

    By the way if you are using Delphi 2009 and newer you can get advantage of Generics. A short example implementation of a generic thread safe class may be:

      tThreadSafeObject<T: class> = class
        private
          fObject: T;
          fCriticalSection: tCriticalSection;
        public
          constructor Create(cpObject: T);  // We expect previously created object here. We own it!
                                            // TODO: Implement ownership option?
          destructor Destroy;
          function Lock: T;
          procedure Unlock;
      end;
    
    { tThreadSafe<T> }
    
    constructor tThreadSafeObject<T>.Create(cpObject: T);
    begin
      inherited Create;
      fObject := cpObject;
      fCriticalSection := TCriticalSection.Create;
    end;
    
    destructor tThreadSafeObject<T>.Destroy;
    begin
      FreeAndNil(fObject);  // In this sample implementation fObject is owned so we free it
      FreeAndNil(fCriticalSection);
      inherited Destroy;
    end;
    
    function tThreadSafeObject<T>.Lock: T;
    begin
      fCriticalSection.Enter;
      result := fObject;
    end;
    
    procedure tThreadSafeObject<T>.Unlock;
    begin
      fCriticalSection.Leave;
    end;
    

    Usage:

    procedure foo;
    var 
      tsObj: tThreadSafeObject<tMyClass>;
    begin
     tsObj := tThreadSafeObject<tMyClass>.Create(tMyClass.Create);
     try      // In real World tsObj would be variable, accessed by different threads
       with tsObj.Lock do
       try
         // Do some atomic stuff here
       finally
         tsObj.Unlock;
       end;
     finally
       freeAndNil(tsObj);
     end 
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can use .button() to create beautiful submit buttons, but the rest of the
I can use partial template specialization inside class declaration template<class T1, class T2> struct
I can use the following command to search log files over the past 10
HBase can use HDFS as back-end distributed file system. However, their default block size
I can use default design of MVC solution. For example, controller: public class ProductController
You can use more than one css class in an HTML tag in current
We can use the web worker in HTML5 like this: var worker = new
Can the compiler make automatic use of SSE2 while optimisations are disabled? When optimisations
Can i use IdHTTP in the OnExecute Event (Indy) ? like this : procedure
Can I use the new SQL Server Data Tools that come with SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.