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);
}