From: spaace on

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;
}
From: Cezary H. Noweta on
Hello,


> 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
From: Pavel A. on
Maybe you'll be interested to see this:

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

Regards,
-- pa


"spaace" <spaace(a)gmail.com> wrote in message
news:58b7ddf1-9d9a-40bc-a543-ace2ef104264(a)u15g2000prd.googlegroups.com...
>
> 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;
> }