Solved

Thread synchronization

Posted on 2002-07-02
6
476 Views
Last Modified: 2010-04-04
Hi, all,
problem: I'm going to write service app, which will monitor set of directories. This service app will be controlled from external application using windows messages (e.g. for changing directory to monitor). It seems to be necessary to use 2 threads - one for service itself [Main] and another for real processing [Process], because it will need to do kind of full scan of directory, and that might be time-consuming, especially over the network. The idea is to have TList [FIFO IIRC]: Main will add user requests to the end of the list and Process will take items out of the list one by one starting from first one and execute them.
How to synchronize usage of TList? Seems that list should be locked when item is being added [Main] and removed [Process]. What's the best way to do that? Process thread will lock list, get item, update list, unlock it and then process the action, so list (hopefully) will not be locked for a long time. How to handle a situation when the list is locked by Process and WM arrives to Main?
Also what is the best way to check the list for new items in Process Thread?
Code samples would be fine.

W_Fox
0
Comment
Question by:W_Fox
6 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 7124818
Have a look at TThreadList which implements a thread-safe list. (See LockList/UnlockList methods.)
0
 

Author Comment

by:W_Fox
ID: 7124964
Sounds nice, TThreadList seems to be the way to go :)
Do I need to check whether the list is locked or not when calling MyList.LockList? This will happen if list is locked by any of threads and message or timeout occurs in other thread. List will be unlocked for sure when processing ends, but it is possible that MyList.LockList will be called twice or more w/o UnlockList.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7125009
W_Fox,

If you have D5/6 (not sure for D4), the natural object for FIFO would be TQueue. Also, you could use TCriticalSection for locking (that's how Lock/Unlock are implemented in thread-safe components).


>Do I need to check whether the list is locked or not when calling MyList.LockList

No - that is the purpose of Lock/Unlock - to let OS do it
for you. As soon as you lock an object, everybody who hits
Lock of the same object will be immediately frozen, waiting
for Unlock. I'd advice you too read up on critical sections,
you won't get very far in multithreading without completely understanding those.

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Accepted Solution

by:
Lee_Nover earned 300 total points
ID: 7125047
you should use the TThreadList like:

with ThreadList.LockList do
try
  // do your stuff with the items
finally
   ThreadList.UnlockList;
end;

and a really good site about threads :
http://www.pergolesi.demon.co.uk/prog/threads/ToC.html

I've also done this queue thing you're planning
maybe you should check out TCommThread and the QueueCommand procedure
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/MiscFiles/vn_common/lnVidTypes.pas
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 7126264
You should always use try..finally as Lee_Nover suggested to make sure that the lock is released when no longer needed.
When thread B calls LockList and the list is already locked by thread A, then thread B enters a waiting state (execution is blocked within LockList) until thread A leaves the critical section. Thread B then acquires the lock (enters the critical section) and only then execution returns from LockList and continue executing.
0
 

Author Comment

by:W_Fox
ID: 7126381
Lee Nover's comment will be accepted as answer, particularly because of the link :) TOndrej, 100 points to you as well for TThreadList and 50 points to Cynna for TQueue.

Thank you, guys, you really helped me.
W_Fox
p.s. If I'll run into problems with threads, I hope I can contact you for advice :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now