C++Builder This Is How To Use Parallel Programming in C++ Builder

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
This Is How To Use Parallel Programming in C++ Builder
By Yilmaz Yoru November 30, 2021

Do you want to learn what is Parallel Programming? Do you want to speed up your heavy calculation methods? Let’s see how it works.

What is parallel programming in C++?​

Parallel Programming is generally used to solve heavy calculation problems such as real time analysis of multi dimensional data, image processing, calculations on fluid mechanics, thermodynamics and other engineering problems. Parallel Programming is a method which uses multiple computational resources, processors or processing by groups of servers. Generally in this type of programming, it takes a problem, breaks it down into a series of smaller steps, delivers instructions, and processors execute the solutions of each parts at the same time in different Threads, CPU Cores, CPU’s, GPUs.

How does C++ Builder help with parallel programming in C++?​

In C++ Builder, the Для просмотра ссылки Войди или Зарегистрируйся provides the Parallel Programming Library (PPL), giving your applications the ability to have tasks running in parallel taking advantage of working across multiple CPU devices and computers. The PPL includes a number of advanced features for running tasks, joining tasks, waiting on groups of tasks, etc. to process. For all this, there is a Для просмотра ссылки Войди или Зарегистрируйся that self tunes itself automatically (based on the load on the CPU’s) so you do not have to care about creating or managing threads for this purpose.

You can use this library by including Для просмотра ссылки Войди или Зарегистрируйся in your apps. This unit is made up of several features that can be included into new and existing projects. The unit also includes a number of overloaded arguments to make it suitable for C++ as well as Delphi.

Using the PPL, your applications can easily:

Is there an example of using TParallel in C++ Builder?​

This is a very basic TParallel::For() syntax,

C++:
TParallel::For(0, 10, TProc1<int>([](int I){
  // …
}));
This is a simple TParallel::For() example. In this example we used calculate() procure in 32 multi tasks. That means calculate() function can run 32 times simultaneously in different threads of your CPU.
C++:
#include <vcl.h>
#include <windows.h>
#include <iostream>
#include <chrono>
#include <System.Threading.hpp> // parallel library
 
#pragma hdrstop
#pragma argsused
 
double calculate(int I)
{
 double t=0;
 for(int i=1; i<100000; i++)
 {
 for(int j=1; j<10000; j++)
 {
 t+=1+j/i;
 }
 }
 std::cout << I << ",";
 
 return t;
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
 
 TParallel::For(0, 32, TProc1<int>([](int I)
 {
 calculate(I);
 }));
 
 Sleep(5000);
 std::cout << "done";
 getchar();
 
 return 0;
}