0
votes

i´m creating a thread that takes as parameter dispatchInputs which is an CString array but in mean while i need to use "env" in DispFrontEnd function, can a thread use the global variable "env" ? if not how how can i pass it as argument to DispFrontEnd ?

static JNIEnv *env; 
static JavaVM *jvm; 

JNIEnv* startJVM()
{
 JNIEnv *env;
 JavaVM * jvm;
 env = create_vm(&jvm);
 if ( env == NULL) { logloc->LogDebug("1env == NULL ******************");}
 return env;
}  

env = startJVM();


hDispThread = (HANDLE)_beginthread(DispFrontEnd,0,(void *)dispatchInputs);

static void DispFrontEnd(void * indArr)
{

 CString str;

 CString dest[56];
    char* orig_idx_ptr = reinterpret_cast(indArr);
    int array_size = 56; 
    memcpy(dest, orig_idx_ptr, array_size); 

 str.Format("DISPATCH INPUTS: (Trace Detail)[%s] (Session State ID)[%s] (SessionID)[%s] (MediaTypeID)[%s] (MediaCode)[%s] (5)[%s] (6)[%s] (7)[%s] (8)[%s] (9)[%s] (10)[%s] (11)[%s] (12)[%s] (13)[%s] " , dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7], dest[8], dest[9], dest[10], dest[11], dest[12], dest[13]);
 logloc->LogMethod(str );
 if (env != NULL) 
 { 

  jclass clsR = NULL;
  jclass arqClass = NULL;
  jclass messageToHostClass = NULL;

  jobject jobjRetData = NULL;

  jobjectArray jobjWOArr = NULL;
  jobjectArray getResponseArray = NULL;

  jmethodID getResponse = NULL;
  jmethodID getMessageToHost = NULL;

  arqClass = env->FindClass("BizB/Arq");


}

Tks Rev

2
try the <pre> tags to format code as codeGeorge Godik

2 Answers

2
votes

yes a thread can use the value of a global variable!

0
votes

See Is it ok to save JavaVM * and JNIEnv * as a global variable and use it elsewhere?

The JNIEnv "represents a context for a specific JNI method invocation. It is dependent on the current thread, the arguments to the current method, the local references that have been created by the current method, all sorts of things, and it becomes invalid once that JNI method returns."

Also this answer: How to create static JNI Environment Pointer?

"It's generally unsafe to cache a JNIEnv* instance and keep using it"