Solved

Thread synchronization

Posted on 2002-07-02
6
480 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 119
Create Database on Android via Delphi dbExpress 3 116
Reconfigure Delphi Install? 2 61
DBGrid or StringGrid ? 6 105
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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