Unexpected output in threads in C++

Viewed 65

I am new to threading and I wrote a simple thread program to see how it works in C++.

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

void funcSum(long long int start, long long end)
{
    long long int sum = 0;
    for(auto i = start; i <= end; ++i)
    {
        sum += i;
    }
    cout << sum;
}
int main()
{
    auto startTime = high_resolution_clock::now();
    long long int start = 0, end = 90000;
    thread t1(funcSum, start, end);
    t1.join();
    funcSum(start, end);
    auto stopTime = high_resolution_clock::now();
    auto duration = duration_cast<seconds>(startTime - stopTime);
    cout << "\n" << duration.count() << " Seconds\n";
    return 0;
}

The above code produces some random output: 40500450004050045000 0 Seconds

What is wrong in this code? Why I am getting a random garbage number? I am expecting it to print only the sum of numbers from 0 to 90000.

2 Answers

Notice that you are running funcSum twice. Once it runs in a new thread, then again after the thread completes (when join() finishes).

In funcSum, you forgot to add a new line. Both executions print 4050045000 as expected but the result is not separated, so it may look like garbage.

This should fix the problem:

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

void funcSum(long long int start, long long end)
{
    long long int sum = 0;
    for(auto i = start; i <= end; ++i)
    {
        sum += i;
    }
    // add new line to make output more readable
    cout << "Calculated sum: " << sum << "\n";
}
int main()
{
    auto startTime = high_resolution_clock::now();
    long long int start = 0, end = 90000;
    thread t1(funcSum, start, end);
    t1.join();
    // no need to call funcSum again, it will be called in t1 anyways
    auto stopTime = high_resolution_clock::now();
    auto duration = duration_cast<seconds>(startTime - stopTime);
    cout << "Duration: " << duration.count() << " Seconds\n";
    return 0;
}
  1. Your function output is: 4050045000 and called twice in the code. Because of you did not use from "std::endl" or "\n" when use of "cout", both of results are in same line. so first modify function:
cout << sum << endl;

or

cout << sum << "\n";
  1. For positive result that is meaningful than negative:
 auto duration = duration_cast<milliseconds>(stopTime - startTime);
  1. For calculation thread time you should taked time before start it and stop timing after thread.join(). Because of thread time in this case is very little, using "second" is not proper and it's better to use of "millisecond".
    long long int start = 0, end = 90000;
    
    auto startTime =  high_resolution_clock::now();
    thread t1(funcSum, start, end);
    t1.join();
    auto stopTime = high_resolution_clock::now();
    
    auto duration = duration_cast<milliseconds>(stopTime - startTime);
    cout << "Duration: " << std::chrono::milliseconds(duration).count() << " Milliseconds\n";
    cin.get();
Related