美文网首页
C++ 线程类thread

C++ 线程类thread

作者: Vwwwwww | 来源:发表于2020-05-27 20:58 被阅读0次

    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();
    }
    

    相关文章

      网友评论

          本文标题:C++ 线程类thread

          本文链接:https://www.haomeiwen.com/subject/snvgahtx.html