In the past, I often heard that the MFC object should not be used across threads, because MFC is NOT thread-safe. For example, the cwnd object should not be used across threads and can be replaced by a window handle (hwnd. The csocket/casyncsocket object should not be used across threads and should be replaced by a socket handle. So what is thread security? When should I consider it? IfProgramThread security should be considered if multithreading is involved. For example, if an interface is designed and needs to be used in a multi-threaded environment in the future, or an object needs to be used across threads, this must be considered. There is no authoritative definition of thread security. Here I will only talk about my understanding: the provided interfaces for threads are atomic operations or switching between multiple threads will not lead to ambiguity in the execution results of this interface, that is to say, we do not need to consider synchronization issues.
Generally, "thread security" is caused by multi-thread access to shared resources. If we need to take synchronization Measures to Protect the shared resources accessed by an interface, this interface is NOT thread-safe. both MFC and stl are not thread-safe. how can we design a thread-safe class or interface? If all the data accessed in the interface belongs to private data, such an interface is thread-safe. or several interfaces are read-only for shared data, so such interfaces are thread-safe. if data is shared between multiple interfaces, and data is read and written, the caller does not need to consider data synchronization if the designer takes the synchronization measures, this interface is thread-safe, otherwise it is not thread-safe.