模拟线程上的死锁 [英] Simulating a deadlock on a thread
问题描述
我有3个线程,它们当前正在同时运行。
I have 3 threads, that are currently running simultaneously.
def f1():
print "running first thread\n"
sleep(10)
def f2():
print "running second thread\n"
sleep(10)
def f3():
print "running third thread\n"
sleep(10)
if __name__ == "__main__":
thread1 = Thread(target = f1)
thread2 = Thread(target = f2)
thread3 = Thread(target = f3)
try:
thread1 = Thread(target = f1)
thread1.start()
thread2 = Thread(target = f2)
thread2.start()
thread3 = Thread(target = f3)
thread3.start()
while(thread1.isAlive() or thread2.isAlive() or thread3.isAlive()):
thread1.join()
thread2.join()
thread3.join()
except (KeyboardInterrupt, SystemExit):
sys.exit()
如何模拟死锁?另外,如何让每个线程都在另一个后面运行?还可以列出脚本中当前正在运行的所有线程吗?还是给他们优先级?
How can I simulate a deadlock? Also, how can I have each thread run after the other? Also can I list all the threads that are currently running in my script? or give them priorities ?
推荐答案
如何模拟死锁?
How can I simulate a deadlock?
所有的死锁意味着一个或多个线程被阻止进行任何进度,因此您可以使用单个线程对其进行仿真。只需将设为True:
在 sleep(10)
周围即可。
All a deadlock means is that one or more threads are blocked from making any progress, so you can simulate it with a single thread. Just put a while True:
around the sleep(10)
.
在实际情况下,通常您有两个线程同时阻止彼此进行进度。例如,也许他们以相反的顺序使用了一对锁,所以线程1直到获得锁2才释放锁1,但是线程2直到获得锁1才释放锁2。最好通过使两个线程永久阻塞来模拟它。
In realistic cases, you usually have two threads blocking each other from progress at the same time. For example, maybe they've taken a pair of locks in reverse order, so thread 1 won't release lock 1 until it gets lock 2, but thread 2 won't release lock 2 until it gets lock 1. So, it might be better to simulate it by having two threads block permanently.
如果要实际创建死锁,最简单的方法是使各个线程彼此阻塞:将 thread2.join()
添加到 f1
和 thread1.join()
到 f2
。然后 f1
无法完成,直到 f2
完成, f2
在 f1
之前无法完成,所以任何人都无法完成。
If you want to actually create a deadlock, the simplest way is to have the threads literally block on each other: add thread2.join()
to f1
, and thread1.join()
to f2
. Then f1
can't finish until f2
does, f2
can't finish until f1
does, so neither one can ever finish.
但是,如果要创建一个现实的死锁,您几乎肯定会想要使用 threading.Lock
做类似上述的两锁方案。
However, if you want to create a realistic deadlock, you will almost certainly want to use synchronization objects like threading.Lock
to do something like the two-lock scenario described above.
,如何让每个线程依次运行?
Also, how can I have each thread run after the other?
好吧,最简单的方法是首先不使用线程。但是,如果您真的想要,只需执行以下操作即可:
Well, the easy way would be to not use threads in the first place. But if you really want, just do this:
thread1.start()
thread1.join()
thread2.start()
thread2.join()
thread3.start()
thread3.join()
我还可以列出脚本中当前正在运行的所有线程吗?
Also can I list all the threads that are currently running in my script?
请参见 threading.enumerate()。您通常不希望将其用于调试目的;如果希望以后(在执行操作时)访问它们,请在创建线程时跟踪它们。
See threading.enumerate(). You usually don't want to use this except for debugging purposes; keep track of the threads as you create them, if you want to have access to them later (as you're doing).
或给他们优先级?
or give them priorities ?
文档说:
当前,没有优先级,没有线程组,并且无法破坏,停止,挂起,恢复或中断线程。
currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted.
如果需要,您必须走出线程
的范围,例如,通过 ctypes
使用本机API , win32api
等
If you want this, you have to step outside of threading
and, e.g., use native APIs via ctypes
, win32api
, etc.
这篇关于模拟线程上的死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!