Solved

Threadsafe function

Posted on 2001-07-11
3
159 Views
Last Modified: 2012-05-04
Lets say I have a simple function which don't interacts with the VCL, e.g. adding two numbers.
If I call this function the same time from two threads, would that cause an error.
Would it make a differens if the function creates an local object and frees it after use. Then two objects should be created I guess.

Please higher my thread knowledge :)
0
Comment
Question by:koger
3 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6274999
You can call the same function from 2 different threads as long as the function is thread-safe. Functions like Pos(), IntToStr() and Length() are safe. The same counts for Objects. Each thread can create its own objects. E.g. TStringlist and TMemoryStream.
0
 
LVL 1

Expert Comment

by:alx512
ID: 6275785
Use threadvar directive instead of var.
A thread-local variable is like a global variable, except that each thread of execution gets its own private copy of the variable, which cannot be accessed from other threads.

Thread-variable declarations
  cannot occur within a procedure or function.
  cannot include initializations.
  cannot specify the absolute directive.

Reference-counted variables (such as long strings, dynamic arrays, or interfaces) are not thread-safe, even if they are declared with threadvar. Do not use dynamic thread variables, since there is in general no way to free the heap-allocated memory created by each thread of execution. Finally, do not create pointer- or procedural-type thread variables.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 6275903
alx512 is right, but only if you need something like global variables. THEN threadvar is one solution, and THEN dynamic reference-counted variables are not thread safe in the meaning, that threadvar don't secure them automatically.
If you need global variables, you can also not use threadvar, but normal global variables and secure them with e.g. critical sections. In that case you can also use dynamic reference-counted variables. Of course its more work than threadvars, because you have to secure the access to the variables yourself.

But I think koger was only talking about local variables, and there you can use whatever you like, it's all thread safe. Epsylon is right there. Also you can let 1000 threads run the same code at the same time, no problem at all... Look, in win9x the system dlls are all shared between all processes. Nevertheless all threads can theoretically run through the system APIs at the same time, no problem. Each thread has its own stack and its own code pointer.

Regards, Madshi.
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

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 103
Delphi XE10 Round Image 2 128
Can Viruses spread while transferring Binary data with Winsock 2 75
Delphi inherited method 6 70
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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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