?
Solved

Thread safe? or not?

Posted on 1998-04-20
2
Medium Priority
?
470 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
[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 1

Accepted Solution

by:
extreme earned 80 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 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