Zuerst entscheiden, welche Werkzeugkette zu verwenden, Ihre "application.mk" bearbeiten (nicht zu verwechseln mit android.mk) und legen Sie für gcc 4.8:
NDK_TOOLCHAIN_VERSION := 4.8
oder wenn Sie Klirren wollen:
NDK_TOOLCHAIN_VERSION := clang
Das hat aber nichts mit Threads zu tun. Dies definiert nur, welche Toolchain zu verwenden ist.
Jetzt über Themen, hier ist ein einfaches Beispiel für Android NDK:
#include <pthread.h> // <--- IMPORTANT
// This will be used to pass some data to the new thread, modify as required
struct thread_data_arguments
{
int value_a
bool value_b;
};
//---------------------------------
// This function will be executed in the new thread, do not forget to put * at the start of the function name declaration
void *functionRunningInSeparateThread(void *arguments)
{
struct thread_data_arguments *some_thread_arguments = (struct thread_data_arguments*)arguments;
if (some_thread_arguments->value_b == true)
{
printf("VALUE= %i", some_thread_arguments->value_a);
}
// Signal the end of the thread execution
pthread_exit(0);
}
//---------------------------------
// This is the actual function creating and starting the new thread
void startThread()
{
// Lets pass some data to the new thread, you can pass anything even large data,
// for that you only need to modify thread_data_arguments as required
struct thread_data_arguments *some_thread_arguments;
some_thread_arguments = (thread_data_arguments*)malloc(sizeof(*some_thread_arguments));
some_thread_arguments->value_a = 12345;
some_thread_arguments->value_b = true;
// Create and start the new thread
pthread_create(&native_thread, NULL, functionRunningInSeparateThread, (void*)some_thread_arguments)
}
Siehe z.B. http://stackoverflow.com/questions/15269496/how-to-compile-c11-code-with-android-ndk-and-eclipse und http: // stackoverflow.com/questions/14014659/android-ndk-error-muss-aktiviert-mit-der-std-c11-oder-std-gnu11-compiler – Michael
@Michael, ich muss mit clang 3,2 oder gcc 4,8 bauen, oder sein sicher, dass es nicht möglich ist und pthreads verwenden. – Kimi
Seit NDK Revision 10d - GCC 4.8 ist der Standard für alle 32-Bit ABIs –