Synchronizing between TThreads lazarus / 64-FPC development ---------------------------------------------------------------------- ArminLinder · 2023-07-14 16:44 Hi folks, I have been using Synchronize to sync TThread instances with the program main thread a while. Is there a similar mechanism to synchronize two arbitrary threads? Thnx, Armin. ---------------------------------------------------------------------- Remy Lebeau · 2023-07-14 18:09 Simply use a suitable synchronization object, such as TCriticalSection, TMutex, TMultiReadExclusiveWriteSynchronizer, etc (depending on your needs) to protect the code/resources you want to synchronize access to. ---------------------------------------------------------------------- Lulu · 2023-07-19 12:40 Hi, there is also RTLEvent. See in the folder where is installed Lazarus, subfolder examples/multithreading there is a demo waitforexample1.lpi ---------------------------------------------------------------------- marcov · 2023-07-19 12:46 I put message objects in a thread safe queue (tthreadlist or tlist protected by criticalsection), and let the thread block on an TEvent. If I put objects in the queue, I .Setevent the Tevent, and the thread drains the queue. If it is empty, it resets the tevent and blocks on it again. This is fine for not too finegrained threading. ---------------------------------------------------------------------- ArminLinder · 2023-07-25 17:19 I did neanwhile also note, that there is a TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod) in classes.inc (line: 1800). Seems that TThread.Synchronize calls it, passing "self" to AThread. Would that thing help me out? Armin. ---------------------------------------------------------------------- Remy Lebeau · 2023-07-25 19:26 Quote from: Nimral on July 25, 2023, 07:19:59 pmI did neanwhile also note, that there is a TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod) in classes.inc (line: 1800). Seems that TThread.Synchronize calls it, passing "self" to AThread. Would that thing help me out? You already know that TThread.Synchronize() (any flavor of it) goes through the main UI thread.  Sure, you could have multiple threads syncing with each other using TThread.Synchronize(), but that will still involve the main UI thread, and if the threads are not doing any UI work than that just becomes an unnecessary bottleneck.  There are better options/approaches for handling non-UI synchronization. ---------------------------------------------------------------------- Thaddy · 2023-07-26 10:03 Further more you are turning a threaded application into a non- threaded application. Which is common and silly.