我正在使用windbg(最新的MSDN下载页面).
我正在尝试在我的应用程序中调试死锁,并且!锁定非常有用.但是,它不起作用:
0:023> !locks NTSDEXTS: Unable to resolve ntdll!RTL_CRITICAL_SECTION_DEBUG type
NTSDEXTS: Please check your symbols
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会感到不安.我已正确加载符号:
0:023> .sympath
Symbol search path is: srv*
Expanded Symbol search path is: cache*c:\debuggers\sym;SRV*http://msdl.microsoft.com/download/symbols
Run Code Online (Sandbox Code Playgroud)
并且NTSD同意:
0:023> lmv m ntdll
start end module name
777b0000 77930000 ntdll (pdb symbols) c:\debuggers\sym\wntdll.pdb\E9D10FA3EB884A23A5854E04FB7E2F0C2\wntdll.pdb
Loaded symbol image file: C:\Windows\SysWOW64\ntdll.dll
Image path: ntdll.dll
Image name: ntdll.dll
Timestamp: Mon Jul 13 18:11:23 2009 (4A5BDB3B)
CheckSum: 00148A78
ImageSize: 00180000
File version: 6.1.7600.16385
Product version: 6.1.7600.16385
File flags: 0 (Mask 3F)
File OS: 40004 …Run Code Online (Sandbox Code Playgroud) 我正在考虑使用类变量作为线程锁,因为我不想在全局变量中定义锁并且还想防止死锁.这实际上有用吗?例:
import threading
class A(object):
lock = threading.Lock()
a = 1
@classmethod
def increase_a(cls):
with cls.lock:
cls.a += 1
Run Code Online (Sandbox Code Playgroud)
考虑到我不会A.lock在类的内部或外部重新分配变量,我的假设是它被视为全局锁定?它是否正确?
我知道Python线程一次只能执行一个字节码,那么为什么线程库会提供锁?我假设如果一次只执行一个线程,则不会发生竞争条件.
该库提供锁,条件和信号量.这是同步执行的唯一目的吗?
更新:
我做了一个小实验:
from threading import Thread
from multiprocessing import Process
num = 0
def f():
global num
num += 1
def thread(func):
# return Process(target=func)
return Thread(target=func)
if __name__ == '__main__':
t_list = []
for i in xrange(1, 100000):
t = thread(f)
t.start()
t_list.append(t)
for t in t_list:
t.join()
print num
Run Code Online (Sandbox Code Playgroud)
基本上我应该启动100k线程并递增1.返回的结果是99993.
a)如果有GIL同步和避免竞争条件,结果如何不是99999?b)甚至可以启动100k OS线程吗?
看到答案后更新2:
如果GIL没有真正提供一种方法来执行简单的操作,例如原子递增,那么将它放在那里的目的是什么?它对于讨厌的并发问题没有帮助,那么它为什么要到位呢?我听说过C扩展的用例,有人会举例说明吗?
任何人都可以通过示例解释 mongodb 中的意图共享和意图排他锁吗?
我阅读了它们的功能,但我无法弄清楚它们在真实数据库示例中的实际用途。
更新:(更多信息)
假设:mongodb版本在3.0.0以上
创建文档时会发生什么?在不同层获取哪些锁:DB、Collection 或文档?(S、X、IS 或 IX)
我正在使用 python 3.6.7 和 Ubuntu 18.04
运行以下脚本后,每个进程都有自己的共享锁:
from multiprocessing import Process, Manager
def foo(l1):
with l1:
print('lol')
if __name__ == '__main__':
processes = []
with Manager() as manager:
for cluster in range(10):
lock1 = manager.Lock()
calc_args = (lock1, )
processes.append(Process(target=foo,
args=calc_args))
for p in processes:
p.start()
for p in processes:
p.join()
Run Code Online (Sandbox Code Playgroud)
我有一个奇怪的例外:
Process Process-2:
Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "temp.py", line 5, in foo …Run Code Online (Sandbox Code Playgroud) C编程:
当一个线程试图获取一个互斥锁并且无法获得它时会发生什么?
它睡觉了吗?
线程是否会在pthread_mutex_unlock(&mutex)时被唤醒; 叫做?
然后尝试再次获得锁定?
但我正在同步"名册"对象,无论它到处都是新的.怎么会 ?
违规代码:
public Roster getRoster() {
if (roster == null) {
return null;
}
if (!roster.rosterInitialized) {
try {
synchronized (roster) {
roster.reload();
long waitTime = SmackConfiguration.getPacketReplyTimeout();
long start = System.currentTimeMillis();
while (!roster.rosterInitialized) {
if (waitTime <= 0) {
break;
}
roster.wait(waitTime);
long now = System.currentTimeMillis();
waitTime -= now - start;
start = now;
}
}
}
catch (InterruptedException ie) {
// Ignore.
}
}
return roster;
}
Run Code Online (Sandbox Code Playgroud) 我有几个继承自ClassA的对象,它有一个抽象方法MethodA.
这些继承对象中的每一个都可以同时允许特定数量的线程进入其MethodA.
catch:线程只能在对象的MethodA中,而没有其他对象的MethodA同时被处理.
我怎么解决这个问题?我正在考虑使用信号量,但不知道该怎么做,因为我无法完全解决问题以获得解决方案.
编辑:
示例代码(可能包含语法错误:)
public class ClassA
{
public virtual void MethodA{}
}
public class OneOfMySubclassesOfClassA // there can be multiple instances of each subclass!
{
public override void MethodA{
// WHILE any number of threads are in here, THIS MethodA shall be the ONLY MethodA in the entire program to contain threads
EDIT2: // I mean: ...ONLY MethodA of a subclass (not of a instance of a subclass) in the entire program...
}
}
...and more subclasses...
Run Code Online (Sandbox Code Playgroud) 我正在阅读 Remzi 教授的 OSTEP 书 http://pages.cs.wisc.edu/~remzi/OSTEP/
我只能部分理解以下代码如何导致唤醒/等待竞争条件。(代码取自书籍章节。http: //pages.cs.wisc.edu/~remzi/OSTEP/threads-locks.pdf
void lock(lock_t *m) {
while (TestAndSet(&m->guard, 1) == 1); //acquire guard lock by spinning
if (m->flag == 0) {
m->flag = 1; // lock is acquired
m->guard = 0;
} else {
queue_add(m->q, gettid());
m->guard = 0;
park();
}
}
}
void unlock(lock_t *m) {
while (TestAndSet(&m->guard, 1) == 1); //acquire guard lock by spinning
if (queue_empty(m->q))
m->flag = 0; // let go of lock; no one wants it
else
unpark(queue_remove(m->q)); // …Run Code Online (Sandbox Code Playgroud) 在调试转储文件时,我需要定期检查锁,为此我使用windbg扩展命令!locks。当一切顺利时,这将提供如下输出:
CritSec +54a8a8 at 0054a8a8
WaiterWoken No
LockCount 0
RecursionCount 1
OwningThread 13d8
EntryCount 0
ContentionCount 0
*** Locked
CritSec +b73a8d at 00135e8d
WaiterWoken No
LockCount 0
RecursionCount 1
OwningThread 55f3
EntryCount 0
ContentionCount 0
*** Locked
...
Scanned 662 critical sections
Run Code Online (Sandbox Code Playgroud)
但是,有时我会收到以下错误消息:
Stopped scanning because of problem reading critical section debug info
Scanned 7 critical sections
Run Code Online (Sandbox Code Playgroud)
是否可以提供一些信息?(例如,它在哪里critical section debug info,我如何在没有!locks命令的情况下读取它,...)