[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multithreaded

Posted on 2003-11-09
2
Medium Priority
?
527 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 320 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 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