线程安全数据和线程安全容器

时间:2020-12-26 18:09:24

Hi Guys I want to know what is the difference between thread safe Data and Thread Safe Containers

嗨大家好我想知道线程安全数据和线程安全容器之间的区别

1 个解决方案

#1


Thread safe data:

线程安全数据:

  • Generally refers to data which is protected using mutexes, semaphores or other similar constructs.

    通常是指使用互斥锁,信号量或其他类似结构保护的数据。

  • Data is considered thread safe if measures have been put in place to ensure that:

    如果采取措施确保以下方面的数据被认为是线程安全的:

    • It can be modified from multiple threads in a controlled manner, to ensure the resultant data structure doesn't becoming corrupt, or lead to race conditions in the code.
    • 它可以以受控方式从多个线程进行修改,以确保结果数据结构不会损坏,或导致代码中的竞争条件。

    • It can be read in a reliable fashion without the data become corrupt during the read process. This is especially important with STL-style containers which use iterators.
    • 它可以以可靠的方式读取,而不会在读取过程中数据损坏。对于使用迭代器的STL样式容器,这一点尤为重要。

  • Mutexes generally work by blocking access to other threads while one thread is modifying shared data. This is also known as a critical section, and RAII is a common design pattern used in conjunction with critical sections.

    当一个线程正在修改共享数据时,互斥锁通常通过阻止对其他线程的访问来工作。这也称为关键部分,RAII是与关键部分结合使用的常见设计模式。

  • Depending on the CPU type, some primitive data types (e.g. int) and operations (increment) might not need mutex protection (e.g. if they resolve down to an atomic instruction in machine language). However:

    根据CPU类型,一些原始数据类型(例如int)和操作(增量)可能不需要互斥保护(例如,如果它们解析为机器语言的原子指令)。然而:

    • It is bad practice to make any assumptions about CPU architecture.
    • 对CPU架构做出任何假设是不好的做法。

    • You should always code defensively to ensure code will remain thread-safe regardless of the target platform.
    • 您应始终采取防御性编码,以确保无论目标平台如何,代码都将保持线程安全。

Thread safe containers:

线程安全容器:

  • Are containers which have measures in place to ensure that any changes made to them occur in a thread-safe manner.

    是否有适当措施的容器,以确保对它们所做的任何更改都以线程安全的方式进行。

  • For example, a thread safe container may allow items to be inserted or removed using a specific set of public methods which ensure that any code which uses it is thread-safe.

    例如,线程安全容器可以允许使用一组特定的公共方法插入或删除项目,这些公共方法确保使用它的任何代码都是线程安全的。

  • In other words, the container class provides the mutex protection as a service to the caller, and the user doesn't have to roll their own.

    换句话说,容器类将互斥锁保护作为服务提供给调用者,并且用户不必自己滚动。

#1


Thread safe data:

线程安全数据:

  • Generally refers to data which is protected using mutexes, semaphores or other similar constructs.

    通常是指使用互斥锁,信号量或其他类似结构保护的数据。

  • Data is considered thread safe if measures have been put in place to ensure that:

    如果采取措施确保以下方面的数据被认为是线程安全的:

    • It can be modified from multiple threads in a controlled manner, to ensure the resultant data structure doesn't becoming corrupt, or lead to race conditions in the code.
    • 它可以以受控方式从多个线程进行修改,以确保结果数据结构不会损坏,或导致代码中的竞争条件。

    • It can be read in a reliable fashion without the data become corrupt during the read process. This is especially important with STL-style containers which use iterators.
    • 它可以以可靠的方式读取,而不会在读取过程中数据损坏。对于使用迭代器的STL样式容器,这一点尤为重要。

  • Mutexes generally work by blocking access to other threads while one thread is modifying shared data. This is also known as a critical section, and RAII is a common design pattern used in conjunction with critical sections.

    当一个线程正在修改共享数据时,互斥锁通常通过阻止对其他线程的访问来工作。这也称为关键部分,RAII是与关键部分结合使用的常见设计模式。

  • Depending on the CPU type, some primitive data types (e.g. int) and operations (increment) might not need mutex protection (e.g. if they resolve down to an atomic instruction in machine language). However:

    根据CPU类型,一些原始数据类型(例如int)和操作(增量)可能不需要互斥保护(例如,如果它们解析为机器语言的原子指令)。然而:

    • It is bad practice to make any assumptions about CPU architecture.
    • 对CPU架构做出任何假设是不好的做法。

    • You should always code defensively to ensure code will remain thread-safe regardless of the target platform.
    • 您应始终采取防御性编码,以确保无论目标平台如何,代码都将保持线程安全。

Thread safe containers:

线程安全容器:

  • Are containers which have measures in place to ensure that any changes made to them occur in a thread-safe manner.

    是否有适当措施的容器,以确保对它们所做的任何更改都以线程安全的方式进行。

  • For example, a thread safe container may allow items to be inserted or removed using a specific set of public methods which ensure that any code which uses it is thread-safe.

    例如,线程安全容器可以允许使用一组特定的公共方法插入或删除项目,这些公共方法确保使用它的任何代码都是线程安全的。

  • In other words, the container class provides the mutex protection as a service to the caller, and the user doesn't have to roll their own.

    换句话说,容器类将互斥锁保护作为服务提供给调用者,并且用户不必自己滚动。