本文介绍了是否足够声明一个函数为transaction_safe,所以他们可以使用线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要在我的一个类中声明所有的函数为 transaction_safe ,就可以在事务中使用线程安全 atomic_noexcept ,atomic_cancel,atomic_commit 来自实验性事务内存TS?

Is enough just to declare all of the functions as transaction_safe in some my class, so its can be used as thread-safe in transactions atomic_noexcept, atomic_cancel, atomic_commit from Experimental Transactional Memory TS?

已知的事务内存TS(ISO / IEC TS 19841:2015)实验C ++标准库。简单示例如下:

As known there are Transactional Memory TS (ISO/IEC TS 19841:2015) in the Experimental C++ standard libraries. Simple examples are here: http://en.cppreference.com/w/cpp/language/transactional_memory

另外还有针对
的C ++扩展的技术规范事务内存:

第34页:

23.4 Associative containers [associative]
23.4.4 Class template map [map]
23.4.4.1 Class template map overview [map.overview]

In 23.4.4.1 [map.overview], add "transaction_safe" to the declarations of all
variants of the begin and end member functions and to
the declarations of size, max_size, and empty.

如果事务内存将提交到C ++标准,那么我们可以这样做,并且线程安全?

I.e. if Transactional Memory will commit to the C++ Standard, then can we simply do something like this and will it thread-safe?

#include<map>
#include<thread>

std::map<int, int> m;

int main() {

 std::thread t1([&m]() {
  atomic_cancel
  {
   m[1] = 1;    // thread-safe
  }
 } );

 t1.join();

 return 0;
}


$ b -fgnu-tm 键:

只要将所有函数声明为 transaction_safe 在一些我的类,所以它可以用作线程安全 - 如果我将调用它的作用域: atomic_cancel {obj.func(); }

And will enough just to declare all of the functions as transaction_safe in some my class, so its can be used as thread-safe - if I will call its in scope: atomic_cancel { obj.func(); }?

推荐答案

std :: map 不会是 transaction_safe 方法, t在atomic_cancel中调用它。这将是一个编译时错误。

std::map<int, int>::operator[] wouldn't be a transaction_safe method, so you couldn't call it in atomic_cancel. It would be a compile time error.

这篇关于是否足够声明一个函数为transaction_safe,所以他们可以使用线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:18