简体   繁体   English

在C(JNI)中结束Java线程

[英]Ending a Java thread in C (JNI)

Dear multithreading/Java/C/JNI gurus, 亲爱的多线程/ Java / C / JNI专家,

I have a slightly specific problem. 我有一个稍微具体的问题。 I have a Java program that spawns threads. 我有一个生成线程的Java程序。 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). 在run()方法中,调用C(通过JNI),首先,在TLS中分配线程局部变量,然后输入event_loop(因此线程的默认生存期由此循环确定) 。

My problem now is how to be able to shut down/kill the thread if something like SIGSEGV occurs. 我现在的问题是如果发生类似SIGSEGV的事情,如何能够关闭/终止线程。 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. 这就是我们使用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.) (我知道,有些人不鼓励这样做,当然做防御性编程是正确的,试图提前避免这种崩溃。这段代码仅用于迁移期,因为我们正在从C更改为Java。但是这由于我们拥有的资源很少,所以需要一些时间。)

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? 如果我使用signal.h,我的信号处理程序是否会在线程上下文中调用? Would I have access to the TLS variables? 我可以访问TLS变量吗? Could I then somehow via the env-pointer to the JVM throw an exception to break out of the run()? 我可以通过env指针以某种方式通过JVM抛出一个异常来打破run()吗? Or I could invoke interrupt() onto the Java Thread, as eg mentioned in Bruce Eckel's book (see below). 或者我可以在Java线程上调用interrupt(),例如在Bruce Eckel的书中提到的(见下文)。

Finally, one more question: SIGSEGV is Posix world, STATUS_ACCESS_VIOLATION is Windows world. 最后,还有一个问题:SIGSEGV是Posix世界,STATUS_ACCESS_VIOLATION是Windows世界。 When I tried the following in the C code in Windows: 当我在Windows中的C代码中尝试以下内容时:

 char *s = "hello world";
     *s = 'H';

I don't get SIGSEGV, (but STATUS_ACCESS_VIOLATION, I guess). 我没有得到SIGSEGV,(但我猜是STATUS_ACCESS_VIOLATION)。 Since I use signal.h, there is only a very limited set of signals available I can handle. 由于我使用的是signal.h,因此我只能处理一组非常有限的信号。 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)? 或者我会更好地使用C端的pthreads并在信号处理程序中调用pthread_exit()(从下面的第一个链接获取的想法)?

That were my questions. 那是我的问题。 I would be very thankful for any help. 我会非常感谢任何帮助。 Many thanks in advance. 提前谢谢了。

Helpful threads ;) I found: 有用的线索;)我发现:

All the signal handler has to do is persuade the function ( callNativeCFunction() ) to return. 所有信号处理程序必须做的是说服函数( callNativeCFunction() )返回。 If C has loops, have them check a module variable ( signaled , shutdown , done , or similar). 如果C有循环,请让它们检查模块变量( signaledshutdowndone或类似)。

If after the call to the function, the thread exits, it is all set. 如果在调用函数后,线程退出,则全部设置。 Otherwise maybe the function should return a status which indicates whether the thread should terminate or not. 否则,函数可能会返回一个状态,指示线程是否应该终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM