c++ 类内使用多线程 std::thread
头文件
#include <thread>
class ClassName
{
private:
std::thread blink_thread;
volatile bool flag;
public:
};
cpp
int ClassName::start_thr()
{
// 确保 thread 结束,不要重新进入thread
this->flag = false;
while (this->blink_thread.joinable())
this->blink_thread.join();
this->flag = true;
blink_thread = std::thread(&ClassName::task, this);
return 0;
}
void ClassName::task()
{
while (this->flag == true)
{
sleep(1);
}
}
编译
- Linux下需要 依赖 pthread
- g++ -lpthread