[Solved] Is there a way to run in parallel completeley unrelated tasks in C/C++

Introduction

Yes, it is possible to run completely unrelated tasks in parallel in C/C++. This can be done using a variety of techniques, such as multi-threading, asynchronous programming, and message passing. Each of these techniques has its own advantages and disadvantages, and can be used to achieve different levels of parallelism. In this article, we will discuss the various ways to run unrelated tasks in parallel in C/C++, and the pros and cons of each approach.

Solution

Yes, it is possible to run completely unrelated tasks in parallel in C/C++. This can be done using multi-threading, which allows multiple threads of execution to run concurrently. Multi-threading can be implemented using the POSIX Threads library (pthreads) or the C++11 thread library. Additionally, there are other libraries such as OpenMP and Intel Threading Building Blocks (TBB) that can be used to facilitate multi-threaded programming.


You can try with OpenMP tasks. Open a parallel block and create two task blocks in it. Something like this:

#pragma omp parallel
{

   #pragma omp task {
      // hello world code 
   }
   #pragma omp task {
      // connect mysql
   }
}

Add the -fopenmp option to your compilation line. There might be better approaches than that, as it requires you putting all the code in the same code block. Anyway that’s quite an easy solution with minimal code addition.

1

solved Is there a way to run in parallel completeley unrelated tasks in C/C++