Delphi线程变量问题
当我们声明了一个threadvar
时,这个变量什么时候会被初始化(对象被创建)?它是否发生在 var 的第一次赋值时?例如:
threadvar
myThreadVar : string;
......
//inside a thread
...
myThreadVar := 'my value'; // In this point the var will be initialized?
如果我在线程设置 var 的值后尝试在线程外部使用该 var,会发生什么情况?例如:
//at the main thread (application)
...
//Call the thread;
//thread finishes execution
//thread is destroyed
ShowMessage(myThreadVar); // what happens here?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
线程的线程变量在其线程第一次访问其中任何一个时被初始化。它们被设置为默认的全位零值,对于字符串来说是空字符串。
线程变量可能会也可能不会最终确定。这取决于 RTL 收到线程终止通知的程度。因此,最好不要在线程变量中存储任何动态分配的类型(包括字符串)。相反,使用
TThread
对象的实例变量来存储特定于线程的数据。你的问题的第二部分是废话。它让您在线程终止后在线程上执行代码。不存在“在线程之外”运行代码这样的事情。所有代码都在线程中运行。每个程序至少有一个线程。
每个线程都有自己的 threadvar 副本。没有线程可以读取另一个线程的副本,因此一旦线程终止,其所有线程变量都将无法访问。
您的
ShowMessage
调用将显示属于当前线程的值,而不是已终止的线程。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.创建线程时,线程本地存储将被清零(初始化)。因此,在运行
myThreadVar := 'my value';
行之前,它将是一个空字符串。至于你的第二个问题,线程变量对于每个线程都是唯一的。当您声明一个 threadvar 时,您就在线程本地存储中声明了一个槽,并且每个线程都获得该槽的副本。您可以将其视为
thread1.myThreadVar
、thread2.myThreadVar
、mainThread.myThreadVar
等。因此,如果您设置一个 threadvar在一个线程中尝试在另一个线程中读取它,您将无法读取在另一个线程中设置的内容;您将读取分配给当前线程版本的 threadvar 的任何内容。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.threadvar 意味着每个线程都有一个变量的实例。不存在“线程外部”这样的东西 - 如果您没有在您创建的显式线程内运行,那么您将在进程的默认线程上运行。如果将 threadvar 设置为显式线程中的某个值,则该值对于所有其他线程都是不可见的。
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.