- Home /
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!
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.
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
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.
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
![](https://koobas.hobune.stream/wayback/20220612151037im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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