简体   繁体   English

使用互斥锁后程序仍然崩溃(我可以在同一线程中使用互斥锁吗?)

[英]Program still crashes after using mutex(can i use mutex in the same thread?)

I created two threads, and use mutex to synchronize them. 我创建了两个线程,并使用互斥锁对其进行同步。
In the mainwindow program(which i regard as the main thread) in which the other thread is created, I have to use mutex in at least two functions , because one is a slot to accept signals from UI when user selects a menu and configure the data , and there is also a timer which runs out 1 time per sec and triggers a slot function which reads the data. 在创建另一个线程的mainwindow程序(我认为是主线程)中,我必须在至少两个函数中使用互斥锁 ,因为一个是一个插槽,用于在用户选择菜单并配置菜单时接受来自UI的信号。 数据 ,并且还存在其用完每秒和触发器的槽函数,其读取数据 1周时间的计时器

My program often crashes even i use mutex . 即使我使用互斥锁,我的程序也会经常崩溃 In 'main thread' there are different functions which have mutex's lock and unlock operations, one of the functions is a slot linked to the timer. 在“主线程”中,具有互斥量的锁定和解锁操作的不同功能,其中一个功能是链接到计时器的插槽。 Also the other thread continuously writes the data . 另外,另一个线程连续写入数据

I am so confused, why ? 我很困惑,为什么?
(:) I really need a better phone to edit my question before this time :) ) (:)在这段时间之前,我真的需要一个更好的电话来编辑我的问题:))

My code: 我的代码:
In thread: 在线程中:

class Background : public QThread
{
    Q_OBJECT

public:
    void Background::run(void)
    {
        initFile();

        while(1)
        {

            Mutex->lock();
            msleep(40);
            rcv();          //writes map here
            Mutex->unlock();

        }

    }
...
}

In thread's rcv(): 在线程的rcv()中:

void Background::rcv()
{
    DEVMAP::iterator dev_r;

    for(dev_r= DevMap.begin(); dev_r!= DevMap.end(); dev_r++)//DevMap is a refrence to the dev_map in mainwindow.
    {
       ...  ....//writes the map

    }
}

In mainwindow: 在主窗口中:

void MainWindow::initTimer()
{
    refreshTimer = new QTimer(this);
    connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshLogDisplay()));
    refreshTimer->start(1000);
}

void MainWindow::refreshLogDisplay()
{

//MUTEX
    mutex->lock();

    ......//read the map

//MUTEX
    mutex->unlock();

}

In the thread's construction: 在线程的构造中:

Background(DEVMap& map,...,QMutex* mutex):DevMap(map)...,Mutex(mutex){}

In mainwindow which creates the thread: 在创建线程的mainwindow中:

void MainWindow::initThread()
{


    mutex = new QMutex;
    back = new Background(dev_map,..., mutex);
    back->start();

}

And: 和:

void MainWindow::on_Create_triggered()//this function is a slot triggered by a menu item in the MainWindow UI
{

    ......//get information from a dialog 


//MUTEX

    mutex->lock();

    BitState* bitState = new BitState(string((const char *)dlg->getName().toLocal8Bit()),
                                        string((const char *)dlg->getNO().toLocal8Bit()),
                                      dlg->getRevPortNo().toInt(), dlg->getSndPortNo().toInt());


    dev_map.insert(DEVMAP::value_type (string((const char *)dlg->getPIN().toLocal8Bit()), *bitState)); 
     //writes map here



//MUTEX
     mutex->unlock();


}

You can use mutex in any thread. 您可以在任何线程中使用互斥体。 It was designed for this purposes. 它是为此目的而设计的。 But you should not create dead locks, for instance if you do 'nested' calls of the 'lock'. 但是您不应该创建死锁,例如,如果您对“锁”进行了“嵌套”调用。

Good: 好:

mutex->lock();
//code
mutex->unlock();
//code
mutex->lock();
//code
mutex->unlock();

Bad: 坏:

mutex->lock();
//code
mutex->lock(); //dead lock
//code
mutex->unlock();
//code
mutex->unlock();

Be accurate when using locks in functions: 使用功能锁时要准确:

void foo()
{
mutex->lock();
//code
mutex->unlock();
}

mutex->lock();
foo(); //dead lock
mutex->unlock()

Also you need to lock as less code as possible. 另外,您需要锁定尽可能少的代码。 Placing sleep() inside the lock is not not a good idea as far other threads will wait while it's sleeping. 将sleep()放置在锁中并不是一个好主意,因为其他线程在睡眠时将等待。

Not good: 不好:

while(1)
{
  Mutex->lock();
  msleep(40);
  rcv();
  Mutex->unlock();
}

Better: 更好:

while(1)
{
  msleep(40);
  Mutex->lock();
  rcv();
  Mutex->unlock();
}

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

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