|
-
December 14th, 2008, 04:17 AM
#1
threading problem
Code:
#include <iostream>
#include <fstream>
#include <conio.h>
#include <process.h>
using namespace std;
unsigned int __stdcall foo(void *param)
{
while(1) cerr<<"howdeee\n";
return 0;
}
unsigned int __stdcall foo2(void *param)
{
while(1) cerr<<"NOOOOOOOOO\n";
return 0;
}
void main()
{
_beginthreadex(NULL,0,foo,NULL,0,0);
_beginthreadex(NULL,0,foo2,NULL,0,0);
while(1) cerr<<"FORk\n";
}
I have core quad computer so threads can run in parallel on different cores, but what I'm getting as an output is:
howdeee
FORk
NOOOOOOOOO
in this pattern repeating for ever, thats not what I expexted I expected them to be randomly printed mabe two howdeee then a FORk then NOOOOOOOO then maybe a FORk ect... since threads run in parallel :S. but as is it seems they are running serially foo then main then foo2 any ideas of why this is happening?
thanks in advance
Last edited by compuKidd; December 14th, 2008 at 09:54 AM.
Reason: spelling mistake
-
December 14th, 2008, 11:21 AM
#2
Re: threading preoblem
Well Try
Code:
//call this in your code to start the thread
AfxBeginThread(myfctn,"");
//...definition of your function
UINT myfctn(LPVOID pParams)
{
while (1)
{
//do your printing here
Sleep(100);
}
return 0;
}
ahoodin
To keep the plot moving, that's why.

-
December 14th, 2008, 12:59 PM
#3
Re: threading preoblem
You are using the multi-threaded version of the CRT - which provides synchronization to individual streams internally. Try changing one of the "cerr" to "cout".
gg
-
December 14th, 2008, 02:11 PM
#4
Re: threading preoblem
What library should I include?
-
December 14th, 2008, 02:20 PM
#5
Re: threading preoblem
wow when I replaced cerr with cout hell broke at my program 
outputs are not only random they are also scrambled between each other, here is a sample:
hFoOwRdke
eFeO
RkN
OFOOOROkO
OFOOOROk
hFoOwRdke
eeF
ONROkO
OFOOOROkO
OFOO
Rhko
wFdOeReke
FONROkO
OFOOOROkO
OFOO
Rkh
oFwOdReke
eF
ORNkO
OFOOOROkO
OOFOO
Rkh
oFwOdReke
eF
ORNkO
OOFOOOROkO
OFOO
Rkh
oFwOdReke
even words between each other got scrambled, any one knows why this happened with cout and didn't in cerr????
thanks "Codeplug"
and one more thing is it better to use _beginthreadex or AfxBeginThread? I read some threads that talked about this but none got conclusive statements
thanks
-
December 14th, 2008, 03:12 PM
#6
Re: threading preoblem
If all statements use cout, then the output looks the same as if all were cerr - for me (VS2008).
If I mix cerr and cout, then the characters from each can be intertwined. But that's just how my implementation behaves. You shouldn't "expect" anything - since threading isn't in the current standard.
AfxBeginThread is part of the MFC framework. _beginthreadex is part of the MS C-Runtime. If your application uses MFC, use AfxBeginThread, otherwise _beginthreadex.
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|