Link to home
Start Free TrialLog in
Avatar of efryevt
efryevt

asked on

Threadsafe access of FIFO from 2 separate threads in Delphi

I have an application where it is necessary to build and fill a FIFO style queue.  There is a Thread that is responsible for polling a client machine for data.  This data needs to be pumped into a queue.  Then, asynchronously, the main thread is going to retrieve data from the queue for processing.  

What is the best way to make this threadsafe?  Two separate threads need to access the same queue (there are other properties they both need to access as well; such as a channel count, etc).  The producer (filling) thread will be running in a continuous loop.  The consumer (dumper) will be executed asynchronous to the fill and I have no control over when it is called (it is external to my section of code).  

This may be very simple, but I am new to Delphi.  I have read about synchronizing, but I would love to see an example that shows this or something near.  I do not need assistance on setting up a queue unless there is an issue with using TObjectQueue.

Thanks in advance!
SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of efryevt
efryevt

ASKER

Thank you both for the comments.  I have taken time to review them and I see potential for both to serve as a solution.  

The third party libraries offer a lot of capability, and I will probably use these in my project (or future projects).  

Also, Sara's example using TCriticalSection is a solution.  This seems to be similar to the lock(obj) functionality of C# which I am familiar with.  Thank you for the explanation.

In all fairness I must mark both as solutions.  Thank you again.