RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TThread Class

TThread is an abstract class that enables creation of separate threads of execution in an application.

Pascal
TThread = class;
C++
class TThread;

Classes

Create a descendant of TThread to represent an execution thread in a multi-threaded application. Each new instance of a TThread descendant is a new thread of execution. Multiple instances of a TThread derived class make an application multi-threaded.  

When an application is run, it is loaded into memory ready for execution. At this point it becomes a process containing one or more threads that contain the data, code and other system resources for the program. A thread executes one part of an application and is allocated CPU time by the operating system. All threads of a process share the same address space and can access the process's global variables. 

Use threads to improve application performance by

  • Managing input from several communication devices.
  • Distinguishing among tasks of varying priority. For example, a high priority thread handles time critical tasks, and a low priority thread performs other tasks.

Following are issues and recommendations to be aware of when using threads:

  • Keeping track of too many threads consumes CPU time; the recommended limit is 16 active threads per process on single processor systems.
  • When multiple threads update the same resources, they must be synchronized to avoid conflicts.
  • Most methods that access an object and update a form must only be called from within the main thread or use a synchronization object such as TMultiReadExclusiveWriteSynchronizer.

Define the thread object's Execute method by inserting the code that should execute when the thread is executed.

Note: See the Threads demo for multi-threading program samples.
 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!