当前位置:网站首页>Thread + thread problem record

Thread + thread problem record

2022-06-25 08:06:00 qq_ forty-two million eight hundred and sixty-three thousand ni

This paper records c++ Threads and issues .

Thread programming implementation method

windows

3 Kind of
----win32 library
----c++ Runtime
----MFC Class library

linux

2 Kind of
----posix Thread library
----c++11 Thread class library

The relationship between threads

   The main thread can create other threads ; Other threads can also create threads ; Threads are equal .

windows

   Other threads are descendants of the main thread ,main sign out , Then all descendant threads exit .

linux

Add

guardian Non Guardian

   guardian / Non daemon threads only represent the difference between the purposes of threads .
See equality for the above threads . hypothesis n Threads ,n=7. Then the program ( That's the process ) The exit time is equal to the longest thread . That is to say 6 After the threads are executed , The process does not exit , Wait for the seventh thread to finish executing before exiting .
   However, such a thread usage scenario cannot be satisfied .
for example 6 Threads execute users , A thread database . So the others are 6 Threads execute on behalf of the user ( process ) Should exit . However, because the thread database cannot determine this, the process cannot exit .
So there are differences between threads . But this belongs to the category of use .
   The flag bit can be set to 1 To distinguish
This flag bit is daemon, Guardian .
   In terms of usage scenarios, when 6 A thread can represent a process , The seventh one needs to be set daemon. So it is also called user ( The front desk ) And non users ( backstage ) Threads .

Thread to exit

There should be only one way for a thread to exit, that is, the thread function return 0, meanwhile new Memory needs delete.
Just follow main equally . After all, they are all threaded functions .

Thread communication

event

lock

Semaphore

Exclusive access

Threads Concurrent

   Multiple threads in the same process usually do different jobs , Can achieve parallel Handle , Avoid a long time occupation of a task CPU Time .
   For example, a thread is responsible for sending and receiving data through the network , Another thread does the required computation , The third thread does file input and output , When one of them For some reason Blocking after ( For example, the thread that sends and receives data through the network waits for the other party to send data ), Other threads can still execute without being blocked .
   When the number of threads is more than the processor of the computer (CPU) In number , To run all these threads , The operating system is Every Independent Thread scheduling some CPU Time , Operating system to Rotation mode provides time slices to threads , This gives a false impression , It seems that these threads are running at the same time .

The problem record

main Function and thread function return to who

main Exit in the end does not affect

原网站

版权声明
本文为[qq_ forty-two million eight hundred and sixty-three thousand ni]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250619195820.html