Pyqt5中关于QMutex的问题

这是子线程的启动部分,第一次启动的时候2个qmutex的lock是成功的,顺利进行了下面的启动

        if channel_num in range(1, 7):
            self.dac_dict['channel_num'] = channel_num
            self.dac_dict['h_or_l'] = 1
            self.dac_dict['lcc_flag'] = 0
            self.dac_dict['fluke_flag'] = 0
            if self.dac_dict['lock'] == 0:
                lcc_lock1.lock()
                fluke_lock1.lock()
            self.my_lcc_send_work.is_run = True
            self.my_lcc_send_thread.start()
            self.my_lcc_receive_work.is_run = True
            self.my_lcc_receive_thread.start()
            self.my_fluke_send_work.is_run = True
            self.my_fluke_send_thread.start()
            self.my_fluke_receive_work.is_run = True
            self.my_fluke_receive_thread.start()
            self.dac_dict['lock'] += 1

但是在线程退出之后,第二次启动的时候,线程就很奇怪的启动不了,在debug模式下发现是qmutex的lock失败了。但是在线程第一次退出的代码中,尝试去对qmutex lock又是成功的。

        if self.dac_dict['lcc_flag'] == 0xff and self.dac_dict['fluke_flag'] == 0xff:
            self.my_lcc_send_work.is_run = False
            self.my_lcc_receive_work.is_run = False
            # lcc_lock1.lock()
            # lcc_lock2.unlock()
            self.my_lcc_send_thread.quit()
            self.my_lcc_receive_thread.quit()
            self.my_lcc_send_thread.wait()
            self.my_lcc_receive_thread.wait()
            self.my_fluke_send_work.is_run = False
            self.my_fluke_receive_work.is_run = False
            # fluke_lock1.lock()
            # fluke_lock2.unlock()
            self.my_fluke_send_thread.quit()
            self.my_fluke_receive_thread.quit()
            self.my_fluke_send_thread.wait()
            self.my_fluke_receive_thread.wait()

这个qmutex的问题没有搞明白,准备试试qwaitcondition。
在两个线程的work函数中print了一番,因为我的程序中,线程最后是在lcc_receive这个信号关联的槽中退出的,因此lcc2_lock已经unlock,那么lcc1_lock就一定是lock的。那么再次启动线程,自然就出错了,原因就在这里。

猜你喜欢

转载自blog.csdn.net/cp_srd/article/details/109516831