std::thread knowledge points

1. A thread represents a system resource or even dedicated hardware, so a thread can be moved but not copied.

2. std::thread:: hardware_concurrency () Get how many tasks the hardware supports to execute at the same time. (Not necessarily equal to the number of CPU cores, depending on the machine structure. For example, a processor with 6 cores and 12 threads is different from a processor with 6 cores and 8 threads)

3. The current thread id: std::this_thread:: get_id ().

4. If the id of a thread is std::thread::id{}, it may:

  • Not assigned a task
  • over
  • Has been moved
  • Has been detached()

5、输出id:std::hash<std::thread::id>()(id)。

6. The std::thread will be started immediately after the completion of the construction.

7. Moving a std::thread to another std::thread does not affect its execution, it just changes what the thread points to.

8、std::this_thread::sleep_for(std::chrono::seconds(1))暂停1秒。

9. join () does nothing and waits for the thread execution to complete.

10. Detach () separates the association between the child thread and the main thread, that is, the child thread continues to run independently in the background, the main thread can no longer obtain the control of the child thread , even if the main thread ends, the child thread will not end if it is not executed. Try not to use it .

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/114660373