- Home /
Weird Threading Issue
I have a thread manager class what handles threads and queues them so that I can have a thread-safe way of checking when a thread is finished. I have a GameObject calling the managers update loop every frame to check each threads state.
Manager class :
using System;
using System.Collections.Generic;
using UnityEngine;
public class ThreadTracker
{
public static List<ThreadObject> queue = new List<ThreadObject>();
public static List<ThreadObject> threads = new List<ThreadObject>();
public static int threadLimit = 8;
static ThreadInstance instance;
// Add a thread to the queue.
public static void addThreadToQueue(ThreadObject obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
checkThreadInstanceInitialized();
//Logger.Log("Adding thread to queue");
queue.Add(obj);
}
// Register a new thread into the list and start it.
static void registerThreadHandler(ThreadObject obj)
{
//Logger.Log("Registering thread with ID value " + threads.Count + ".");
obj.id = threads.Count;
threads.Add(obj);
threads[threads.Count - 1].startThread();
}
// Un-register and dispose of a finished thread.
public static void unRegisterThreadHandler(int id)
{
//Logger.Log("Un-registering thread with ID value " + id + ".");
threads.RemoveAt(id);
}
// Check whether the thread instance object is initialized.
static void checkThreadInstanceInitialized()
{
if (instance == null)
{
instance = new GameObject().AddComponent<ThreadInstance>();
instance.name = "Thread_Instance";
instance.GetComponent<Transform>().hideFlags = HideFlags.HideInHierarchy;
}
}
// Thread tracking loop. ( Called every frame )
public static void threadTrackerLoop()
{
if (threads.Count > threadLimit)
{
throw new ThreadLimitExceededException();
}
for(int i = 0; i < threads.Count; i++)
{
if (threads[i].getThreadState() == true)
{
threads[i].threadFinished();
}
}
checkQueueForSlots();
}
// Check threads to see if there are any spare slots available.
static void checkQueueForSlots()
{
int count = (threadLimit - threads.Count);
for (int i = 0; i < count; i++)
{
if (queue.Count > 0)
{
registerThreadHandler(queue[0]);
queue.RemoveAt(0);
}
}
}
}
Thread Object Class :
using System;
using System.Collections.Generic;
using System.Threading;
public class ThreadObject// : IThreadHandler
{
public object threadObject;
private bool threadState = false;
public int id = -1;
public ThreadObject(int uniqueID)
{
id = uniqueID;
}
public ThreadObject()
{
}
// Initialize thread lock object.
public void initializeThreadData()
{
threadObject = new object();
}
// Start the thread.
public void startThread()
{
initializeThreadData();
Logger.Log("Thread with ID " + id + " has started.");
Thread thread = new Thread(new ThreadStart(doThread));
thread.Start();
}
// Called from the main thread to cleanup and finish.
public void threadFinished()
{
Logger.Log("Thread with ID " + id + " has finished.");
ThreadTracker.unRegisterThreadHandler(id);
}
// Thread base function
void doThread()
{
Thread.Sleep(2350);
finishThread();
}
// Called internally by the thread to finish itself.
void finishThread()
{
setThreadState(true);
}
// Get the threads state.
public bool getThreadState()
{
lock (threadObject)
{
return threadState;
}
}
// Safely set the threads final state.
public void setThreadState(bool state)
{
Monitor.Enter(threadObject);
try
{
threadState = state;
}
finally
{
Monitor.Exit(threadObject);
}
}
}
The problem is basically happening when the thread limit is reached, it literally starts another set of threads, instantly stops them again and so on until the thread list is empty, I am not sure what is causing this issue and it would be very helpful for some hints to why it happens.
Comment