Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi threadvar question

When we have a threadvar declared, when this variable will be initialized (the object is created)? Does it occurs at the first assignment of the var? For example:

threadvar
  myThreadVar : string;

......

//inside a thread
  ...
  myThreadVar := 'my value'; // In this point the var will be initialized?

What happens if I try to use this var outside a thread after the thread has set the value for the var? For example:

//at the main thread (application)
  ...
  //Call the thread;
  //thread finishes execution
  //thread is destroyed
  ShowMessage(myThreadVar); // what happens here?
like image 725
Rafael Colucci Avatar asked May 04 '11 18:05

Rafael Colucci


3 Answers

The threadvars for a thread are initialized the first time their thread accesses any one of them. They are set to a default all-bits-zero value, which for strings is the empty string.

Threadvars may or may not be finalized. It depends on how much notice the RTL gets that a thread is terminating. For that reason, it's probably best not to store any dynamically allocated types (strings included) in threadvars. Instead, use an instance variable of a TThread object to store thread-specific data.


The second part of your question is nonsense. It has you executing code on a thread after the thread has already terminated. There is no such thing as running code "outside a thread." All code runs in threads. Every program has at least one thread.

Each thread has its own copy of a threadvar. No thread can read another thread's copy, so once a thread terminates, all its threadvars are inaccessible.

Your ShowMessage call will display the value belonging to the current thread, not the thread that already terminated.

like image 117
Rob Kennedy Avatar answered Oct 07 '22 03:10

Rob Kennedy


The thread-local storage will be zeroed out (initialized) when the thread is created. So before you run the line myThreadVar := 'my value';, it'll be an empty string.

As for your second question, threadvars are unique to each thread. When you declare a threadvar, you declare a slot in thread-local storage, and each thread gets a copy of the slot. You can think of it as kind of like thread1.myThreadVar, thread2.myThreadVar, mainThread.myThreadVar, etc. So if you set a threadvar in one thread and try to read it in another, you won't read what you set in the other thread; you'll read whatever is assigned to the current thread's version of the threadvar.

like image 27
Mason Wheeler Avatar answered Oct 07 '22 01:10

Mason Wheeler


threadvar means that you have an instance of the variable per thread. There's no such thing as 'outside a thread' - if you're not running inside an explicit thread that you created, you're running on the process' default thread. if you set a threadvar to a value within an explicit thread, that value is invisible to all other threads.

like image 40
500 - Internal Server Error Avatar answered Oct 07 '22 03:10

500 - Internal Server Error