Solved

Multithreaded

Posted on 2003-11-09
2
521 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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