C++学习笔记(三十六):c++ 线程

  • 本节主要学习c++线程。
  • 线程的简单代码示例
  • #include 
    #include 
    static bool s_Finished = false;
    
    void Work()
    {
    	using namespace std::literals::chrono_literals;
    	std::cout << std::this_thread::get_id() << std::endl;
    	while (!s_Finished)
    	{
    		std::cout << "Working..." << std::endl;
    		std::this_thread::sleep_for(1s);
    	}
    	
    }
    
    int main()
    {
    	//在用户按下回车前一直打印Working...,按下回车后在执行worker.join();语句之后的程序
    	std::thread worker(Work);
    	std::cin.get();
    	s_Finished = true;
    	worker.join();
    	std::cout << "Finish..." << std::endl;
    	std::cout << std::this_thread::get_id() << std::endl;
    
    	std::cin.get();
    }
  • 关于线程的更多知识,请参考并发支持库 - cppreference.com

你可能感兴趣的:(c++学习笔记,c++,学习,笔记)