在多线程中使用输入参数调用函数 [英] Calling a function with input arguments in multithreading

查看:221
本文介绍了在多线程中使用输入参数调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,几个线程可以同时调用它.该函数有一个输入参数.例如,请考虑以下功能:

I have a function which several threads may call it simultaneously. This function has an input argument. For example, Please consider the following function:

bool MyClass::run(QString moduleName)
{
    qDebug() << QThread::currentThreadId();
    ...
}

我有两个使用此功能的选项.首先,我可以在run函数中使用互斥锁,如下所示:

I have two options for using this function. First I can use a mutex inside the run function like below:

...
run("Reza"); // can be different for different threads
...
bool MyClass::run(QString moduleName)
{
    qDebug() << QThread::currentThreadId();
    QMutexLocker locker(&runMutex);
    ...
}

第二个是在调用此函数时使用互斥锁,如下所示:

The second is using a mutex when calling this function like below:

...
runMutex.lock();
run("Reza"); // can be different for different threads
runMutex.unlock();
...
bool MyClass::run(QString moduleName)
{
    qDebug() << QThread::currentThreadId();
    ...
}

哪个是正确的和更好的?输入参数(moduleName)在第一个选项中是否更改? (我的意思是在调用线程时将不同的参数传递给该函数)

which one is correct and better? Does the input argument (moduleName) change in the first option? (I mean when calling thread pass a different argument to this function)

预先感谢您的帮助. 雷扎

Thanks in advance for your helps. Reza

推荐答案

在许多情况下,同时具有 都是有意义的!内部获取互斥锁的功能使此功能更容易使用,并且通常更安全.但是,在显式地使用互斥锁时,除了run()方法执行的操作之外,您还希望执行其他操作,同时还要保持锁定,这是很常见的.在那种情况下,您将拥有另一个获得锁定且无法调用内部锁定功能的函数(嗯,除非互斥锁是递归互斥锁,我并不认为这是个好主意).出路是使用该函数的版本,例如run_locked()或具有不同参数的版本(假定已获取锁).

In many contexts it makes sense to have both! The function acquiring a mutex internally makes use of this function easier and generally safer to use. However, When using mutexes explicitly it is quite common that you'll want to do additional operations in addition to those done by the run() method while also holding the lock. In that case you'd have another function which acquires the lock and which can't call the internally locking function (well, unless the mutex is a recursive mutex which I don't really think is ever a good idea). The way out is to have a version of the function, e.g., run_locked() or a version with different parameters which assumes the lock is acquired.

在使用显式锁定时(我通常会尝试避免这样做,因为我无法解释使用锁定的代码),发现具有这样的相应功能对很有帮助(我通常会使用std设施) :

When using explicit locking (I generally try to avoid doing so because I can't reason about code using locks) I found it helpful to have have corresponding pairs of function like this (I'd normally use std facilities):

bool MyClass::run(QString moduleName) {
    QMutexLocker kerberos(&runMutex);
    return this->run(kerberos, moduleName);
}
bool MyClass::run(QMutexLocker& kerberos, QString moduleName) {
    // do whatever work is needed here
}

传递锁保护对象可确保周围有一个获得的保护,即用户不能在没有任何获得的锁保护的情况下仅调用期望获得该锁的函数.为错误的锁购置锁卫可能会导致误用.

Passing the lock-guard object makes sure there is an acquired guard around, i.e., a user can't just call the function expecting the lock to be acquired without any acquired lock guard. There is a potential misuse by acquiring a lock guard for the wrong lock.

这篇关于在多线程中使用输入参数调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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