[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Multithreaded

Posted on 2003-11-09
2
Medium Priority
?
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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