开发者

Can you cache JNIEnv?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜