 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
nicolasr Guest
|
Posted: Thu Aug 14, 2003 10:59 pm Post subject: Dead lock in Component with threads |
|
|
Hi,
I have the following problem with multithreading:
My component creates one or more threads that do some work and now and then
update the user interface (main form in VCL main thread) via Synchronize().
Until now I thought I would be save with Synchronize() but sometimes I seem
to have dead locks when closing the program while the threads are still
active.
My first thought was that the main form is already destroying and the
threads of my component still try to synchronize. Since Synchronize() is
blocking the threads wait for the main thread to process messages. The main
thread, however, is already out of its message loop and is waiting for the
threads to terminate. Can anyone confirm that this could be?
Now I let my threads check the components csDestroying flag before trying to
synchronize and it seems to help. But is this save enough?
Any other ideas how to avoid this conflict when closing a program with
active worker threads?
thanks for any idea,
Nick
|
|
| Back to top |
|
 |
nicolasr Guest
|
Posted: Sat Aug 16, 2003 1:07 am Post subject: Re: Dead lock in Component with threads |
|
|
thanks for the help Remy,
| Quote: | Have your MainForm's OnCloseQuery or OnClose event call a method of your
component which then terminates the threads while the message queue is
still
running.
Is it possible to do this in a way so that it's transparent to the component |
user? Wouldn't any event handler the user assigns override the one my
component assigned?
| Quote: | Alternatively, make sure your component destructor is terminating the
threads as well as calling their WaitFor() methods so that messages and
Synchronize() requests are still being processed even if the main message
queue is already stopped. Each thread should also be testing its
Terminated
property immediate after calling Synchronize() and returning ASAP if it is
set to true.
I think I don't quite understand this. In which way are messages processed |
when I call WaitFor() even if the main message queue is stopped?
I am terminating the threads in the destructor. But I don't call WaitFor(),
because it has no timeout.
I use my own function that first calls Terminate() on the thread, then waits
for a timeout and if the thread has not terminated (checked with
GetExitCodeThread()) I call TerminateThread().
Would overriding BeforeDestruction() (and terminating the threads there) be
an option?
Nick
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Aug 16, 2003 3:35 am Post subject: Re: Dead lock in Component with threads |
|
|
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote
| Quote: | Is it possible to do this in a way so that it's transparent
to the component user?
|
Not without hooking the form/application.
| Quote: | Wouldn't any event handler the user assigns override
the one my component assigned?
|
Of course.
| Quote: | I think I don't quite understand this. In which way are
messages processed when I call WaitFor() even if the
main message queue is stopped?
|
When WaitFor() is called in the context of the main VCL thread, it processes
messages internally as long as there are still pending SendMessage()
requests in progress. Synchronize() uses SendMessage(). In the case of
BCB6, WaitFor() also calls the new CheckSynchronize() function as well,
since Synchronize() no longer uses SendMessage() in BCB6.
| Quote: | I am terminating the threads in the destructor. But I
don't call WaitFor(), because it has no timeout.
|
That is because the whole purpose of WaitFor() is to "wait for" the thread
to fully terminate.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|