c++ 线程类 thread
#include <thread>;
#include <chrono>
using namespace std;
void fun(int a){
printf("thread %d " ,a);
this_thread::sleep_for(chrono::second(1));
}
int main(){
thread t(fun,1);
t.join();
}
注:join 是线程等待,等待子线程结束后在继续执行;
线程等待要用sleep_for
线程抽象类
#include <thread>;
using namespace std;
class Thread{
public:
std::thread::id get_id();
void start();
void join();
virtual void query();
bool is_start;
private:
thread th;
};
void Thread::start(){
if(is_start == false) {
thread t(std::bind(&Thread::query,this));
th = std::move(t);
is_start = true;
}
}
void Thread::join(){
th.join();
}
thread::id Thread::join(){
return th.get_id();
}
网友评论