开发者

Can you cache JNIEnv?

开发者 https://www.devze.com 2023-03-29 06:43 出处:网络
Is it safe to cache a JNIEnv object across multiple JNI calls? For example, can I d开发者_运维技巧o something like

Is it safe to cache a JNIEnv object across multiple JNI calls? For example, can I d开发者_运维技巧o something like

void foo(JNIEnv* env)
{
    static JNIEnv* cached;
    if( ! cached )
        cached = env;

    /* use cached */
}

Of course, the code I have in reality isn't anything this silly, but the above example does demonstrate the usage case well.


Yes, you can, but only within a single thread. Your cache can't be as simple as the above unless you are sure, somehow, that your native code will only be called on a single thread. Otherwise you need to use thread-local storage to hold your JNIEnv references.

0

精彩评论

暂无评论...
验证码 换一张
取 消