开发者

Call Thread.yield() or equivalent from C (NDK)

I have a an AsyncTask in Android 2.3.3 with SDK 2.2.

Within the 开发者_运维技巧task/class I am utilizing a C-library with the NDK.

Instead of having a while(condition) loop in Java, I have this loop in C to avoid expensive calls via JNI during each loop iteration.

If I had the while(condition) loop in Java I would call Thread.yield() at the end of each loop-iteration to allow the Thread to get suspended.

Is there a possibility in C to get the same behaviour? I tried it with usleep(10) at the the end of the while(condition) loop in C but I did not get the expected behaviour.

Any suggestions?


If one really wants to call Thread.yield() from C-Code one could do the following:

Java_de_company_MyClass_setupJNI(JNIEnv* env, jobject thiz) {

    (...snip...)

    jclass threadClass = (*env)->FindClass(env, "java/lang/Thread");
    jmethodID yieldFunctionID = (*env)->GetStaticMethodID(env, threadClass, "yield", "()V");

    (*env)->CallStaticVoidMethod(env, threadClass, yieldFunctionID);

    (...snip...)    
}

Don't forget error checking.


You can use sched_yield()

(edited after Steven Bell's comment)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜