C++C++17Introduction to C++Learn C++

How To Harness The Power Of Multi-Threading In C++

How To Harness The Power Of Multi Threading In C++

Sooner or later the subject of multi-tasking or multi-threading comes up when programming. We’ll try to answer some of the most common questions about multi-threading or multi-tasking in C++, because multi-tasking can be useful in the future when developing C++ applications with the C++ IDE.

  • What is a task?
  • What do we mean by multi-tasking?
  • What is a thread?
  • What is multi-threading?
  • Why do we need the TTask class?
  • Why do we need to use a thread?
  • How to implement tasks
  • How to implement a thread
  • Differences between a task and a thread
  • What are the differences between parallel programming and multi-tasking?
  • Can we use multi-threading when drawing?

Use the C++ Builder Parallel Programming Library for multi-tasking

The Parallel Programming Library (PPL) provides a TTask class to run one task or multiple tasks in parallel. A Task is a unit of work you need to get done. The PPL does the association between the task and the thread that performs the task so you can run several tasks in parallel without having to create your own custom threads and managing them.

There is thread-associated classes in System.Threading.  The Thread is a small set of executable instructions. When the time comes when the application is required to perform few tasks at the same time.

Task is an object that represents some work that should be calculated by a thread. Tasks class to let you create tasks and run them asynchronously. The task can tell you if the work is completed and if the operation returns a result, the task gives you the result .can be used whenever you want to execute something in parallel. Asynchronous implementation is easy in a task, using’ async’ and ‘await’ keywords.

TTask creates and manages interaction with instances of ITaskITask is an interface that provides a range of methods and properties to StartWaitCancel and also a property for Status (Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception).

How can I prevent my program’s user interface from becoming unresponsive in C++?

One functionality of TTask is to prevent the user interface from locking up if you want to start something in the background. The following code example shows how to run a single task and start it:

What is the difference between a

Here are some differences between a task and a thread in C++

  1. The Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.
  2. The task can return a result. There is no direct mechanism to return the result from a thread.
  3. Task supports cancellation through the use of cancellation tokens. But Thread doesn’t.
  4. A task can have multiple processes happening at the same time. Threads can only have one task running at a time.
  5. We can easily implement Asynchronous using ’async’ and ‘await’ keywords.
  6. A new Thread()is not dealing with Thread pool thread, whereas Task does use thread pool thread.
  7. A Task is a higher level concept than Thread.

CLANG Enhanced Compilers

If we are using a Clang-enhanced C++ compiler, we can use lambda expressions:

Using multi-threading for any C++ Compiler

For any C++ compiler, we can declare a TProc subclass and use an instance of it casted as _di_TProc:

We can start multiple tasks as below,

How to use multi-threading in bitmaps and/or in canvas,

TCanvas and all subclasses have support for multi-threaded environments, but does not support simultaneous execution of Canvas instances. This means that when a Canvas calls BeginScene, the execution of the code inside this BeginScene...EndScene section blocks any other thread that attempts to process drawing, including drawing on other canvases.

Write BeginScene...EndScene blocks as short as possible to avoid blocking other threads.

TBitmap has complete multi-threading support. Instances can be created, destroyed, and modified in any thread without synchronization. It has also Canvas property that can be used to draw drawings into images with multi-threading.

Note: You will still require normal thread synchronization if you share objects between threads, to ensure an object is not deleted while another thread is using it. Also, be cautious adding your own synchronization around bitmap access. It can cause deadlocks.


RAD Studio C++ Builder has everything you need to get programming with C++. Why not download a free trial today?

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?