std :: mutex和std :: lock是否可以保证处理器间代码中的内存同步? [英] Does `std::mutex` and `std::lock` guarantee memory synchronisation in inter-processor code?

查看:103
本文介绍了std :: mutex和std :: lock是否可以保证处理器间代码中的内存同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用openMP编写在多核节点上运行的代码. openMP具有特定的内存模型,该模型可以确保在获取锁时,内存在运行在不同内核上的线程之间是同步的.

I'm currently using openMP to write code running on multi-core nodes. openMP has a specific memory model which guarantees that memory is synchronised between threads running on different cores when a lock is acquired.

我考虑使用C ++ 11构造(带有std::mutexstd::lockstd::thread)而不是openMP(因为它们具有更大的灵活性),并且想知道是否/如何保证处理器之间的内存同步?如果没有,我该如何执行呢?

I consider using C++11 constructs (std::thread with std::mutex and std::lock) instead of openMP (because of their larger flexibility) and wonder if/how memory synchronisation between processors is guaranteed here? And if not, how can I enforce it?

推荐答案

该标准对§30.4.1.2[thread.mutex.requirements.mutex]/6-25 的同步提供了以下保证. >

The standard makes the following guarantees about synchronization of std::mutex, in §30.4.1.2[thread.mutex.requirements.mutex]/6-25

m.lock()表达式应格式正确,并具有以下语义

The expression m.lock() shall be well-formed and have the following semantics

同步:同一对象上的先前unlock()操作应与此操作同步.

Synchronization: Prior unlock() operations on the same object shall synchronize with this operation.

同样,

表达式m.unlock()必须格式正确,并具有以下语义

The expression m.unlock() shall be well-formed and have the following semantics

同步:此操作与随后获得对同一对象所有权的锁定操作进行同步.

Synchronization: This operation synchronizes with subsequent lock operations that obtain ownership on the same object.

(其中与...同步"是$ 1.10中解释的特定术语,尽管通过阅读 C ++并发操作更容易理解)

(Where "synchronizes with" is a specific term explained in $1.10, although it's much easier to understand by reading C++ Concurrency In Action)

这篇关于std :: mutex和std :: lock是否可以保证处理器间代码中的内存同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆