Skip to content

Latest commit

 

History

History
65 lines (52 loc) · 1.42 KB

4.2.md

File metadata and controls

65 lines (52 loc) · 1.42 KB

Wait/Notify

public class JNIWaitNotify extends BaseOperation {
    @Override
    public void invoke() {
        waitNativeThread();
        notifyNativeThread();
    }

    public native void  waitNativeThread();

    public native void  notifyNativeThread();
}
pthread_mutex_t mutex;
pthread_cond_t cond;

pthread_t waitHandle;
pthread_t notifyHandle;

int flag = 0;

void *waitThread(void *) {
    LOGD("wait thread lock");
    pthread_mutex_lock(&mutex);
    while (flag == 0) {
        LOGI("waiting");
        pthread_cond_wait(&cond, &mutex);
    }
    LOGI("wait thread unlock");
    pthread_mutex_unlock(&mutex);
    pthread_exit(0);
}

void *notifyThread(void *) {
    LOGD("notify thread lock");
    pthread_mutex_lock(&mutex);
    flag = 1;
    pthread_mutex_unlock(&mutex);
    pthread_cond_signal(&cond);
    LOGD("signal");
    LOGD("notify thread unlock");
    pthread_exit(0);

}

extern "C"
JNIEXPORT void JNICALL
Java_com_jdqm_ndktutorials_jni_JNIWaitNotify_waitNativeThread(JNIEnv *env, jobject thiz) {
    pthread_mutex_init(&mutex, nullptr);
    pthread_cond_init(&cond, nullptr);
    pthread_create(&waitHandle, nullptr, waitThread, nullptr);
}



extern "C"
JNIEXPORT void JNICALL
Java_com_jdqm_ndktutorials_jni_JNIWaitNotify_notifyNativeThread(JNIEnv *env, jobject thiz) {
    pthread_create(&notifyHandle, nullptr, notifyThread, nullptr);
}