Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2013-05

TThread.WaitFor delay

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Henry Vermaak

Hi list

When I call TThread.WaitFor, I almost always get a 100ms delay, presumably due to the CheckSynchronize(100) here:

http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/unix/tthread.inc?view=markup#l296

Is it feasible to add an extra check so that this doesn't happen when FOnTerminate isn't assigned and DoSynchronizeMethod is false?

Also, it looks like spinning on FFinished like this can get you into trouble with cpu re-ordering, or am I missing something?

Henry

Reply by email

Henry Vermaak
to Henry Vermaak
Henry Vermaak wrote 12 lines
Hi list When I call TThread.WaitFor, I almost always get a 100ms delay, presumably due to the CheckSynchronize(100) here: http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/unix/tthread.inc?view=markup#l296 Is it feasible to add an extra check so that this doesn't happen when FOnTerminate isn't assigned and DoSynchronizeMethod is false? Also, it looks like spinning on FFinished like this can get you into trouble with cpu re-ordering, or am I missing something?

Anyone have thoughts on this?

Henry

Reply by email

Ludo Brands
to Henry Vermaak
Henry Vermaak wrote 7 lines
On Thu, May 30, 2013 at 03:54:08PM +0100, Henry Vermaak wrote: > Hi list > > When I call TThread.WaitFor, I almost always get a 100ms delay, > presumably due to the CheckSynchronize(100) here: > >

http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/unix/tthread.inc?view=markup#l296

Show quoted text 10 lines
> > Is it feasible to add an extra check so that this doesn't happen when > FOnTerminate isn't assigned and DoSynchronizeMethod is false? > > Also, it looks like spinning on FFinished like this can get you into > trouble with cpu re-ordering, or am I missing something? Anyone have thoughts on this? Henry

WaitForThreadTerminate(FHandle, 0) blocks the main thread and skipping the spin on FFinished would block all threads that call synchronize until the thread that is waited for finishes. You can only test assignment of FOnTerminate for the current thread and not for other threads.

DoSynchronizeMethod doesn't exist anymore in 2.7.1.

I agree the current implementation is not very satisfactory when you don't use synchronize. You could be better off using WaitForThreadTerminate yourself instead of WaitFor.

Ludo

Reply by email

Henry Vermaak
to Ludo Brands
Ludo Brands wrote 23 lines
On 06/19/2013 12:10 PM, Henry Vermaak wrote: > On Thu, May 30, 2013 at 03:54:08PM +0100, Henry Vermaak wrote: >> Hi list >> >> When I call TThread.WaitFor, I almost always get a 100ms delay, >> presumably due to the CheckSynchronize(100) here: >> >> http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/unix/tthread.inc?view=markup#l296 >> >> Is it feasible to add an extra check so that this doesn't happen when >> FOnTerminate isn't assigned and DoSynchronizeMethod is false? >> >> Also, it looks like spinning on FFinished like this can get you into >> trouble with cpu re-ordering, or am I missing something? > > Anyone have thoughts on this? WaitForThreadTerminate(FHandle, 0) blocks the main thread and skipping the spin on FFinished would block all threads that call synchronize until the thread that is waited for finishes. You can only test assignment of FOnTerminate for the current thread and not for other threads.

Ah, right. I thought the spin on FFinished was only needed to prevent deadlock if the current thread has an OnTerminate event. Why worry about other threads synchronising at that point?

DoSynchronizeMethod doesn't exist anymore in 2.7.1.

Right, sorry, I'm not really familiar with the internals.

I agree the current implementation is not very satisfactory when you don't use synchronize. You could be better off using WaitForThreadTerminate yourself instead of WaitFor.

This is what I ended up doing, was just wondering if there's a way to remove this penalty somehow.

Henry

Reply by email

Ludo Brands
to Henry Vermaak
Henry Vermaak wrote 11 lines
On Wed, Jun 19, 2013 at 04:05:19PM +0200, Ludo Brands wrote: > WaitForThreadTerminate(FHandle, 0) blocks the main thread and skipping > the spin on FFinished would block all threads that call synchronize > until the thread that is waited for finishes. You can only test > assignment of FOnTerminate for the current thread and not for other > threads. Ah, right. I thought the spin on FFinished was only needed to prevent deadlock if the current thread has an OnTerminate event. Why worry about other threads synchronising at that point?

WaitFor could be called a long time before the thread is finished. If the mainthread enters WaitForThreadTerminate it won't handle any synchronize or queued methodes any more. Somebody has to call CheckSynchronize regularly. So the FFinished loop ends only when the thread is already done and WaitForThreadTerminate will return quickly.

Ludo

Reply by email