Board index » cppbuilder » Dead lock in Component with threads

Dead lock in Component with threads


2003-08-15 06:59:49 AM
cppbuilder29
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
 
 

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
 

Re:Dead lock in Component with threads

thanks,
Nick
 

{smallsort}

Re:Dead lock in Component with threads

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net>wrote in message
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