Re: Multithreading questions
On Sun, 22 Apr 2007 10:49:03 -0400, "Dave Cullen" <nospam@mail.com> wrote:
I'm considering using a worker thread to control machinery in a dialog based
application. The task will be repetitive, lasting about 1 second every 3 or
4 seconds.
My questions are these:
From what I've read, the worker thread must return or call AfxEndThread from
within itself.
The preferred method is just to return from your thread entry function; the
real thread entry function will call AfxEndThread for you. The problem with
calling AfxEndThread yourself is that it won't unwind the stack and destroy
local variables, which can cause memory leaks for objects that allocate
memory, lead to deadlocks when lock objects don't get a chance to release
their sync objects, etc.
What happens if the main thread terminates first? Does the
worker thread keep running?
http://members.cox.net/doug_web/threads.htm#Q2
Because you are using MFC's CWinThread, you should also read Q1 at a
minimum.
If the machine hangs and the user is forced to do something ugly like
CTRL-ALT-DEL does that kill both threads?
I'm not sure what Ctrl+Alt+Del does these days. It used to reset the
computer, and nothing survives that.
I can either allow the worker thread to run continuously and communicate
through synchronization objects, or run and terminate each time it's needed.
What's the preferred method and the tradeoffs for these choices?
Unless you're talking hundreds of times a second (or whatever threshold you
determine to be perceptible), I'd use whichever is more convenient. Based
on your problem description, I think either way would be fine.
--
Doug Harrison
Visual C++ MVP