Solved

Thread safe? or not?

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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