Solved

Thread safe? or not?

Posted on 1998-04-20
2
446 Views
Last Modified: 2010-04-06
easy one.  are strings (and other simple types) thread safe?

if I have a global string variable defined, and two threads reading/writing it, is it safe?  or do I need to make up a thread safe string (similar to a Thread safe list - TThreadList)
0
Comment
Question by:1750
2 Comments
 
LVL 1

Accepted Solution

by:
extreme earned 20 total points
ID: 1335703
You should not access strings in two thread because strings in Delphi are dynamically allocated. So, in a call to a function in a thread you can pass the address (don't forget that strings since D2 are pointers) of a string that is destroyed in another thread to store a diferent string. That would cause your program to commit an error...Maybe not an access violation, but sure it won't work as spected. Static types *could* be thread safe. I say could because that depends on your logic, in some cases access an integer (for example) between two threads could not cause problems, and sometimes not.

Consider a code where you do something like this:

globalvar:
  i : integer = 0;
 
threadcode:
while i < xx do
  begin
     inc (i, 2);
     Print (i);
  end;

That code executed in parallel won't work as spected, because after one thread modifies the value of i, the other thread inmediatly could modify it too. So, when the program executes the Print line i has a value that is equal to: Last i + 4, not + 2. this can happen, or not... depends on several factors. Do you got the idea?.

All resources that should be accessed by two or more diferent threads should be accessed using some control mechanism. Call it semaphores, critical sections, etc... The only resources that you access directly without control are the control resources, like the variables used as semaphores, or maybe some object that encapsulates the critical section logic.

About the simple types, of course that access them directly in the thread would not cause extra problems to your app besides the wrong logic, but access the strings could cause more problems than expected. When working with control algoritms don't forget to check very well the logic you use, it's a common problem when working with threads to cause a deadlock (when two threads wait infinitely for a shared resource to be available).

I hope this serve you.

Cheers
 
0
 

Author Comment

by:1750
ID: 1335704
Thanks! good answer.  Having looked at the Source for TThreadList, Ive created a simple TThreadString class which uses the same locking mechanism, a "TRTLCriticalSection", and its associated methods.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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