Threads

Threads created by Java are already “attached” to JVM, but if you call Java code from threads created in C++, each such thread needs to call scapix::jni::attach_thread() at least once.

By default, Scapix JNI enables automatic attachment, at the expense of slight overhead.

This is controlled by CMake option SCAPIX_JNI_AUTO_ATTACH_THREAD.

Alternatively, you can manually attach such threads:

set(SCAPIX_JNI_AUTO_ATTACH_THREAD OFF)
#include <scapix/jni/env.h>

namespace jni = scapix::jni;

void thread_func()
{
    jni::attach_current_thread();

    // ...

    jni::detach_current_thread();
}

Call jni::attach_current_thread() at least once for each thread created from C++, jni::detach_current_thread() is called automatically on thread exit. You may also manually call jni::detach_current_thread() earlier if desired.