I wrote a program in codeblocks and the code is shown below.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int Param, i, j, k;
if(argc != 2){
fprintf(stderr, "An integer parameter is required \n");
return -1;
}
Param = atoi(argv[1]);
if(Param<0){
fprintf(stderr, "An integer >= 0 is required \n");
}
printf("first \n");
for(i = 0; i < 1500; i++)
for(j = 0; j < 1500; j++)
for(k = 0; k < 1500; k++);
printf("second \n");
for(i = 0; i < 1500; i++)
for(j = 0; j < 1500; j++)
for(k = 0; k < 1500; k++);
printf("Done \n");
return 0;
}
In the program, I didn't use any multithread function and libraries like windows.h or thread.h. But when I opened the task manager to observe the threads it used, I was surprised that the program was using 2 threads. I have no idea why this can happen. Is there something added into the program I wrote while compiler is compiling my program? Please help me to find out the mystery.

This is the picture about the question.
This might be a case of implicit parallelism where your CPU/compiler will exploit instruction level parallelism (ILP for short) to improve sequential processor performance. As your two for loops are independant of each other your compiler will make use of vectorization and your processor will automatically create threads to shorten execution time. Therefore you are running 2 or even 4 threads, dependant on your compiler/system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With