Recent

Author Topic: [SOLVED] Does using TTimer creates a race condition?  (Read 1485 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
[SOLVED] Does using TTimer creates a race condition?
« on: November 27, 2023, 05:47:55 am »
I think the answer is yes, but just wanted to make sure:

Can I assume that when a timer event fires, its inserting a message in the main messaging loop so that no race conditions occur between the calls made from within that event and the code running in the main thread?
« Last Edit: November 28, 2023, 11:38:49 pm by EganSolo »

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Does using TTimer creates a race condition?
« Reply #1 on: November 27, 2023, 08:52:26 pm »
I think the answer is yes, but just wanted to make sure:

Can I assume that when a timer event fires, its inserting a message in the main messaging loop so that no race conditions occur between the calls made from within that event and the code running in the main thread?
In Windows: Yes. But only because the message queue and messages to windows are handled by the same main thread.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1430
    • Lebeau Software
Re: Does using TTimer creates a race condition?
« Reply #2 on: November 28, 2023, 02:06:10 am »
Can I assume that when a timer event fires, its inserting a message in the main messaging loop so that no race conditions occur between the calls made from within that event and the code running in the main thread?

TTimer runs in the thread that activates it, so if that happens to be the main thread then there is no race condition.  It is possible to run TTimer in another thread that has its own message loop.
« Last Edit: November 28, 2023, 06:35:18 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: Does using TTimer creates a race condition?
« Reply #3 on: November 28, 2023, 10:58:42 am »
Remy and Serge: thanks for your responses. It seems though that Remy's answer holds regardless of the OS, no? Serge seems to think that in non-Windows OSes TTimer does not __necessarily__ runs in the main thread. Did I get that right?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1430
    • Lebeau Software
Re: Does using TTimer creates a race condition?
« Reply #4 on: November 28, 2023, 06:58:48 pm »
It seems though that Remy's answer holds regardless of the OS, no? Serge seems to think that in non-Windows OSes TTimer does not __necessarily__ runs in the main thread. Did I get that right?

Looking at TTimer's source code right now, I see it can behave differently on various OSes.  For instance, on Windows it runs in the message loop of the activating thread.  Whereas under MacOS using Carbon, it always runs in the message loop of the main thread instead of the activating thread.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: Does using TTimer creates a race condition?
« Reply #5 on: November 28, 2023, 09:03:31 pm »
Can I assume that when a timer event fires, its inserting a message in the main messaging loop so that no race conditions occur between the calls made from within that event and the code running in the main thread?

The timer event will be fired by the same thread that also provides all other events. So as long as you don't have other threads you don't need to think about synchronisation.

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: Does using TTimer creates a race condition?
« Reply #6 on: November 28, 2023, 11:38:26 pm »
Great! Thanks!
I'm wondering if it would make sense to update the wiki page on TTimer to include this tidbit?
The page I'm thinking of is https://wiki.lazarus.freepascal.org/TTimer. Obviously, I don't have permission to alter the page. Just a thought

 

TinyPortal © 2005-2018