是的disconnect()
,并且connect()
正在使用互斥锁进行保护。
这些是disconnect()
函数的第一行:
bool QMetaObjectPrivate::disconnect(const QObject *sender, int signal_index,
const QObject *receiver, int method_index,
DisconnectType disconnectType)
{
if (!sender)
return false;
QObject *s = const_cast<QObject *>(sender);
QMutex *senderMutex = signalSlotLock(sender);
QMutex *receiverMutex = receiver ? signalSlotLock(receiver) : 0;
QOrderedMutexLocker locker(senderMutex, receiverMutex);
这是connect()
函数的第一行:
bool QMetaObjectPrivate::connect(const QObject *sender, int signal_index,
const QObject *receiver, int method_index, int type, int *types)
{
QObject *s = const_cast<QObject *>(sender);
QObject *r = const_cast<QObject *>(receiver);
QOrderedMutexLocker locker(signalSlotLock(sender),
signalSlotLock(receiver));
如果您查看QObject
文档,您可以看到:
注意:该类中的所有函数都是可重入的,但connect()、connect()、disconnect() 和 disconnect() 也是线程安全的。
编辑
当发出信号时,会调用 QMetaObject::activate 函数来锁定发送者的对象互斥锁:
void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index,
void **argv)
{
...
QMutexLocker locker(signalSlotLock(sender));