Today, I will briefly explain how android NDK calls functions in a C ++ class.
[Cpp] view plaincopyprint? // # Include <iostream>
# Include <stdio. h>
# Include <pthread. h>
# Include <jni. h>
# Include <android/log. h>
// # Include "testport. h"
Static const char * TAG = "Acanoe ";
# Define LOGI (fmt, args...) _ android_log_print (ANDROID_LOG_INFO, TAG, fmt, # args)
# Define LOGD (fmt, args...) _ android_log_print (ANDROID_LOG_DEBUG, TAG, fmt, # args)
# Define LOGE (fmt, args...) _ android_log_print (ANDROID_LOG_ERROR, TAG, fmt, # args)
Extern "C "{
Using namespace std;
Extern pthread_cond_t ntcond;
Extern pthread_mutex_t ntmutex;
Class TestPort {
Public:
Explicit TestPort ();
~ TestPort ();
Static void * thr_fn (void * arg );
Void InitPort ();
Private:
Pthread_t ntid;
};
Pthread_cond_t ntcond = PTHREAD_COND_INITIALIZER;
Pthread_mutex_t ntmutex = PTHREAD_MUTEX_INITIALIZER;
TestPort: TestPort (void ){
}
TestPort ::~ TestPort (void ){
}
Void TestPort: InitPort ()
{
Pthread_create (& ntid, NULL, thr_fn, NULL );
}
Void * TestPort: thr_fn (void * arg)
{
LOGD ("thread_create success ");
Pthread_mutex_lock (& ntmutex );
Pthread_cond_signal (& ntcond );
Pthread_mutex_unlock (& ntmutex );
}
}
Extern "C "{
Jint Java_com_example_pthread_Ptrhead_cjjtest (JNIEnv * env, jclass thiz );
};
Jint Java_com_example_pthread_Ptrhead_cjjtest
(JNIEnv * env, jclass thiz)
{
LOGD ("cjjtest start ");
TestPort test;
Test. InitPort ();
Pthread_mutex_lock (& ntmutex );
Pthread_cond_wait (& ntcond, & ntmutex );
Pthread_mutex_unlock (& ntmutex );
LOGD ("got the signal. \ n ");
}
// # Include <iostream>
# Include <stdio. h>
# Include <pthread. h>
# Include <jni. h>
# Include <android/log. h>
// # Include "testport. h"
Static const char * TAG = "Acanoe ";
# Define LOGI (fmt, args...) _ android_log_print (ANDROID_LOG_INFO, TAG, fmt, # args)
# Define LOGD (fmt, args...) _ android_log_print (ANDROID_LOG_DEBUG, TAG, fmt, # args)
# Define LOGE (fmt, args...) _ android_log_print (ANDROID_LOG_ERROR, TAG, fmt, # args)
Extern "C "{
Using namespace std;
Extern pthread_cond_t ntcond;
Extern pthread_mutex_t ntmutex;
Class TestPort {
Public:
Explicit TestPort ();
~ TestPort ();
Static void * thr_fn (void * arg );
Void InitPort ();
Private:
Pthread_t ntid;
};
Pthread_cond_t ntcond = PTHREAD_COND_INITIALIZER;
Pthread_mutex_t ntmutex = PTHREAD_MUTEX_INITIALIZER;
TestPort: TestPort (void ){
}
TestPort ::~ TestPort (void ){
}
Void TestPort: InitPort ()
{
Pthread_create (& ntid, NULL, thr_fn, NULL );
}
Void * TestPort: thr_fn (void * arg)
{
LOGD ("thread_create success ");
Pthread_mutex_lock (& ntmutex );
Pthread_cond_signal (& ntcond );
Pthread_mutex_unlock (& ntmutex );
}
}
Extern "C "{
Jint Java_com_example_pthread_Ptrhead_cjjtest (JNIEnv * env, jclass thiz );
};
Jint Java_com_example_pthread_Ptrhead_cjjtest
(JNIEnv * env, jclass thiz)
{
LOGD ("cjjtest start ");
TestPort test;
Test. InitPort ();
Pthread_mutex_lock (& ntmutex );
Pthread_cond_wait (& ntcond, & ntmutex );
Pthread_mutex_unlock (& ntmutex );
LOGD ("got the signal. \ n ");
}
A simple explanation:
In fact, it is to use C ++
Extern "C "{
}
So that the C function can call the C ++ function, and then your real Jni function body.