Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cout doesn't work with pthreads?

I'm trying out a simple program to test multi-threading. I just print a series of "x" and "O" in alternate threads. Now, if I use cout , no output is seen on screen. If I use fputc and output to stderr , it works fine. Why is cout (output to stdout) not working here ?

My code is given below :

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

using namespace std;

static int count;

void* print_xs(void *unused)
{
    while(1)
    {
        if (count >=100) break;
        if (count%2==0)
        {
            count++;
            cout<<"X=";  // no output here
            fputc('X',stderr); // works !
        }
        else
        {
            sleep(1);
        }
    }
    return NULL;

}


int main()
{
    pthread_t tid;
    pthread_create(&tid,NULL,&print_xs, NULL);

    while(1)
    {
        if (count >=100) break;
        if (count%2!=0)
        {
            count++;
            cout<<"O="; // no output here 
            fputc('O',stderr); // works !
        }
        else
        {
            sleep(1);
        }
    }

    pthread_join(tid,NULL);
    return (0);

}
like image 273
vinit Avatar asked Aug 30 '26 04:08

vinit


1 Answers

Since std::cout is a buffered-stream, you need to flush it in order to send the buffer to the standard output.

Just try something like:

cout<< "O=";
cout.flush();

That should work.

Additional notes

  1. As some comments have already suggest you, std::cout is not thread-safe in C++03 and before. It could be useful to protect that object with a mutex. This could be not a problem since C++11 standard.

The FDIS says the following in §27.4.1 [iostream.objects.overview]:

Concurrent access to a synchronized (§27.5.3.4) standard iostream object’s formatted and unformatted input (§27.7.2.1) and output (§27.7.3.1) functions or a standard C stream by multiple threads shall not result in a data race (§1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. — end note ]

That means without mutex it's guaranteed the object will not corrupted in a data-race context. But the problem on overlapped output remains. So if you be sure each line is printed without overlap by another thread, you still need a mutex.

  1. C++11 has introduces a multi-thread library (often is a wrapper for pthread). Here some reference. Just take a look at, you could find it useful.
like image 93
BiagioF Avatar answered Aug 31 '26 20:08

BiagioF