From: Jack on
Hello,
Could I ask one further question before I move on...
I find out that the thread returns and exits
immediately after the run call.
Are there any ways to run the thread infinitely
without limit?
Thanks
Jack


From: Jack on
Where should be the best place to put the join() call?
I tried WM_DESTROY, but it didn't run infinitely.
I tried to put it under the last statement of the method,
And the program just hanged. Any other options?
Thanks
Jack


From: Ulrich Eckhardt on
Jack wrote:
>> Are you by any chance using Thread object on the stack?
>
> Ummm.... I set out the thread as a local variable of the method, I vaguely
> remember one of you said that although the var is on the stack, the thread
> itself should be on the heap... no? correct me if I am wrong...

Sorry, but the terms "on the stack" or "on the heap" don't apply to a
thread, just as you can't say that about a file. These are only meaningful
for C++ objects.

Now, concerning your class object, no, it is not on the stack. However, its
lifetime is bound to the auto_ptr object on the stack, which might lead to
problems if it is still used by the created thread while the creating
thread has already destroyed the object.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Jack on
Thanks guys for your efforts!
Now my coding looks like this:

switch(uMessage)
{
case WM_CREATE:
hEvent = CreateEvent(NULL,FALSE,FALSE,"Test");
break;
case WM_COMMAND:
// the thread is starting to run here athread->run(); by a push of
a button
break;
case WM_DESTROY:
result1 = reinterpret_cast<int>(thread1->join());
PostQuitMessage(0);
break;
....

////////////////////////////////////////
I have removed the thread as a local variable and put it in the "global"
area.
like this

HANDLE hEvent;
std::auto_ptr<BFThread> thread1(new BFThread(1));

//////////////////////////////////////
I can see the thread run, but it terminates instantly.
Hello <<<<<<<<< output of the thread
The thread 'Win32 Thread' (0x718) has exited with code 1 (0x1).
The program '[3808] GridPartition.exe: Native' has exited with code 0 (0x0).
//////////////////////////////////

I consider it to be very wrong. But don't know how to handle it....
Could anyone lend me a hand?
Thanks
Jack


From: Tim Roberts on
"Jack" <jl(a)knight.com> wrote:
>
>Could I ask one further question before I move on...
>I find out that the thread returns and exits
>immediately after the run call.
>Are there any ways to run the thread infinitely
>without limit?

The thread will run until your function returns, then it exits. If you
need it to do work, then code the thread so it does work.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.