Solved

Thread safe? or not?

Posted on 1998-04-20
2
438 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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