Site icon 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.

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:

[crayon-673f59f482ad2746593380/]

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:

[crayon-673f59f482adf007854873/]

We can start multiple tasks as below,

[crayon-673f59f482ae2177392806/]

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.

[crayon-673f59f482ae4577807702/]

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.

[crayon-673f59f482ae6134293164/]

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

Exit mobile version