same thread?

Tygur

Regular
Joined
Oct 2, 2003
Messages
68
I'm trying to find the proper way of finding out whether two variables refer to the same thread. I considered using the Is operator (== in C#) to determine whether the two variables are pointing to the same object, but I'm wondering if it might be possible to have two separate objects that both refer to the same thread. I tried looking for some sort of a ThreadID property so I can uniquely identify a thread that way, but couldn't find any.

So am I safe just using Is (or == if i was using C#) or should I do it some harder way?
 
1) The == operator can be overloaded. By default it just checks if both variables are the same reference/instance. I recommend using Object.ReferenceEquals instead of == to see if two objects are the same reference.
2) Are these instance or static variables?
3) This Is operator just returns a true or false if an object is the same type.
e.x.: string me = "";
me is string; //returns true because me is an instance of a string
me is object;// returns true because string is derived from object
ms is TextReader;//returns false because me is not an instance of TextReader.
me = null;
me is string;//returns fase because it's set to null
4) Threads share the same memory space.
5) Could you possible rephrase or clear up your question(s)
 
The two variables are Thread variables. In other words, they refer to an instance of the Thread Class. What I'm trying to find out is whether or not they refer to the same thread.

Can there be two Thread class instances that both refer to the same thread? If not, then simply using == or Is should be enough to see if the two variables refer to the same thread (if they refer to the same class instance, they refer to the same thread, otherwise the threads would have to be different). But if it is somehow possible to have two different instances of the Thread class referring to the same thread, then Is and == won't work for my purposes, and I will need another way.

Does that clarify things?
 
Tygur said:
What I'm trying to find out is whether or not they refer to the same thread

Use Object.ReferenceEquals or Thread.Equals (it's not overrided), if it returns true, they both refer to the same thread.

Tygur said:

Can there be two Thread class instances that both refer to the same thread?

The only way I think that can be achieved is by doing something like.
Thread a = new Thread();
Thread b = a;//a and b refer to the same thread.

Tygur said:

But if it is somehow possible to have two different instances of the Thread class referring to the same thread, then Is and == won't work for my purposes, and I will need another way.

You could try using the Thread.Name property.

Tygur said:

then Is and == won't work for my purposes

I'm not sure if 'Is' is a VB keyword but 'is' is a C# keyword and it does Type checking, not reference.
 
Just as an example to check the usefulness of the Equals method:

Visual Basic:
Imports System.Threading

Public Class Form1
	Inherits System.Windows.Forms.Form

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim t1 As Thread
		Dim t2 As Thread
		t1 = New Thread(AddressOf Thread1Proc)
		t2 = New Thread(AddressOf Thread2Proc)
		t1.IsBackground = True
		t2.IsBackground = True
		t1.Start()
		t2.Start()

		'Ensure that the Equals method was not overridden by the thread class to check for value equality
		'Also, check the .NET SDK for information on whether Equals() was overridden
		Dim t3 As Thread
		t3 = t1
		Dim slot As LocalDataStoreSlot = t3.AllocateDataSlot()
		t3.SetData(slot, 1)

		'Check for equality between thread three and thread one
		If t3.Equals(t1) Then
			MessageBox.Show("Thread three equals thread one.", String.Empty)
		Else
			MessageBox.Show("Thread three does NOT equal thread one.", String.Empty)
		End If

		'Check for equality between thread two and thread one
		If t2.Equals(t1) Then
			MessageBox.Show("Thread two equals thread one.", String.Empty)
		Else
			MessageBox.Show("Thread two does NOT equal thread one.", String.Empty)
		End If
	End Sub

	Private Sub Thread1Proc()
		Do
			'Nothing
		Loop
	End Sub

	Private Sub Thread2Proc()
		Do
			'Nothing
		Loop
	End Sub
End Class
I encourage you to run this to see how things work.
 
Because of this line:
t3 = t1

..Thread 3 definitely does equal thread 1. There is no doubt that both t3 and t1 refer to the same exact object. That's what = does, and that's what Is and the default implementation of Equals() tests for. However, suppose we use we obtain a Thread object in another way, such as Thread.CurrentThread(), for example. What I'm wondering is if there is any case where two separate thread Objects (two separate instances of the Thread class) might somehow refer to the same thread. I guess there probably isn't any such case, but I'm just making sure.
 
I don't know where this discussion is leading to but I'll answer what I think your primary question is with a yes/no.

Tygur said:
I'm wondering if it might be possible to have two separate objects that both refer to the same thread.

YES!
 
Now I'm wondering if you misunderstood. The code posted by Derek Stone is not an example of that happening, right? If we agree on that, then we probably are on the same page.
 
Tygur said:
Now I'm wondering if you misunderstood. The code posted by Derek Stone is not an example of that happening, right? If we agree on that, then we probably are on the same page.
Derek's code demonstrates two different instances of Thread objects that control the same thread that will run/execute the method Thread1Proc.


Tygur said:

What I'm wondering is if there is any case where two separate thread Objects (two separate instances of the Thread class) might somehow refer to the same thread. I guess there probably isn't any such case, but I'm just making sure.
There is such a case, it would mostly likely have to be done using static methods, or maybe events and delegates. It really depends on how you've coded your program or whatever you're doing. Do you want this scenario to occur in your code? What are you trying to do with your code?
 
HJB417 said:
Derek's code demonstrates two different instances of Thread objects that control the same thread that will run/execute the method Thread1Proc.
Not really.

First, the code up until t3 is brought into the picture:
Dim t1 As Thread
Dim t2 As Thread
t1 = New Thread(AddressOf Thread1Proc)
t2 = New Thread(AddressOf Thread2Proc)
t1.IsBackground = True
t2.IsBackground = True
t1.Start()
t2.Start()


Obviously, t1 is one thread, and t2 is another, and we are in agreement with that. Now let's look at the declaration and initialization of t3:
Dim t3 As Thread
t3 = t1


At this point, there are still two Thread objects. Both t3 and t1 refer to the same exact object. That also means that they both refer to the same instance of the Thread class (not separate instances). The one object that both t3 and t1 reference controls the only thread that runs Thread1Proc. And that is the only object (or instance of the Thread class) that controls that thread.
 
I'm sorry, I've been misinterpreting the word 'instance' and some parts of this dialog.

1) Dereks code shows two (t1, t3) variables that reference the same instance of a Thread object.
2) Only two instances (t1, t2) of type Thread were created.


Tygur said:
Can there be two Thread class instances that both refer to the same thread?

No. This can only be achieved using the = operator, as far as I know.

Tygur said:
then simply using == or Is should be enough to see if the two variables refer to the same thread (if they refer to the same class instance, they refer to the same thread, otherwise the threads would have to be different).

Correct, as long as the variables in question are of type Thread :)


...........weirdly enough, I think this is reply just a rehash of my 2nd reply.
 
Back
Top