Search Unity

When are callback for il2cpp_thread_attach() processed?

Discussion in 'Android' started by Digika, Apr 19, 2021.

  1. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    ...
     
    Last edited: Apr 19, 2021
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    There is not any relationship between thread attach and the player loop. A background there will be attached to the VM whenever it needs to be.

    But I'm curious about why this matters? Generally, managed code should not care about il2cpp_thread_attach.
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    We don't support calls to the Unity API from C++ code, nor do we support user code calling il2cpp_thread_attach - that should only be called by Unity engine code. So this code is in uncharted territory.

    Is the code shown here in the Run function that was passed to the CreateThread API above? If so, then Unity has no control over when this code executes with respect to the player loop. The code will run when Windows schedules the thread.
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    Although access to the Unity API is not supported on other threads, some of the methods might happen to work sometimes. There is no guarantee that a call to the Unity API from a non-main thread will throw an exception.

    There is no magic here. The il2cpp_thread_attach function attaches the thread to the virtual machine, so that the garbage collector can track it. That function does not interact with Unity Engine code at all.
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    No, I don't believe that there is.