简体   繁体   English

同步主线程和工作线程

[英]Synchronising main thread and worker thread

In QT, from main(GUI) thread I am creating a worker thread to perform a certain operation which accesses a resource shared by both threads. 在QT中,我从main(GUI)线程创建一个工作线程来执行某些操作,该操作访问两个线程共享的资源。 On certain action in GUI, main thread has to manipulate the resource. 在GUI中执行某些操作时,主线程必须操纵资源。 I tried using QMutex to lock that particular resource. 我尝试使用QMutex锁定该特定资源。 This resource is continuously used by the worker thread, How to notify main thread on this? 该资源由工作线程连续使用,如何在此通知主线程? Tried using QWaitCondition but it was crashing the application. 使用QWaitCondition尝试过,但是它使应用程序崩溃了。

Is there any other option to notify and achieve synchronisation between threads? 还有其他选项可以通知并实现线程之间的同步吗? Attached the code snippet. 附加了代码段。

void WorkerThread::IncrementCounter()
{
    qDebug() << "In Worker Thread IncrementCounter function" << endl;
    while(stop == false)
    {
        mutex.lock();
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 100; j++)
            {
                counter++;
            }
        }

        qDebug() << counter;
        mutex.unlock();
    }
    qDebug() << "In Worker Thread Aborting " << endl;
}

//Manipulating the counter value by main thread.
void WorkerThread::setCounter(int value)
{
    waitCondition.wait(&mutex);
    counter = value;
    waitCondition.notify_one();
}

You are using the wait condition completely wrong. 您使用的等待条件完全错误。 I urge you to read up on mutexes and conditions, and maybe look at some examples . 我敦促您阅读互斥量和条件,也许看看一些示例

wait() will block execution until either notify_one() or notify_all() is called somewhere. wait()将阻止执行,直到在某处调用notify_one()或notify_all()为止。 Which of course cannot happen in your code. 当然,这不可能发生在您的代码中。
You cannot wait() a condition on one line and then expect the next two lines to ever be called if they contain the only wake up calls. 您不能在一行上使用wait()条件,然后如果它们包含唯一的唤醒调用,则不能期望接下来的两行被调用。

What you want is to wait() in one thread and notify_XXX() in another. 您想要的是在一个线程中使用wait(),在另一个线程中使用notify_XXX()。

You could use shared memory from within the same process. 您可以在同一过程中使用共享内存。 Each thread could lock it before writing it, like this: 每个线程可以在编写之前将其锁定,如下所示:

QSharedMemory *shared=new QSharedMemory("Test Shared Memory");

if(shared->create(1,QSharedMemory::ReadWrite))
{
    shared->lock();

    // Copy some data to it
    char *to = (char*)shared->data();
    const char *from = &dataBuffer;
    memcpy(to, from, dataSize);

    shared->unlock();
}

You should also lock it for reading. 您还应该锁定它以便阅读。 If strings are wanted, reading strings can be easier that writing them, if they are zero terminated. 如果需要使用字符串,则以零结尾的情况下,读取字符串要比编写字符串更容易。 You'll want to convert .toLatin1() to get a zero-terminated string which you can get the size of a string. 您需要将.toLatin1()转换为以零结尾的字符串,该字符串可以获取字符串的大小。 You might get a lock that multiple threads can read from, with shared->attach(); 您可以使用shared-> attach()获得一个可以读取多个线程的锁。 but that's more for reading the shared memory of a different process.. 但这更多是为了读取不同进程的共享内存。

You might just use this instead of muteces. 您可能只使用它而不是静音。 I think if you try to lock it, and something else already has it locked, it will just block until the other process unlocks it. 我认为,如果您尝试锁定它,而其他东西已经将其锁定,它将阻塞直到其他进程将其解锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM