Wrong value from thread
I'm using a thread in unity to handle some actions in the background. At first, everything runs smoothly. But after a while, if I try to access any variable from the main thread, it seems to get stuck, even though accessing the same variable from the main thread returns the expected value.
This is how the thread starts, but I don't know any other pieces that could be helpful
ThreadManager.actionThread = new Thread(new ThreadStart(ThreadManager.ThreadedActions));
ThreadManager.actionThread.Start();
EDIT: Alright, here are a few pieces of code from the thread:
The main loop:
public static void ThreadedActions()
{
while (true)
{
Map.ActivateNearbyVoid();
}
}
I've heard that Unity doesn't handle endless loops in other threads well, but don't know much about that.
The part in the thread that has the issue:
public static void ActivateNearbyVoid()
{
activeChunks = new List<Vector3Int>();
HashSet<GameObject> toAdd = new HashSet<GameObject>();
int range = 4;
Vector3Int pos = new Vector3Int(chunkx, chunky, chunkz);
Controller_Camera.instance.GizmosTemp = () =>
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector3(chunkx, chunky, chunkz), Vector3.one * 0.7f);
Gizmos.color = Color.blue;
Gizmos.DrawCube(pos, Vector3.one * 1f);
};
for (int x = chunkx - range; x <= chunkx + range; x++)
{
for (int y = chunky - range; y <= chunky + range; y++)
{
for (int z = chunkz - range; z <= chunkz + range; z++)
{
if (chunks.ContainsKey(new Vector3Int(x, y, z)))
{
foreach (GameObject obj in chunks[new Vector3Int(x, y, z)].solidObjects)
{
if (!toAdd.Contains(obj))
toAdd.Add(obj);
}
activeChunks.Add(new Vector3Int(x, y, z));
}
}
}
}
...
The variables chunkx, chunky and chunkz are static variables that I'm trying to access. They get modified from the main thread. At first, the second thread reads them like it should, but after a while they freeze at a specific value. They seem to be doing the same both in the pos variable definition and the for loops. But in the Controller_Camera.instance.GizmosTemp action, which runs from the main thread, the three variables have the values they should while the pos variable is different.
That sounds like you have no experience with threads. Do you even know about basic thread-safety? You never ever want to access the same variables / resources from two different threads at the same time. You need some sort of synchronisation / locking if you share any resources. Without your actual code we can't help you at all.
You also should be aware of the fact that you have to ter$$anonymous$$ate your threads yourself. Threads are not ter$$anonymous$$ated when you press stop in the editor. So the thread(s) would continue to run in the editor and can cause all sorts of problems. The list of common errors when it comes to threading is almost endless.
I'll move the question into the help room as this is not a question about something Unity specific.
You should edit your question and add more details if you want any answers.
It's true that I am very new to threads, and my problem probably comes from there.
And yeah, without the code, I'm not expecting much. I'll try to add a few pieces hoping one of them helps.
I know about thread safety, but since there was no need for the threads to be synchronized, I thought I'd ignore it.
About ending the thread, at first I was ending it using .Abort(), but for some weird reason removed it. Oops...
Thanks for all the tips and for moving the it to the help room too :)