Recent

Author Topic: Synchronizing between TThreads  (Read 1678 times)

ArminLinder

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
Synchronizing between TThreads
« on: July 14, 2023, 06:44:45 pm »
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.
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Re: Synchronizing between TThreads
« Reply #1 on: July 14, 2023, 08:09:23 pm »
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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Lulu

  • Sr. Member
  • ****
  • Posts: 302
Re: Synchronizing between TThreads
« Reply #2 on: July 19, 2023, 02:40:28 pm »
Hi, there is also RTLEvent. See in the folder where is installed Lazarus, subfolder examples/multithreading there is a demo waitforexample1.lpi
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12317
  • FPC developer.
Re: Synchronizing between TThreads
« Reply #3 on: July 19, 2023, 02:46:24 pm »
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

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
Re: Synchronizing between TThreads
« Reply #4 on: July 25, 2023, 07:19:59 pm »
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.
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1536
    • Lebeau Software
Re: Synchronizing between TThreads
« Reply #5 on: July 25, 2023, 09:26:13 pm »
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?

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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 17420
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Synchronizing between TThreads
« Reply #6 on: July 26, 2023, 12:03:42 pm »
Further more you are turning a threaded application into a non- threaded application.
Which is common and silly.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018