简体   繁体   English

如何停止在共享库中实现的阻塞pthread_join()

[英]How to stop a blocking pthread_join() implemented in a shared library

My code is calling a function from a third-party library before the exit of the program. 我的代码在程序退出之前从第三方库调用函数。 Unfortunately the called function blocks the main thread, which is caused by pthread_join() in the .so library. 不幸的是,被调用的函数阻塞了主线程,这是由.so库中的pthread_join()引起的。

Since it is inside the library, which is out of my control, I am wandering how to break it so the main thread can proceed . 因为它在库中,这是我无法控制的, 所以我在徘徊如何打破它,以便主线程可以继续

Attaching the info from using gdb : 从使用gdb附加信息:

0x00007ffff63cd06d in pthread_join (threadid=140737189869312, thread_return=0x0)
    at pthread_join.c:89
89          lll_wait_tid (pd->tid);
Missing separate debuginfos, use: debuginfo-install keyutils-libs-1.4-5.el6.x86_64 krb5-libs-1.10.3-65.el6.x86_64 libcom_err-1.41.12-23.el6.x86_64 libselinux-2.0.94-7.el6.x86_64 openssl-1.0.1e-57.el6.x86_64

Thanks in advance. 提前致谢。

The library is designed to have the calling thread wait for something to finish. 该库旨在让调用线程等待完成某些事情。 Since you can't change the design of the library, just call the library from a thread that has nothing else to do. 由于您无法更改库的设计,只需从没有其他任何操作的线程调用库。

By the way you design the interaction, you can then get whatever semantics you want. 顺便提一下,您可以设计交互,然后可以获得所需的语义。 If you want the calling thread to get the results at its convenience later, you can use a promise/future. 如果您希望调用线程稍后方便地获得结果,您可以使用promise / future。 You can design the calling thread to wait a certain amount of time and then timeout. 您可以将调用线程设计为等待一段时间然后超时。 In the timeout case, you can ignore the result if you don't need it or you can design some way to check and get the result later. 在超时情况下,如果您不需要它,可以忽略结果,或者您可以设计一些方法来检查并在以后获得结果。 You can also have the thread that calls the library do whatever needs to be done with the result so that the calling thread doesn't have to worry about it. 您还可以让调用库的线程执行结果需要完成的任何操作,以便调用线程不必担心它。

Just quarantine the code you can't control and write whatever code around it you need to get the behavior your code needs. 只需隔离无法控制的代码,并编写代码所需的代码,以获得代码所需的行为。 The library needs the thread that calls it to wait until it's done, so isolate the thread that calls it and let the library have what it wants. 库需要调用它的线程等到它完成,所以隔离调用它的线程并让库具有它想要的东西。

If you call exit , the process is terminated without shutting down the other threads. 如果调用exit ,则终止进程而不关闭其他线程。

If you have a pthread_t handle for the thread that is being waited on, you can perhaps call pthread_cancel on it, but if the application and libraries are not prepared to handle thread cancellation, it will cause other problems. 如果你有一个正在等待的线程的pthread_t句柄,你可以在其上调用pthread_cancel ,但如果应用程序和库没有准备好处理线程取消,它将导致其他问题。 (Canceling the thread does pthread_join will not help because the shutdown will then block on the same thread that pthread_join waits on.) (取消线程pthread_join将无济于事,因为关闭将阻塞pthread_join等待的同一线程。)

In general, it is probably a better idea to figure out why the pthread_join call is waiting indefinitely in your environment (that is, why the other thread is not termining), and fix that. 通常,可能更好的想法是弄清楚为什么 pthread_join调用在您的环境中无限期地等待(也就是说,为什么另一个线程没有终止),并修复它。

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

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