Forum > LCL

[SOLVED] Does using TTimer creates a race condition?

(1/2) > >>

EganSolo:
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?

ASerge:

--- Quote from: EganSolo 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?

--- End quote ---
In Windows: Yes. But only because the message queue and messages to windows are handled by the same main thread.

Remy Lebeau:

--- Quote from: EganSolo on November 27, 2023, 05:47:55 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?

--- End quote ---

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.

EganSolo:
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:

--- Quote from: EganSolo on November 28, 2023, 10:58:42 am ---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?

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version