Solved

global variables multi-threading application

Posted on 2011-02-16
7
1,584 Views
Last Modified: 2012-05-11
Hello experts.

I have a multi-threading Delphi application that uses some global variables but only for read-only purposes. The initial value of those global variables is set when the application starts and only the main thread is active. I have not had problems with my application so far but want to know if it is safe to read the value of global variables in a multi-threaded application and the reason why it is safe or not to access the global variables for reading their value.

Regards.
0
Comment
Question by:arturosm
7 Comments
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 200 total points
ID: 34910346
it would be better if you pass the value of the variables to the thread
this way you can keep the global variables and the thread is not affected by any changes

you could build in a system to restart the thread

using global variables accessed by threads is not such a good idea

you could protect access to them with critical sections
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 100 total points
ID: 34910385

So long as they are strictly readonly, then it would be safe to access them.
However for safety and less headache, you should still protect them
0
 

Author Comment

by:arturosm
ID: 34910445
Thank you Geert_Gruwez.

I don´t know if this is another question, just that I don´t know much about critical sections, can you tell me more please, I'm asking for concepts more than for code.

Regards.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:arturosm
ID: 34910477
Thank you ewangoya.

Yes, they are strictly read only, basically they contain the value of paths to folders that the application uses for reading and writing files.

Regards.
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 200 total points
ID: 34910523
unit x;

var globalvar: string;

function valueofglobalvar: string;

implementation

uses SyncObjs;

var cs: TCriticalSection;

procedure initCs;
begin
  cs := TCriticalSection.Create;
end;

procedure EndCs;
begin
  cs.Free;
end;

function valueofglobalvar: string;
begin
  cs.Enter;
  try
    Result := globalvar;
  finally
    cs.Leave;
  end;
end;

initialization
  InitCs;
finalization
  EndCs;
end.




0
 
LVL 12

Accepted Solution

by:
Hypo earned 200 total points
ID: 34910549
I'd say that it's "safe", but it's considered to be "bad practice" to have unprotected variables that are accessed from multiple threads, even if you only read from them... It would probably work fine in your application since you say that these variables are only written to before any threads start... and then they are only read...

There is a synchronization object in Delphi that's called TMultiReadExclusiveWriteSynchronizer, and the purpose of this object is to allow for multiple threads to access/read the same variables simultaniously, whilst it still gives the possibility to protect the resource when you need to write to it...

This syncrhonization object is also normally used when you have a resource that you know will be often read by mutliple threads, but only written to in rare occacions; and the synchronization object helps speeding up the application since reads then don't have to wait for other reads...

Since the TMultiReadExclusiveWriteSynchronizer allows multiple threads to access the same resource at the same time as long as no one has locked it for writing, I'd say that your application design would be "safe" as well... and If you want to be extra safe, you implement a locking mechanism using the TMultiReadExclusiveWriteSynchronizer.

/Hypo
0
 

Author Closing Comment

by:arturosm
ID: 34913558
Thank you experts, your comments helped me to understand better this subject.

Regards.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

20 Experts available now in Live!

Get 1:1 Help Now