Ending a Java thread in C (JNI)

Dear multithreading/Java/C/JNI gurus, I have a slightly specific problem. I have a Java program that spawns threads. In the run() method, a call to C is made (via JNI), where at first, thread local variables are allocated in TLS, an then an event_loop is entered (so the default lifetime of the thread is determined by this loop). My problem now is how to be able to shut down/kill the thread if something like SIGSEGV occurs. It would be important that the whole process and the other threads within it could continue. That's why we separated the threads by using TLS. (I know, this is discouraged by some people and of course it is right to do defensive programming, trying to avoid such crashes in advance. This code is intended for a migration period only, since we are changing from C to Java. But this will take some time due to the small amount of resources we have.) class MyThread extends Thread { run() { //wrapping the following with try-catch and throwing exception in signal //handler on C side possible? callNativeCFunction(); //allocates TLS, enters event_loop() } } If I would use signal.h, would my signal handler be invoked in thread context? Would I have access to the TLS variables? Could I then somehow via the env-pointer to the JVM throw an exception to break out of the run()? Or I could invoke interrupt() onto the Java Thread, as e.g. mentioned in Bruce Eckel's book (see below). Finally, one more question: SIGSEGV is Posix world, STATUS_ACCESS_VIOLATION is Windows world. When I tried the following in the C code in Windows: char *s = "hello world"; *s = 'H'; I don't get SIGSEGV, (but STATUS_ACCESS_VIOLATION, I guess). Since I use signal.h, there is only a very limited set of signals available I can handle. Do you know how I could handle the above case, too? Or would I be better off with pthreads on the C side and calling pthread_exit() in the signal handler (idea taken from first link below)? That were my questions. I would be very thankful for any help. Many thanks in advance. Helpful threads ;) I found: - https://stackoverflow.com/questions/1801788/handling-segfault-signal-sigsegv-need-to-determine-the-cause-of-segfault-using-si - https://stackoverflow.com/questions/4645870/how-to-debug-sigsegv-in-jvm-gctaskthread - Bruce Eckel's Thinking in Java, 4th ed., p. 1194, “Checking for an interrupt”

以上就是Ending a Java thread in C (JNI)的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准