The output generated by my code is ambiguous.
int main()
{
cout << "THIS IS AN THREADING EXAMPLE IN C++" << endl;
cout<<"MAIN , reverse and forward are executed"<<endl;
thread rev_thread(print_reverse,1);
thread for_thread(print_forwward,1);
rev_thread.join();
for_thread.join();
cout << "AIN , reverse and forward are DONE" << endl;
return 0;
}
void print_reverse(int wait_sec)
{
for(int rev=5000;rev>=1;rev--)
{
//std::this_thread::sleep_for (std::chrono::seconds(wait_sec));
cout<<"The rev ::" << rev<<endl;
}
}
void print_forwward(int wait_sec)
{
for(int forw=1;forw<=5000;forw++)
{
//std::this_thread::sleep_for (std::chrono::seconds(wait_sec));
cout<<"The forward ::" << forw<<endl;
}
}
I am expecting a mix of print_forwward and print_reverse in any random manner **
But here is what i am getting :-
The rev ::4997
The rev ::4996
The rev ::4995
The rev ::4994
The rev ::4993
The rev ::4992
The rev ::4991
The rev ::4990
The rev ::4989
The rev ::4988
The rev ::4987
The rev ::4986
The forward ::2
The forward ::3
The forward ::4
The forward ::5
The forward ::6
The forward ::7
The forward ::8
The rev ::4985
The rev ::4984
The rev ::4983
The rev ::4982
The rev ::4981
The forward ::The rev ::4980
The rev ::4979
9The rev ::4978
The rev ::4977
The rev ::4976
The forward ::10
The forward ::11
The forward ::12
The forward ::13
The forward ::14
The rev ::The forward ::15
The forward ::16
4975The forward ::17
The forward ::18
The forward ::19
The forward ::20
The forward ::21
The forward ::22
The rev ::The forward ::234974
The forward ::24
The forward ::25
what is 234974 all about?? and moreover the print rev and forward in one line??!!
Please notice the gaps are also printed in the same way as output!
Here I have commented out the wait for testing
Please note I am new to this concept.
Any help will be highly appreciated