Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Salmjak · Dec 05, 2015 at 02:01 PM · c#fpslagthreadsloops

ThreadPooled loops cause lag

I'm writing an A* algorithm to my game, the problem is that the algorithm loop causes lag or is too slow when used in a coroutine (Takes about 0.3-0.5 seconds without any yields, 15-20 seconds with 2 yields, about 300*8 - 500*8 iterations to find the path (*8 since I check 8 neighbors for each node in a nested loop). So I decided I would put it in a separate thread so I could skip the wait/sleep/yield statements and make it run full speed. I'm using a threadpool in C# and queue every pathfinding in there (so everytime I want to find a path I send a queued item into the threadpool, currently I queue 20 functions at the start of the game).

The problem is: It still lags (FPS drop by 20-30 fps). In the profiler the lag spikes turn up as belonging to "Camera.render", but as soon as all pathfinding is done (20 paths) the lag spikes stops (in reality it decreases bit by bit with every finished thread). Could it be that the threadpool runs threads before the camera, rendering and other UI and thus makes some parts of the game have to wait? How can I avoid this and keep the speed relevant? I tried skipping the threadpool and just create new threads and set the priority to below normal, but no difference in lag. I also tried setting the affinity to a specific core using a custom class I found, but it didn't seem to work, and I don't really want 100+ pathfinding functions on a single core.

I'm using Unity 5.2.3f and using a 6 core CPU, I read something about that Unity 5 have increased the use of multithreading, is it possible I now "block" some of these threads from running? I've noticed that the lag decreases a bit when I use Thread.Sleep(1) (and thus return control to the main thread(?), but I still get the 20 FPS drop). Thread.Sleep(10) = no lag, but renders the use of threads useless (since I wanted more speed, not less).

Last question: Does "infinite"/long while-loops without a wait/sleep freeze your application no matter which thread or core they are on? Is a "full-speed" A* impossible without precomputation?

Comment
Add comment · Show 4
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 Fattie · Dec 05, 2015 at 02:07 PM 0
Share

can you explain / add code showing HOW you are threading your processing??

avatar image Salmjak Fattie · Dec 05, 2015 at 03:57 PM 0
Share

Ofcourse, quite stupid of me not to include it since I'm new to threading.

 ThreadPool.QueueUserWorkItem(state => a_star.Path(thisPos, goal, this));

I use a lambda expression since I need to pass several variables to the functions. The last "this" is the class that calls the function and wants a returned value. The expression is called in an instance of the class Individual and the function itself is inside an instance of the class A_star, both instances are tied to the same gameObject. No separate instance of individual will create more than one thread at a time (in the case of my testing setup).

The function does not access any values except those that are passed into the function and those belonging to the A_star class.

EDIT: I've read that ThreadPool can be slow, but I get the same results using Thread().

EDIT #2: The function Path() also calls another function Neighbors (as stated in original comment). As I understand it, this function will also run inside the thread? Or is that completely wrong? Ex. Thread(()=>myFirstFunction); void myFirstFunction(){ mySecondFunction() }

Will mySecondFunction also run in the thread or on the main thread?

avatar image Fattie Salmjak · Dec 05, 2015 at 04:59 PM 1
Share

FYI you can just click "EDIT" on your original question to add more material

avatar image Hostur · Feb 27, 2018 at 09:44 PM 0
Share

Hi, Did you resolve this problem? I'm facing the same. To scale my Inside-Unity server I need more threads to proceed all the clients. I'm using the vectored loops in threaded functions like: new Thread(() => ProceedClients(new ClientsVector(0, 100))).Start(); new T...

I also use more threads in other parts like chunk manager etc. I need ~15 threads but each thread cause higher FPS drops.

0 Replies

· Add your reply
  • Sort: 

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

38 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D Game has very bad lag because of one script? 0 Answers

Scroll lagging too much 0 Answers

Android app is laggy 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