Solved

Thread safe? or not?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 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