Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bhanuka_Dassanayake · Sep 27, 2021 at 11:55 AM · webglmultithreading

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 27, 2021 at 12:19 PM 0
Share

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 -.-

avatar image Bhanuka_Dassanayake · Sep 27, 2021 at 12:43 PM 0
Share

@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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Jobs and BURST low performance on Html5 1 Answer

Window not defined error in WebGL for multithreaded application 0 Answers

Can i catch the WebGL Memory size error? 1 Answer

As I avoid downloading a webgl content made with unity. 0 Answers

Problems with webgl 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges