Forum > FPC development

Synchronizing between TThreads

(1/2) > >>

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

Hi, there is also RTLEvent. See in the folder where is installed Lazarus, subfolder examples/multithreading there is a demo waitforexample1.lpi

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. 

I did neanwhile also note, that there is a TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod) in (line: 1800). Seems that TThread.Synchronize calls it, passing "self" to AThread.

Would that thing help me out?



[0] Message Index

[#] Next page

Go to full version