Main thread wait and CheckSynchronize

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Main thread wait and CheckSynchronize

Roland Schäfer
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Roland Schäfer
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  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Jonas Maebe-2
In reply to this post by Roland Schäfer

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.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Roland Schäfer
On 16.05.2010 16:01, Jonas Maebe wrote:

>
> 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. Looking at classes.inc, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Michael Van Canneyt
In reply to this post by Roland Schäfer

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.

On Sun, 16 May 2010, Roland Schaefer wrote:

> 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  -  [hidden email]
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Brad Campbell
Michael Van Canneyt wrote:
>
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Jonas Maebe-2

On 17 May 2010, at 16:00, Brad Campbell wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Main thread wait and CheckSynchronize

Brad Campbell
Jonas Maebe wrote:

>
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal