- Home /
Multi threading to Main thread
I follow Sebastian Legue's procedural terrain generation tutorials and it work. and I'm try to use it for webgl and for that it don't work. I think it's because of Multi Threading.
so can you tell me how to edit for only main thread?
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Threading;
public class ThreadedDataRequester : MonoBehaviour {
static ThreadedDataRequester instance;
Queue<ThreadInfo> dataQueue = new Queue<ThreadInfo>();
void Awake() {
instance = FindObjectOfType<ThreadedDataRequester> ();
}
public static void RequestData(Func<object> generateData, Action<object> callback) {
ThreadStart threadStart = delegate {
instance.DataThread (generateData, callback);
};
new Thread (threadStart).Start ();
}
void DataThread(Func<object> generateData, Action<object> callback) {
object data = generateData ();
lock (dataQueue) {
dataQueue.Enqueue (new ThreadInfo (callback, data));
}
}
void Update() {
if (dataQueue.Count > 0) {
for (int i = 0; i < dataQueue.Count; i++) {
ThreadInfo threadInfo = dataQueue.Dequeue ();
threadInfo.callback (threadInfo.parameter);
}
}
}
struct ThreadInfo {
public readonly Action<object> callback;
public readonly object parameter;
public ThreadInfo (Action<object> callback, object parameter)
{
this.callback = callback;
this.parameter = parameter;
}
}
}
this is his code.
video link : https://youtu.be/f0m73RsBik4 his Github : https://github.com/SebLague/Procedural-Landmass-Generation
Answer by Bunny83 · Sep 27, 2021 at 12:11 PM
Well, just ignore that class completely. I haven't watched the tutorial, so I don't know how the code looks like that is using this class. However instead of doing this:
ThreadedDataRequester.RequestData(YourGenerateMethod, YourCallback);
you could simply do this:
YourCallback(YourGenerateMethod());
so it just happens synchronously. Though I can't say if executing the code synchronously may cause any issues. You probably have to try it. Though be warned that if the generation is that heavy so it got outsourced into a thread, chances are high that WebGL can't even handle the amount of data.
edit
if this class is used in several places, you can also do the "fix" right inside the "RequestData" method like this:
public static void RequestData(Func<object> generateData, Action<object> callback)
{
#if UNITY_WEBGL
callback(generateData());
#else
ThreadStart threadStart = delegate {
instance.DataThread (generateData, callback);
};
new Thread (threadStart).Start ();
#endif
}
btw: the Update method in this example is extremely flawed on multiple levels. First of all it modifies the queue (removing elements from the queue) while other threads may writing to the queue. He has a lock in the worker threads, however that's pointless if you don't lock every modification access. Next he iterates forward through the queue while removing elements from the queue. This will always just take out half the elements of the queue. While every pending jobs may ultimatively be handled in consecutive frames, it's just a bad and wrong implementation all together -.-
@Bunny83 , Thank you very much for your valuable time☺️.
it worked! but as you told, with some lags. is multi treading don't work for WebGl?