欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++11 thread 初始化 博客分类: c++  

程序员文章站 2024-03-19 10:16:58
...
class background_task{
public:
    int id;
    int value;
    background_task(int _id, int _value):id(_id),value(_value){
        output();
        std::cout << "initialize.\n";
    }
    void do_something(){
        std::cout << "id = " << id << std::endl;
        id = 2;
        std::cout << "do something.\n";
        std::cout << "id = " << id << std::endl;
    }

    void do_something_else(){
        std::cout << "value = " << value << std::endl;
        value = 20;
        std::cout << "do something else.\n";
        std::cout << "value = " << value << std::endl;
    }

    void output(){
        std::cout << "ID = " << id << ", value = " << value << std::endl;
    }

   // void operator() (background_task &){
    void operator() (){

        do_something();
        do_something_else();
    }

};





int main()
{

    background_task tf(1,10);
    tf.output();
    std::thread t(tf);
    
    //tf.do_something();
    //tf.do_something_else();

    tf.output();
    t.join();
    tf.output();
}

 

  • c++11 thread 初始化
            
    
    博客分类: c++  
  • 大小: 28.6 KB