Skip to main content

Workers

Namespace: Holo.Core
Assembly: Holo.Core

HoloMIT SDK provides a flexible and extensible multi-threading system to offload performance-intensive operations from Unity’s main thread. At the core of this system is the BaseWorker class, which allows developers to implement long-running or recurring tasks in a separate thread, safely and efficiently.


What is a Worker?​

A Worker is a class that runs its own dedicated thread and executes logic in a controlled loop. Workers are commonly used in the SDK for:

  • Capturing frames
  • Media encoding/decoding
  • Network I/O
  • Background reconstruction
  • Data preprocessing
  • Telemetry and analytics

BaseWorker Overview​

The BaseWorker class is the foundation for creating threaded components in HoloMIT.

public class BaseWorker {
public enum WorkerType {
Init,
Run,
End
}

public bool isRunning { get; private set; }

public BaseWorker(WorkerType _type = WorkerType.Run) { ... }

protected virtual void Start(); // Starts the thread
public virtual void Stop(); // Signals the thread to stop
public virtual void StopAndWait(); // Waits for clean termination
protected virtual void Update(); // Override: logic per loop iteration
public virtual void OnStop(); // Override: cleanup logic
}
  • Each worker runs its logic in the Update() method, which is called repeatedly on a background thread.
  • The thread is started via Start() and can be stopped using Stop() or StopAndWait().
  • The WorkerType only indicates if you want to concatenate workers, which kind of worker is, but works only as a guideline, has no effect.

Creating Custom Workers​

To implement your own worker, simply inherit from BaseWorker and override the Update() method:

public class MyWorker : BaseWorker {
public MyWorker() : base(WorkerType.Run) { }

protected override void Update() {
// Your logic here
Debug.Log("Running custom threaded operation.");
}

public void Launch() {
Start(); // Call when ready to start the thread
}
}

This thread runs independently of Unity’s main loop, making it ideal for asynchronous or blocking operations.


Threading Notes​

  • All logic inside Update() must be thread-safe.
  • Avoid accessing Unity API objects (like GameObjects, Transforms, UI) from inside a worker thread.
  • Use thread-safe queues or data structures to communicate with the main thread or among workers.

Lifecycle Management​

Workers can safely handle their own shutdown procedure using the OnStop() method. This is especially useful for:

  • Closing sockets
  • Flushing buffers
  • Releasing unmanaged resources

Debugging and Monitoring​

  • Thread start/stop messages are logged if debugThreading is set to true.
  • Unhandled exceptions inside the worker thread are caught and logged.
  • Name() provides a readable name for debugging purposes.

Best Practices​

  • Keep Update() logic as fast and isolated as possible
  • Always use StopAndWait() during cleanup or scene changes to prevent thread leaks
  • Use inheritance and override only the parts you need (SRP – Single Responsibility Principle)
  • Encapsulate long-running media or network systems inside dedicated workers for performance and maintainability

HoloMIT Workers follow a clean threading abstraction that aligns with SOLID design principles, allowing safe extensibility and modular background processing.