Re: Bad practice with an MFC thread? How can I improve it?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sun, 20 Apr 2008 16:37:57 -0400
See below...
On Thu, 17 Apr 2008 14:56:04 +0100, "Moschops" <moschops@xxxxxxxxxxxxxxxxxxxxx> wrote:
I've had to alter some code I was handed. A live camera was constantly****
filling a ring buffer with images, and on a given signal (which I've put in
as a button push for the moment) it stopped, leaving the last X images in
the buffer. The requirement came in such that it would run for X/2 more
frames after the button was pushed (i.e. when you push the button, the saved
video sequence is centred at the time of pushing the button).
Initially, I added a frame counter after the button was pushed, but found
that it caused the processor to spend all the time on counting and no more
time on updating the frames, so in essence it hung. Ideal time for threads,
I decided.
So now pushing the button calls an AfxBeginThread function, like this:
// Spawn thread to decide when to stop capturing
CWinThread* pStopCaptureThread;
pStopCaptureThread = AfxBeginThread (
fnStopCaptureThread,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
Now, I understand that the fnStopCaptureThread function cannot be part of
the class within which this is all happening,
That is ABSOLUTELY not true!!!! I typically make it a class member. Key here is that it
must be a 'static' class member.
*****
so it's a global function.*****
A global function is so retro.
See my essay on worker threads. If you need to access members of the class, the typical
technique is as you have none, passing 'this' as the thread parameter
****
However, I still need access to various class variables to do my****
calculations, so that global function simply calls another function within
the original class object; the fnStopCaptureThread knows what object called
it because it is passed a 'this'.
Here's the body of that global function:
UINT fnStopCaptureThread(LPVOID lParam)
What are those noise characters 'fn' at the start? If this made sense, EVERY function
would have to start with 'fn', and that is not common practice. But make it a static
member of your class!
****
{****
// Run the stopcapture code
Sequencer_class* dlg2 = (Sequencer_class*)lParam; // This kind of casting
is not the C++ way. Needs fixing. Moschops.
dlg2->StopCapture();
Yes, this is the standard technique.
****
return 0;****
}
StopCapture is the function, within the class, that handles counting frames
and working out when to stop. This all seems happy now, and it behaves as
expected thus far.
Looking at my code, I am aware that I've now got a thread playing with my
class object, and the code that spawned that thread is also still playing
with the exact same object, albeit never calling the StopCapture function.
I'm got this feeling that it's a bad idea to have in essence two threads
accessing the same object - presumably I won't be able to predict when
they'll do things relative to each other and could end up with problems.
Nobody cares if you are playing with the "same object" and in fact that is largely
irrelevant. What IS relevant is that you must not play with the same DATA in that object.
So if you are manipulating the same data values from both threads, you are going to be in
trouble. Don't do it without synchronization. Better still, don't do it at all.
****
*****
This code section is quite simple and the variables StopCapture() is playing
with aren't touched by anything else until the thread is killed, but
nonetheless is seems like bad practice.
Multithreading only cares about individual memory locations being accessed concurrently.
Nobody actually cares what syntax is used to create those locations. If the locations are
not accessed concurrently by two or more threads, there is no issue of practice involved
at all, because the values are independent.
****
My MFC experience is quite limited****
(I'm usually an embedded coder) so I'm not sure if I'm seeing things, or if
this is a bad idea. Can anyone help me out? Is this bad practice, and if so,
what's a better way to do it?
The proper way to handle this depends a lot on what you are manipulating and how you are
manipulating it, and at what frequency (high bandwidth vs. low bandwidth), how large the
data is that needs concurrent access, and a lot of other parameters. Without detailed
knowledge of what is going on, there is no one answer that can solve your problem.
joe
*****
Joseph M. Newcomer [MVP]
'Chops
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Bad practice with an MFC thread? How can I improve it?
- From: Moschops
- Re: Bad practice with an MFC thread? How can I improve it?
- References:
- Bad practice with an MFC thread? How can I improve it?
- From: Moschops
- Bad practice with an MFC thread? How can I improve it?
- Prev by Date: Re: flex grids
- Next by Date: Re: oblique text
- Previous by thread: Re: Bad practice with an MFC thread? How can I improve it?
- Next by thread: Re: Bad practice with an MFC thread? How can I improve it?
- Index(es):
Relevant Pages
|