I have been facing this problem for not only in this code but also in a few others.
The printf function in placed in a for loop. It should print at every iteration (loop pass). But it prints everything at a stretch after the loop exits.
In the current example, i am using threading. But i have faced the same problem with other programs i have written that did not involve threading.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "threads.h"
ElementPtr RunQ;
void *loop1(void)
{
int i;
while(1)
for(i=1;i<100;i++)
{
printf("\t%d",i);
if(i%8 == 0)
{
printf("\n");
yield();
}
usleep(500000);
}
}
void *loop2(void)
{
int i;
while(1)
for(i=201;i<300;i++)
{
printf("\t%d",i);
if(i%4 == 0)
{
printf("\n");
yield();
}
sleep(1);
}
}
void *loop3(void)
{
int i;
while(1)
for(i=501;i<600;i++)
{
printf("\t%d",i);
if(i%15 == 0)
{
printf("\n");
yield();
}
usleep(500000);
}
}
int main()
{
InitQueue(&RunQ);
printf("\nRunning threads:\n");
start_thread(&loop1);
start_thread(&loop2);
start_thread(&loop3);
run();
return 0;
}
The output is exactly correct. The only thing is that it is printing every line at once, instead of printing every number one by one. So the first loop would wait 4 seconds and then print 1 to 8 all at once and then switch to thread 2 that waits 4 seconds and then prints and so on.
It should rather print one number at a time. What am I doing wrong?
P.S. I am using gcc compiler on Ubuntu 14.10