std::thread在类成员函数中用法
- 简单方式例子
#include <iostream>
#include <thread>
#include <unistd.h>
#include "string"
using namespace std;
void work(string a)
{
while(1)
{
sleep(1);
std::cout << "it is work1 " << a <<std::endl;
}
}
int main(void)
{
std::thread thread(work,"test");
while(1)
{
std::cout << "it is main" <<std::endl;
sleep(1);
}
}
- thread 在类成员函数中例子
#include <iostream>
#include <thread>
#include <unistd.h>
#include "string"
using namespace std;
class task {
public:
void work1(string a)
{
while(1)
{
sleep(1);
std::cout << "it is work1 " << a <<std::endl;
}
};
std::thread create_thread(void)
{
return thread(&task::work1,this,"test");
};
private:
};
int main(void)
{
task tk;
std::thread thread = tk.create_thread();
while(1)
{
std::cout << "it is main" <<std::endl;
sleep(1);
}
}