Forum > FPC development

Synchronizing between TThreads

(1/2) > >>

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

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

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

Navigation

[0] Message Index

[#] Next page

Go to full version