What is thread security?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.