Re: _beginthreadex not compiling at all - what am I doing wrong?
On 16 Aug., 02:51, Joseph M. Newcomer <newco...@flounder.com> wrote:
See below...
On Wed, 11 Aug 2010 14:22:51 +0300, Timothy Madden <terminato...@gmail.com> wrote:
Uwe Kotyczka wrote:
This is driving me mad, it seems impossible to give
_beginthreadex correct parameters. To reproduce the
problem I just created a new console project from scratch
an added this code:
unsigned __stdcall ThreadProc(LPVOID lpV)
{
static int aaa = *(int*)lpV;
****
Never, ever, for any reason, use a static variable inside a function body, unless it is
preceded with the word 'const'. I have no idea what this code is intended to do, but it
is somewhere between completely silly and totally erroneous. There is no reason to make
this variable stgatic, and since this is a thread procedure, making it static is outright
wrong. Make it a local variable!
****
Joe, you are right, of course.
Let me explain why I posted this code. I am just new to some existing
code.
When taking a look at it I found that there are rather strange things.
The code does not use MFC BTW.
First I found that threads are created by CreateThread though the
thread procedures
do use CRT functions. and there are even a lot of TerminateThread
calls in the code.
So I think that should be redesigned. (Of course, if you are new at a
job, you shouldn't
tell "This code is bullshit", but rather a polite "Something should be
improved here.")
Another thing is the use of static variables. They are also heavily
used. And I found
that VC6 (which has to be used to maintain the code) does not always
seem to debug
static variables correctly. I asked that in another posting. To make
sure that is a
VC6 problem and not a problem from the code I just created a new
console project from
scratch to see the debugger's static variable issue.
Next thing was that it seemed a problem to replace CreateThread by
_beginthreadex.
But it was my fault. I just had not seen that _beginthreadex casts the
thread handle
to unsigned long and that I have to recast it to HANDLE. I saw it
shortly after I posted
the question.
So the code I posted is meaningless in any way. I was just "playing
around" to find
out what the compiler was complaining about.
for (int i = 0; i < 30; i++)
aaa += 2;
return 0;
}
int main(int argc, char* argv[])
{
int start = 102;
unsigned nId;
HANDLE hThread = _beginthreadex(NULL,
0,
ThreadProc,
&start,
0,
&nId);
WaitForSingleObject(hThread, INFINITE);
****
This makes no sense. If you are going to start a thread and wait for it to finish, you
should just to a procedure call. All you have done here is implement a clumsy and
expensive equivalent to a procedure call
****
Of course it does not make sense. I only wante to find out if the
thread handle was
valid.
CloseHandle(hThread);
return 0;
}
I always get a C2440 error and have no idea why.
*****
I have no idea either. Presumably there is other text with this message. Since I have
not troubled to memorize the error message codes, I refuse to answer any question which
does not give the error code text.
****
VC6 is complaining "cannot convert 'unsigned long' to 'void*'",
*****
Yes, that is correct. What is the type of the argument of the thread function? LPVOID?
Then why in the world would you think it could possibly make sense to pass an int as that
value? You might consider an
(LPVOID)
cast on an integer.
****>> VS2003 is complaining "cannot convert from 'uintptr_t' to 'HANDLE'".
****
That sounds right, too. What is the return type from _beginthreadex? It is most
certainly NOT a HANDLE type (RTFM!), it is a uintptr_t, AS DOCUMENTED, so why would you
think it could be used as a HANDLE? It can be CAST to a HANDLE, but it cannot be USED as
a HANDLE. This is pretty basic C language stuff! Elementary C knowledge.
NOte that the return type of CreateThread is NOT a uintptr_t! So OF COURSE It works,
because the return type of CreateThread is (AS DOCUMEN TED) a HANDLE. Why would you think
_beginthreadex returns a HANDLE type when it is CLEARLY DOCUMENTED as returning a
uintptr_t?
*****
My fault, as I mentioned above. (awkward)
Using CreateThread instead of _beginthreadex (with slightly
different parameters) works fine.
****
No, it doesn't. It fails to initialize the C runtime library, and therefore its use is
erroneous.
****
Again not precisely. Should have written: seems to work at all.
Can anyone tell me what i am doing wrong?