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
1
Question by WickedSicked · Jan 22, 2018 at 10:12 AM · plugindllnative pluginthreadingasynchronous

Are calls to C++ DLL handled asynchronously?

Hi everyone,

When I call an imported function (from a C++ DLL), will Unity stall until the function finishes executing, or will it still run asynchronously? The dll does some extensive computing that takes 30-100ms, and I don't want the program to lag during that time.

Thanks in advance!

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
3

Answer by Lysander · Jan 22, 2018 at 11:20 AM

That doesn't have anything to do with the DLL, or Unity, but how the function's set up and/or how you're calling it. Async doesn't just happen, no, and there's nothing about calling a function in a DLL that's going to automatically run it asynchronously.

There's a lot of good information in the .NET documentation of course, and you might also want to look into BeginInvoke/EndInvoke and Tasks. Here's a really good article specifically about using async in Unity. And of course check out any documentation on threading / multithreading / threadpooling.

Comment
Add comment · Show 3 · 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 WickedSicked · Jan 22, 2018 at 12:26 PM 0
Share

Thanks for the reply!

How do I know if the function can be broken up into smaller chunks though? Just try it out?

I probably should have phrased my question a bit differently, what I was really wondering is if the method call will run on Unity's main thread, because if so, I wouldn't have to do any of the async calling stuff, would I?

I took a look at the links you provided, and as I see it I can either call the method async from within Unity, or have it execute async in the DLL itself using std::async. Would one method be preferable over the other?

Cheers

avatar image Bunny83 · Jan 23, 2018 at 02:57 PM 1
Share

Some things you mentioned in this answer doesn't seem to be quite right. First of all when someone talks about a C++ DLL we usually assume that we talk about normal C++, not about C++ / CLI. I actually don't know anybody who uses C++ / CLI. $$anonymous$$ost C++ programmers stay away from managed environments ^^.


So when using a normal C++ DLL in Unity we actually have a managed to native call. Native code is not a CIL assembly.


Coroutines are not async operations. Coroutines run synchonously on the main thread. Coroutines just can be interrupted and resumed at a later point in time.


BeginInvoke / EndInvoke and Tasks are actual asynchronous calls which will usually be executed on a seperate thread. Async calls do not block the main thread unless you immediately call EndInvoke which will block until the actual method has completed. If you look at the example code on the page you've linked for BeginInvoke / EndInvoke you will notice that they print the managed thread id in each case and that it actually runs on a seperate thread, That's why actual async calls and Tasks can only barely be used in Unity since you can't use most of the Unity API in such calls.


The new C# 5 features async and await are a bit special. The async keyword doesn't make the method asynchronous "automagically". It just provides the possibility to run certain things asynchronous. However it soley depends on the "awaitable". So on the bottom line it works very similar to Unity's coroutines (.NET iterator blocks) but actually focus on the task / code and not on yield values.

avatar image Lysander Bunny83 · Jan 24, 2018 at 05:25 AM 0
Share

I defer to your expertise- my misunderstanding with regards to async is indeed because my only real exposure to it has been with the C#5+ features (and not much exposure at that, since Unity operated in the dark ages for so long). Despite the name, it seemed to me that async methods ran on the same thread unless specifically made to run on another, making them more like coroutines, and only occasionally even that.

$$anonymous$$y mistake. I've updated my answer to cut out most of the incorrect information, which sadly leaves it a bit lacking, but at least the documentation links are useful.

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

76 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

Related Questions

Referencing a DLL in C# 0 Answers

Unity 5 Managed DLL considered Native 0 Answers

How do you use a class from a .dll file? 1 Answer

External dll: namespace recognized by mono, not by UT 2 Answers

Pass C# struct containing Matrix to C++ dll 0 Answers


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