Search code examples
c++newlinecoutflush

cout not printing string and variable value consistently , misaligning the output


In the following code threadCount is one of 1,2,3,4 . But in the output, though the string part getting printed perfectly the num value getting missed randomly and it's getting appended after a few lines at times.

void *SPWork(void *t)
{

    int* threadC = (int*)t;
    int threadCount = *threadC;
    cout<<"\n Thread count" << threadCount << endl;
    cout << flush;
    long long int i, adjustedIterationCount;
    adjustedIterationCount = 100/(threadCount);
    for (i=0; i< adjustedIterationCount; i++)
    {
        i++ ;
    }
    pthread_exit((void*) t);
}

Output

......
.....
Thread count1
 Thread count1
 Thread count2
 Thread count1
 Thread count
 Thread count
 Thread count234
 .....
 .....

Notice in the last line thread value is 234. But that value will never be 234.In the previous 2 line that value didn't get appended and so 2,3 got added to this line.

I know it got to do with flush or appending "\n", tried many combinations. But still, the issue persists.

N.B. This is a worker method of a pthread, compiler flags are "-g -Wall -O3 -lpthread"


Solution

  • While the standard streams are guaranteed to be thread-safe, there is no guarantee that the output won't be interleaved. If you want to print to a standard stream from multiple threads in a predictable way, you will need to do some synchronization yourself:

    std::mutex cout_mutex;
    
    void *SPWork(void *t)
    {
        //...
        {
            std::lock_guard<std::mutex> guard(cout_mutex);
            std::cout << "\n Thread count" << threadCount << std::endl;
        }
        //...
    }