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’re going to try and answer some of the most commons questions people have about multi-threading or multi-tasking in C++.

  • 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 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?

close

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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish