Qt <多线程> 互斥锁

it2025-12-23  4

多线程与互斥锁

正在更新中。。。

多个线程一起开启,处理共享的数据,会造成数据不同步、不准确性互斥锁,锁死当前执行线程,并保证执行完后解锁,再执行其他线程 int gPos = 0;//全局变量共享数据 QMutex gMutex;//互斥锁 void Widget::on_startBtn_clicked() { gPies = 0; m_myThread1.start(); m_myThread2.start(); m_myThread3.start(); } void Widget::on_stopBtn_clicked() { m_myThread1.stop(); m_myThread2.stop(); m_myThread3.stop(); } void MyThread::run() { int i = 0; while(!m_isPressed && i++ < 20000) { gMutex.lock(); qDebug() << QString("天上掉下%1个馅饼").arg(++gPos); gMutex.unlock(); } }

正在更新中。。。

最新回复(0)