Solved

Multithreaded

Posted on 2003-11-09
2
519 Views
Last Modified: 2010-04-05
Hi,

How can I handle variable accessing (read and write) simultaneously in a multithreaded application ?

Thanks,
Andrew
0
Comment
Question by:andrewyu
2 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 80 total points
ID: 9712790
You can do this with a Mutex ... In delphi terms, a TCriticalSection will take care of this.

You should NEVER try to access variables simultaneously (specially for write) ... its certain that your app will crash sooner rather than later.  By using a TCriticalSection you  protect each variable so that only one threat can access it at a time.  As soon as you finish updating the variable (writing to it) you should call TCriticalSection.leave.  At that point, another thread will enter the critical section and so on.   ... for example

type

  someclass = class
  protected:
    CriticalSection: TCriticalSection;
  public
   constructor create;
  destructor destroy;
    procedure SomeClass.bcISAPIFilterException(Sender: TObject; E: Exception);
  end;

constructor create;
begin
  CriticalSection:= TCriticalSection.Create;
end;

destructor destroy;
begin
  CriticalSection.Free;
end;

 procedure SomeClass.bcISAPIFilterException(Sender: TObject; E: Exception);
 begin
  CriticalSection.Enter; //protect the log file from multiple threads
  bcLog.LogInfo('An exception occured: ' + E.Message);
  CriticalSection.Leave;
end;

Read the delphi documentation on TCriticalSection as it will explain to you exactly what its used for and why is it needed.

You can also search google for "pthread Mutex" and that will explain in more detail what mutexes (TCriticalSections) are used for.
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9713146
Simple variables are not a problem. Records, strings, objects, lists, arrays etc are a problem. Several objects have a thread-safe version, such as a Lock/Unlock method. TThreadList is a thread-safe version of TList which does not explicitly need to be locked/unlocked.

Failing that, if the data is more complex, use a CriticalSection as jconde suggests.

Geoff M.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

18 Experts available now in Live!

Get 1:1 Help Now