thread.h
Qmutex m_mutex;
void threadPause();
void threadResume();
thread.cpp
bool Flag_StopThread = 0;
bool Flag_PauseThread = 0;
void run()
{
while(1)
{
m_mutex.lock();
m_mutex.unlock();
if (Flag_StopThread)
{
Flag_StopThread = 0;
break;
}
}
}
void Mythread::threadPause()
{
Flag_PauseThread = 1;//线程暂停标志
this->m_mutex.lock();
}
void Mythread::threadResume()
{
Flag_PauseThread = 0;
this->m_mutex.unlock();
}
widget.h
Mythread* m_thread = NULL;
void on_start_clicked();
void on_pause_clicked();
void on_stop_clicked();
widget.cpp
void widget::on_start_clicked()// 开始/恢复
{
if (!m_thread->isRunning())
{
m_thread->start();
}
else
{
m_thread->threadResume();
}
}
void cad::on_pause_clicked() //暂停
{
if (m_thread->isRunning())
{
m_thread->threadPause();
}
}
void cad::on_clear_clicked() //停止
{
if (Flag_PauseThread)
{
Flag_StopThread = 1;//停止线程标志
m_thread->threadResume();
}
}