Re: Threads, objects and classes
On 26 Sie, 09:39, Joseph M. Newcomer <newco...@flounder.com> wrote:
Everything should work well.
You should not require access from the main dialog to the thread contexts=
except for the
most trivial interactions. All other access should be via some kind of=
interthread
messaging. Otherwise, you are going to have to do a lot of synchroniza=
tion and this will
only lead to you getting into trouble.
Suppose I have
class CThreadOne : public CWinThread {
};
class CThreadTwo : public CWinThread {
};
with various declarations and methods in them. My own philosophy depen=
ds on what I'm
trying to accomplish:
(a) I create threads that are handed parameters and go do their work and =
die. They will
communicate with my main thread via PostMessage, and perhaps the only com=
munication is a
single notification that the thread has terminated
(b) I create service threads that are UI threads and communicate with the=
m via
PostThreadMessage; they communicate back with PostMessage
(c) I create interactive threads that accept input either via PostThreadM=
essage or an I/O
Completion Port using GetQueuedCompletionStatus, and communicate to the m=
ain thread via
PostMessage
A variant of this is that I would create a thread that would do PostMessa=
ge queue
saturation, but I have already presented an essay in which I show how to =
use an IOCP in
the OnIdle handler to dispatch messages and avoid queue saturation.
So typically I would do
CThreadOne * T1 = new CThreadOne;
T1->Create(...);
or
CThreadOne * T1 = (CThreadOne) AfxBeginThread(RUNTIME_CLASS(CThreadOne)=
, ...);
Generally I do not retain pointers to the classes, but if I do, they have=
to have a
lifetime that lasts as long as I need them, and anything that causes the =
variable to
disappear before I'm done with the thread typically will ensure the threa=
d terminates
cleanly first.
Now I can call methods such as T1->DoSomething(...) to act upon the state=
of T1, noting
that at all times, if DoSomething is callable from the main thread (which=
is usually what
I want) I will make sure that there is no concurrent access to any data t=
hat is being
manipulated. This usually means a CRITICAL_SECTION around the protecte=
d area. Deadlock
is a potential problem, whcih is why synchronization should be avoided (t=
his doesn't mean
you don't do it; it means you design your system so it is not necessary; =
if it is
necessary, you MUST do it)
Read my essays on threading on my MVP Tips site, including the one called=
"The Best
Synchronization is No Synchronization"
=
joe
On Tue, 26 Aug 2008 00:20:44 -0700 (PDT), woj...@gmail.com wrote:
Hi
I was to make multi-threading application (dialog based), but on the
beggining i was stuck because of this reason: I got one main class
(dialolg) in which i wanted to create two therads which were
declareted in classes CThreadOne and CThreadTwo (CWinThread). I still
dont know how to gain access from main dailog class to threads classes
- how it should look like ? Should I include header files from this
therad classes in main class and then create objects and use
afxbeginthread method ? Or its not the right way ?
Thx in advance for help
Joseph M. Newcomer [MVP]
email: newco...@flounder.com
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm
when in OnInitDialog() im typing : CThreadOne * T1 = (CThreadOne)
AfxBeginThread(RUNTIME_CLASS(CThreadOne),null,null,null,null);
i got error :
error C2440: 'type cast' : cannot convert from 'CWinThread *' to
'CThreadOne'
1> No constructor could take the source type, or constructor
overload resolution was ambiguous
is there any the simplest tutorial with mfc dialog based application
and multithreading ?
Thx in advance for help