You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.7 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "qmythread.h"
#include "qdebug.h"
QMyThread::QMyThread(QObject *parent, quint64 m_sleeptime)
: QThread(parent), _actionFunc(NULL), stopped(false), userCtrol(false), m_stopsleep(false)
{
this->m_sleeptime = m_sleeptime;
//qWarning() << "create mythread";
//this->setPriority(QThread::HighestPriority);
//qWarning() << "create mythread end";
}
QMyThread::~QMyThread()
{
if (isRunning())
{
stop();
quit();
wait();
}
}
void QMyThread::runTsk()
{
if (_actionFunc)
_actionFunc();
else
{
int a = 0;
}
}
void QMyThread::run()
{
/*用户线程调用函数*/
if (userCtrol)//表示按照用户自定义的while循环进行执行
runTsk();
else
{
m_lock.lock();
this->stopped = false;//这里是重新启动的时候需要这样做 因为调用了stop后停止标志无效了 重新start时需要这样才能把线程跑起来
m_lock.unlock();
while (!this->stopped)
{
if (this->stopped==true)
break;
if (_actionFunc)
_actionFunc();
else
{
int a = 0;
}
this->msleep(m_sleeptime);
}
}
}
bool QMyThread::getStopFlag()
{
return this->stopped;
}
//线程停止信号
void QMyThread::stop()
{
m_lock.lock();
this->stopped = true;
m_lock.unlock();
}
bool QMyThread::WaitForSingle(quint8 m_sleep /*= 10*/)
{
// this->msleep(1);
// return true;
bool m_flag = false;
for (int i = 1; i <= m_sleep; i++)
{
if (this->stopped==true)//如果收到停止信号,马上退出等待循环体
break;
if (!this->m_stopsleep)
{
this->msleep(1);
}
else
{
m_flag = true;
break;
}
}
if (m_flag)
{
this->m_stopsleep = false;
return true;
}
else
return false;
}
void QMyThread::WaitSingleIn(bool m_signal)
{
this->m_stopsleep = m_signal;
}