Programming C++Builder How to write multi-threaded applications in CBuilder--define the thread objects
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to write multi-threaded applications in C++Builder?

C++Builder provides several objects that make writing multi-threaded applications easier. Multi-threaded applications are applications that include several simultaneous paths of execution. While using multiple threads requires careful thought, it can enhance your programs by:

  1. Avoiding bottlenecks. With only one thread, a program must stop all execution when waiting for slow processes such as accessing files on disk, communicating with other machines, or displaying multimedia content. The CPU sits idle until the process completes. With multiple threads, your application can continue execution in separate threads while one thread waits for the results of a slow process.
  2. Organizing program behavior. Often, a program's behavior can be organized into several parallel processes that function independently. Use threads to launch a single section of code simultaneously for each of these parallel cases. Use threads to assign priorities to various program tasks so that you can give more CPU time to more critical tasks.
  3. Multiprocessing. If the system running your program has multiple processors, you can improve performance by dividing the work into several threads and letting them run simultaneously on separate processors.

Note: Not all operating systems implement true multi-processing, even when it is supported by the underlying hardware. For example, Windows 9x only simulates multiprocessing, even if the underlying hardware supports it.

The following topics discuss support for threads in C++Builder:

  1. Defining thread objects
  2. Coordinating threads
  3. Executing thread objects
  4. Debugging multi-threaded applications 

First step, how to define thread objects?

For most applications, you can use a thread object to represent an execution thread in your application. Thread objects simplify writing multi-threaded applications by encapsulating the most commonly needed uses of threads.

NoteThread objects do not allow you to control the security attributes or stack size of your threads. If you need to control these, you must use the Windows API CreateThread or the BeginThread function. Even when using Windows Thread API calls or BeginThread, you can still benefit from some of the thread synchronization objects and methods described in Coordinating threads.

To use a thread object in your application, you must create a new descendant of TThread. To create a descendant of TThread, choose File|New|Other from the main menu. In the New Items dialog box, double-click Thread Object (or CLX Thread Object for cross-platform applications) and enter a class name, such as TMyThread. To name this new thread, check the Named Thread check box and enter a thread name (VCL only). Naming your thread makes it easier to track the thread while debugging. After you click OK, C++Builder creates a new .cpp and header file to implement the thread. For more information on naming threads, see Naming a thread.

Note: Unlike most dialog boxes in the IDE that require a class name, the New Thread Object dialog box does not automatically prepend a "T" to the front of the class name you provide.

The automatically generated .cpp file contains the skeleton code for your new thread class. If you named your thread TMyThread, it would look like the following:

//---------------------------------------------------------------------------

#include
#pragma hdrstop
#include "Unit2.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TMyThread::TMyThread(bool CreateSuspended): TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
  // ---- Place thread code here ----
}
//---------------------------------------------------------------------------

In the automatically generated .CPP file, you


Add comment


Security code
Refresh

Programming - C++Builder