Discussion:
Does Visual Studio 2008 synchronize printf
(too old to reply)
spaace
2010-03-29 13:21:50 UTC
Permalink
Hi,

i was trying to teach the concepts of threading to someone, and i
couldnt get the typical jumbled output, one usually sees when printing
from 1-n from 2/3 threads.

I could see the jumbled output in VS 2005/XP combination but the other
person running VS2008/Vista could not see it. I have posted the small
code below ... i tried flushing the output and running 2 more
threads .. but i could not get a jumbled output in Vista

I have not stayed upto date on vista / VStudio diffferences for a long
time ... wondering if someone could explain what is affecting the
Vista runs.


Thanks in Advance
Rgds
Arun


#include "stdafx.h"
#include <iostream>
using namespace std;
#include <process.h>


void Mythread ( void * arg )
{
for(int i = 0; i < 10; i++) printf("\n %d", i);
}

int main(int argc, char * argv[])
{

_beginthread(Mythread, 0, NULL);

Mythread(NULL);

return 0;
}
Cezary H. Noweta
2010-03-29 13:54:21 UTC
Permalink
Hello,
Post by spaace
I have not stayed upto date on vista / VStudio diffferences for a long
time ... wondering if someone could explain what is affecting the
Vista runs.
_beginthread(Mythread, 0, NULL);
Mythread(NULL);
return 0;
Probably the main process has exited before the first ,,printf()'' of
the new thread is executed. Try the following:

HANDLE hThread;

hThread = (HANDLE)_beginthread(Mythread, 0, NULL);

Mythread(NULL);

WaitForSingleObject(hThread, INFINITE);

Try to use _beginthreadex() instead of _beginthread(). Older libraries
(before VS2005) can return invalid thread handle. _beginthreadex()
ensures that the returned thread handle can be used in Wait...() functions.

hThread = (HANDLE)_beginthreadex(NULL, 0, Mythread, NULL, 0, NULL);

and change Mythread to:

unsigned __stdcall Mythread(void *arg)

If you want a really jumbled output then append some Sleep() to your
for() loop:

for(int i = 0; i < 10; Sleep(10), i++)

otherwise you can get all the output of the main thread before the
output of the second thread.
--
-- best regards

Cezary H. Noweta
Pavel A.
2010-03-29 20:11:50 UTC
Permalink
Maybe you'll be interested to see this:

http://research.microsoft.com/en-us/projects/chess/

Regards,
-- pa
Post by spaace
Hi,
i was trying to teach the concepts of threading to someone, and i
couldnt get the typical jumbled output, one usually sees when printing
from 1-n from 2/3 threads.
I could see the jumbled output in VS 2005/XP combination but the other
person running VS2008/Vista could not see it. I have posted the small
code below ... i tried flushing the output and running 2 more
threads .. but i could not get a jumbled output in Vista
I have not stayed upto date on vista / VStudio diffferences for a long
time ... wondering if someone could explain what is affecting the
Vista runs.
Thanks in Advance
Rgds
Arun
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <process.h>
void Mythread ( void * arg )
{
for(int i = 0; i < 10; i++) printf("\n %d", i);
}
int main(int argc, char * argv[])
{
_beginthread(Mythread, 0, NULL);
Mythread(NULL);
return 0;
}
Loading...