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