When does asynchronous, simultaneous, or parallel programming make sense?

The OS has a Scheduler that will allow running these threads when the CPU has the availability to do it. Is not a guarantee that the treads will be running in the order that you started or expected. These threads are not running in a real parallel way. Only asynchronous for one CPU core. A parallel running of these threads will only be real if there existing multiple CPU cores. One dedicated server machine, like a gateway login for distributed data application, may have tens of CPU cores.
Is obvious that managing the process and threads in a programmatic way is useful only when you know exactly what you have to do. It’s a must to understand exactly the demanding applications and workloads. Also, the calculus of the Big O(Omega,Theta ,whatever needed) for time complexity can help to properly dimension how many threads need to implement successfully your algorithms.
Anyhow if you have tasks to run in a number of threads less than the CPU cores number is not a good idea to reschedule the threads by setting the affinity to a specific CPU.
My example is for 8 threads managed on 4 CPU cores running under Windows 10 OS. The first 4 threads will run in parallel and the last 4 in asynchronous mode. I created two similar functions. Only the names and the thread priority are different to highlight the usage purpose of these two types of running threads: parallel and asynchronous. So, one is named ExecParallelTask, and the other one, ExecAsyncTask.
To check and have detailed information about processes and threads i use the Process Explorer tool ( https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer ).

The data struct used like argument functions and the formal declaration of functions.

In the current thread first thing we must need to know is the Ideal CPU core assignation by the OS.
GetThreadIdealProcessorEx() – Giving the CPU core number of the Ideal Processor for the current thread set by the OS.
SetThreadIdealProcessor() – Sets one CPU core to the OS Threads Schedule. The system schedules Threads on their preferred processors whenever possible and we need to change that. We consider that our server machine is special dedicated for a special purpose
SetThreadAffinityMask() – After we suggested to the OS what CPU core it should be use , now we actually set the current thread at the needed CPU core. Sets a CPU core affinity mask for the current thread.
SetThreadPriority() – Sets the priority value for the current thread. This value, together with the priority class of the thread’s process, determines the thread’s base priority level.

After all this settings it doing 10 millions of iteration apply to a simple math expression to keep busy the set CPU core. Before to exit from the current thread is shown how much time was consumed for all iterations in milliseconds

The same thing also for the ExecAsyncTask() function that run in a thread with a Lowest thread priority
In the main() function initialize one MyParam data structure for 8 threads. The threads will run in parallel by 4 and asynchronous by 2 . This pair of tasks, “Parallel Task 1″ and,”Async Task 1” will run asynchronous threads on CPU core 1 . But will run in parallel threads with other such pairs of tasks. After checking of how many CPU cores are available for the current process and for the OS, i initialize one overall process time counter for all running threads.
Finally i declare 8 variable for joinable threads and start running.
And the result is what we expected,

The result are much more spectacular when is need to run for tens or hundred of threads. But also is a big challenge . This kind of multi-threads handling methode is recommended only for dedicated server machines.
See you at the next article about pthread running under Linux OS .
Please follow up if you are interested.