Search Unity

natives calls are on the same thread?

Discussion in 'Android' started by ddfire, Jan 6, 2011.

  1. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    hi
    if i make a call to a native function C++ or java it will lock the game until this function end?
    or the call is in other thread?

    i need to get some info from a server, and it can take some time, what is the proper way to do this?
    thanks
    David
     
  2. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    Could you use the WWW class?
     
  3. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    Or start a thread in native using pthreads. I've done that before.
     
  4. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    It's called on the 'same' thread. (ie, whatever thread that initiated the call into native/java).
     
  5. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    hi
    and each update and/or script call is in its own thread or it will lock the game until it cames back?
    thanks
     
  6. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Not sure what what you mean by 'update'; but if you call a native function (or java, which is considered native when coming from scripts) on a particular thread, then it will immediately execute the native call, on that thread. Any other thread will continue to run normally (unless there are other thread locking primitives (mutex/semaphore/'synchronized' etc) in play), but obviously the thread making the native call will be 'blocked' (as it is performing the call itself).
    There is nothing 'magic' or different about a native call compared to a managed call to a C#/JS class method in this regard.
     
  7. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    i read each script run the "update" function every frame or something like that
     
  8. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    the ::Update function is always called on the main thread. If you do native calls (or any calls) on the main thread, the main thread will stall.
     
  9. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    what is the best way to make an async call ?
    i need to make a call to a webservice and show the result but off cource the rest of the game must be responsive.
     
  10. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Did you try the WWW api, as suggested by DTreble?