在自动化设备中,设备在运转过程中,为了防止设备伤人,通常会在设备门入口安装光幕,当光幕被遮挡时,设备必须暂停,确保安全的情况下,按下继续按钮,设备继续运转。对于多工位的设备,每个工位可能交由一个线程处理,因此暂停时,需要令这些线程暂时挂起。
C++11标准以后,加入了线程相关的接口,在应用中经常需要使线程暂停,在windows API中可以使用suspend 使线程挂起,但容易产生一些意想不到的问题,官方并不推荐使用。但 C++11 中没有使线程暂停的接口。现用条件变量与互斥锁封装一个线程类,实现线程的暂停、继续。
//头文件 Thread.h
#pragma once
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <functional>
class Thread
{
public:
Thread(std::function<void(int)>func, int workID);
virtual ~Thread();
enum State
{
Stoped, ///<停止状态,包括从未启动过和启动后被停止
Running, ///<运行状态
Paused ///<暂停状态
};
State state() const;
void start();
void stop();
void pause();
void resume();
public:
//检查线程是否退出
bool check_stop();
//如果暂停,需要阻塞
bool wait_pause();
private:
void run();
std::function<void(int)> process;
private:
int _workID;
std::thread* _thread;
std::mutex _mutex;
std::condition_variable _condition;
volatile std::atomic_bool _pauseFlag; ///<暂停标识
volatile std::atomic_bool _stopFlag; ///<停止标识
State _state;
};
//源文件 Thread.cpp
#include "Thread.h"
Thread::Thread(std::function<void(int)> func, int workID)
: _thread(nullptr),
_pauseFlag(false),
_stopFlag(false),
_state(Stoped),
_workID(workID)
{
process = func;
}
Thread::~Thread()
{
stop();
}
Thread::State Thread::state() const
{
return _state;
}
void Thread::start()
{
if (_thread == nullptr)
{
_thread = new std::thread(&Thread::run, this);
_pauseFlag = false;
_stopFlag = false;
_state = Running;
}
}
void Thread::stop()
{
if (_thread != nullptr)
{
_pauseFlag = false;
_stopFlag = true;
_condition.notify_all(); // Notify one waiting thread, if there is one.
if (_thread->joinable())
_thread->join(); // wait for thread finished
delete _thread;
_thread = nullptr;
_state = Stoped;
}
}
void Thread::pause()
{
if (_thread != nullptr)
{
_pauseFlag = true;
_state = Paused;
}
}
void Thread::resume()
{
if (_thread != nullptr)
{
_pauseFlag = false;
_condition.notify_all();
_state = Running;
}
}
void Thread::run()
{
process(_workID);
_pauseFlag = false;
_stopFlag = false;
}
//检查线程是否退出
bool Thread::check_stop()
{
return _stopFlag;
}
//如果暂停,需要阻塞
bool Thread::wait_pause()
{
if (_pauseFlag)
{
std::unique_lock<std::mutex> locker(_mutex);
while (_pauseFlag)
{
_condition.wait(locker); // Unlock _mutex and wait to be notified
}
locker.unlock();
}
return !_stopFlag;
}
创建一个MFC的对话框工程,添加按钮如下:
在 OnInitDialog() 函数中添加如下代码创建一个线程
int workID = 1;
pThread = std::make_shared<Thread>(std::bind(&CthreadTest2Dlg::process, this, std::placeholders::_1), workID);
在对话框类中添加线程响应函数
void CthreadTest2Dlg::process(int workID)
{
int work = workID;
int g_count = 0;
while (!pThread->check_stop())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
SetDlgItemInt(IDC_COUNT, g_count++);
pThread->wait_pause();
}
g_count = 0;
SetDlgItemInt(IDC_COUNT, g_count);
}
开始、继续、暂停和停止按钮响应函数
void CthreadTest2Dlg::OnBnClickedStart()
{
pThread->start();
}
void CthreadTest2Dlg::OnBnClickedPause()
{
pThread->pause();
}
void CthreadTest2Dlg::OnBnClickedStop()
{
pThread->stop();
}
void CthreadTest2Dlg::OnBnClickedContinue()
{
pThread->resume();
}
运行程序,单击开始后,编辑框中正常计数,但在线程退出时,阻塞在 join() 函数。经过调试发现MFC的函数 SetDlgItemInt 会影响join()的等待信号。将编辑框的数据直接写入改成post消息后正常。
void CthreadTest2Dlg::process(int workID)
{
int work = workID;
int g_count = 0;
while (!pThread->check_stop())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
PostMessage(WM_TEST_MSG, g_count++, NULL);
pThread->wait_pause();
}
g_count = 0;
}
————————————————
写的很清晰,转载来学习下
原文链接:https://blog.youkuaiyun.com/u010354182/article/details/111384244