Linux线程挂起/恢复 [英] Linux Threads suspend/resume

查看:598
本文介绍了Linux线程挂起/恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,其中有两个并行运行的线程.

I'm writing a code in which I have two threads running in parallel.

1st是启动第二个线程的主线程. 第二个线程只是执行空while循环的简单线程.

1st is the main thread which started the 2nd thread. 2nd thread is just a simple thread executing empty while loop.

现在,我想由创建它的第一个线程暂停/挂起第二个线程的执行. 一段时间之后,我想从被暂停/暂停的位置恢复第二个线程的执行(通过发出一些命令或函数).

Now I want to pause / suspend the execution of 2nd thread by 1st thread who created it. And after some time I want to resume the execution of 2nd thread (by issuing some command or function) from where it was paused / suspended.

推荐答案

这个问题不是关于如何使用互斥锁,而是关于如何挂起线程.

This question is not about how to use mutexes, but how to suspend a thread.

在Unix规范中,有一个名为pthread_suspend的线程函数,还有一个名为pthread_resume_np的线程函数,但是由于某些原因,制作Linux,FreeBSD,NetBSD等的人尚未实现这些函数.

In Unix specification there is a thread function called pthread_suspend, and another called pthread_resume_np, but for some reason the people who make Linux, FreeBSD, NetBSD and so on have not implemented these functions.

因此,要理解它,功能根本就不存在.有解决方法,但不幸的是,它与在Windows上调用SuspendThread并不相同.您必须做各种不可移植的工作才能使线程停止并开始使用信号.

So to understand it, the functions simply are not there. There are workarounds but unfortunately it is just not the same as calling SuspendThread on windows. You have to do all kinds of non-portable stuff to make a thread stop and start using signals.

停止和恢复线程对于调试器和垃圾收集器至关重要.例如,我看到过无法正确实现"SuspendThread"功能的Wine版本.因此,任何使用它的Windows程序都将无法正常工作.

Stopping and resuming threads is vital for debuggers and garbage collectors. For example, I have seen a version of Wine which is not able to properly implement the "SuspendThread" function. Thus any windows program using it will not work properly.

我认为,基于JVM将这种信号技术用于垃圾收集器这一事实,可以使用信号来正确地做到这一点,但是我也刚刚在网上看到一些文章,人们注意到僵局等等. JVM,有时无法重现.

I thought that it was possible to do it properly using signals based on the fact that JVM uses this technique of signals for the Garbage collector, but I have also just seen some articles online where people are noticing deadlocks and so on with the JVM, sometimes unreproducable.

因此要回答这个问题,除非您有一个实现pthread_suspend_np的不错的Unix,否则您无法使用Unix适当地暂停和恢复线程.否则,您将被信号卡住.

So to come around to answer the question, you cannot properly suspend and resume threads with Unix unless you have a nice Unix that implements pthread_suspend_np. Otherwise you are stuck with signals.

Signals的最大问题是,当您有大约五个不同的库都链接到同一个程序,并且都试图同时使用相同的信号时.因此,我相信您实际上不能在一个程序中使用ValGrind之类的东西,例如Boehm GC.至少在用户空间的最低级别上没有大型编码.

The big problem with Signals is when you have about five different libraries all linked in to the same program and all trying to use the same signals at the same time. For this reason I believe that you cannot actually use something like ValGrind and for example, the Boehm GC in one program. At least without major coding at the very lowest levels of userspace.

这个问题的另一个答案可能是.做Linuz Torvalds对NVidia所做的事情,朝他张开手指,让他实现Linux缺少的两个最关键的部分.首先是pthread_suspend,其次是内存页上的脏位,以便可以实现适当的垃圾收集器.在线发起大型请愿书,然后继续动动手指.也许当Windows 20发布时,他们会意识到挂起和恢复线程,并拥有脏位实际上是Windows和Mac比Linux更好的根本原因之一,或者是任何未实现pthread_suspend的Unix也很脏.在虚拟页面上,就像VirtualAlloc在Windows中一样.

Another answer to this question could be. Do what Linuz Torvalds does to NVidia, flip the finger at him and get him to implement the two most critical parts missing from Linux. First, pthread_suspend, and second, a dirty bit on memory pages so that proper garbage collectors can be implemented. Start a large petition online and keep flipping that finger. Maybe by the time Windows 20 comes out, they will realise that Suspending and resuming threads, and having dirty bits is actually one of the fundamental reasons Windows and Mac are better than Linux, or any Unix that does not implement pthread_suspend and also a dirty bit on virtual pages, like VirtualAlloc does in Windows.

我没有希望.实际上,对我来说,我花了很多年时间来规划我的未来,以便为Linux构建东西,但放弃了希望,因为可靠的事情似乎都取决于虚拟内存是否可用以及是否干净地挂起线程.

I do not live in hope. Actually for me I spent a number of years planning my future around building stuff for Linux but have abandoned hope as a reliable thing all seems to hinge on the availability of a dirty bit for virtual memory, and for suspending threads cleanly.

这篇关于Linux线程挂起/恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆