Sign in Join

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

Mailing lists / fpc-pascal / 2010-05

Main thread wait and CheckSynchronize

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Roland Schaefer

Hello,

I have a seemingly simple question which nevertheless is giving me headaches: I have a main thread which does nothing but [1] execute CheckSynchronize, then [2] check whether some other things have to be done as a result of the synchronization (and do those things if necessary), then go back to [1] (until some quit condition is met).

Now, instead of busy waiting in the main thread loop (even with a Sleep inserted), I'd rather have it idle wait until another thread needs to synchronize. Is that possible?

I have tested a solution which achieves what I want (main thread waits until some other thread wakes it) with a TEventObject, but that requires threads to explicitly post a specific event and breaks support for threads which simply expect CheckSynchronize to be called regularly.

I'd really appreciate any hints on this.

Cheers
Roland

Roland Schaefer
to Roland Schaefer

Allow me to bump this. I'd appreciate any comments, even if it's "Why on earth do you want to do that!?" or something.

Thanks
Roland

Roland Schaefer wrote 25 lines
Hello, I have a seemingly simple question which nevertheless is giving me headaches: I have a main thread which does nothing but [1] execute CheckSynchronize, then [2] check whether some other things have to be done as a result of the synchronization (and do those things if necessary), then go back to [1] (until some quit condition is met). Now, instead of busy waiting in the main thread loop (even with a Sleep inserted), I'd rather have it idle wait until another thread needs to synchronize. Is that possible? I have tested a solution which achieves what I want (main thread waits until some other thread wakes it) with a TEventObject, but that requires threads to explicitly post a specific event and breaks support for threads which simply expect CheckSynchronize to be called regularly. I'd really appreciate any hints on this. Cheers Roland _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Roland Schaefer: I have a seemingly simple question which nevertheless is giving me headaches: I have a…
Roland Schaefer wrote
Now, instead of busy waiting in the main thread loop (even with a Sleep inserted), I'd rather have it idle wait until another thread needs to synchronize. Is that possible?

CheckSynchronize has a parameter "timeout" with the default value 0: http://www.freepascal.org/docs-html/rtl/classes/checksynchronize.html

You can pass a different value to wait for a longer period of time (specify the number of milliseconds). There is no equivalent of "wait forever" though.

Jonas

Roland Schaefer
to Jonas Maebe
Jonas Maebe wrote 10 lines
On 14 May 2010, at 00:18, Roland Schaefer wrote: > Now, instead of busy waiting in the main thread loop (even with a Sleep > inserted), I'd rather have it idle wait until another thread needs to > synchronize. Is that possible? CheckSynchronize has a parameter "timeout" with the default value 0: http://www.freepascal.org/docs-html/rtl/classes/checksynchronize.html You can pass a different value to wait for a longer period of time (specify the number of milliseconds). There is no equivalent of "wait forever" though.

Thanks a lot! Setting it sufficiently high gives me exactly what I need.

One suggestion for the docs: I actually had (admittedly very quickly) tried CheckSynchronize with a timeout before. However, the procedure outclevered me, since for the test I didn't create any threads before going into a loop with CheckSynchronize(2147483647). It just didn't seem to work. , I have now found out that it has sanity checks which make it ignore the timeout when there are no threads created. Shouldn't this be mentioned in the docs?

Thanks again
Roland

Michael Van Canneyt
to Roland Schaefer: Allow me to bump this. I'd appreciate any comments, even if it's "Why on earth do you…

Hello,

I think that what you did is the only right solution. Synchronizing threads is tricky, and the default mechanism is meant for GUI apps where the main thread is the GUI thread.

Michael.

Roland Schaefer wrote 36 lines
Allow me to bump this. I'd appreciate any comments, even if it's "Why on earth do you want to do that!?" or something. Thanks Roland On 14.05.2010 00:18, Roland Schaefer wrote: > Hello, > > I have a seemingly simple question which nevertheless is giving me > headaches: I have a main thread which does nothing but [1] execute > CheckSynchronize, then [2] check whether some other things have to be > done as a result of the synchronization (and do those things if > necessary), then go back to [1] (until some quit condition is met). > > Now, instead of busy waiting in the main thread loop (even with a Sleep > inserted), I'd rather have it idle wait until another thread needs to > synchronize. Is that possible? > > I have tested a solution which achieves what I want (main thread waits > until some other thread wakes it) with a TEventObject, but that requires > threads to explicitly post a specific event and breaks support for > threads which simply expect CheckSynchronize to be called regularly. > > I'd really appreciate any hints on this. > > Cheers > Roland > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Brad Campbell
to Michael Van Canneyt
Michael Van Canneyt wrote 7 lines
Hello, I think that what you did is the only right solution. Synchronizing threads is tricky, and the default mechanism is meant for GUI apps where the main thread is the GUI thread.

I wish there was a way for a thread to asynchronously notify the main thread there is a Synchronize() pending. On Linux I can insert an fd() for a pipe into the gtk event loop and poke data into the pipe from the thread. It's incredibly efficient and saves all the waiting and polling. I've not worked out how to do this on OSX or Win32 yet though.

Brad

Dolphins are so intelligent that within a few weeks they can train Americans to stand at the edge of the pool and throw them fish.
Jonas Maebe
to Brad Campbell
Brad Campbell wrote 11 lines
Michael Van Canneyt wrote: > I think that what you did is the only right solution. Synchronizing > threads is tricky, and the default mechanism is meant for GUI apps > where the main thread is the GUI thread. I wish there was a way for a thread to asynchronously notify the main thread there is a Synchronize() pending. On Linux I can insert an fd() for a pipe into the gtk event loop and poke data into the pipe from the thread. It's incredibly efficient and saves all the waiting and polling. I've not worked out how to do this on OSX or Win32 yet though.

If you use the timeout parameter from CheckSynchronize, you do get that effect (if you pass high(longint) as timeout, then you only "poll" if nothing happened for 2^31 miliseconds, which is about 24 days).

Jonas

Brad Campbell
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 17 May 2010, at 16:00, Brad Campbell wrote: > > I wish there was a way for a thread to asynchronously notify the main > thread there is a Synchronize() pending. On Linux I can insert an fd() > for a pipe into the gtk event loop and poke data into the pipe from > the thread. It's incredibly efficient and saves all the waiting and > polling. I've not worked out how to do this on OSX or Win32 yet though. If you use the timeout parameter from CheckSynchronize, you do get that effect (if you pass high(longint) as timeout, then you only "poll" if nothing happened for 2^31 miliseconds, which is about 24 days).

Err. yeah, but doesn't checksynchronize block until either an event or the timeout?

I'd have thought that would destroy your GUI interactivity.

Brad

Dolphins are so intelligent that within a few weeks they can train Americans to stand at the edge of the pool and throw them fish.