相关疑难解决方法(0)

C++ 11 STL容器和线程安全

我无法找到有关此信息的最新信息.

C++ 11版本的STL容器是否保证了一定程度的线程安全性?

由于性能原因,我确实希望他们不这样做.但话说回来,这就是为什么我们同时拥有std::vector::operator[]std::vector::at.

c++ multithreading stl c++11

40
推荐指数
2
解决办法
3万
查看次数

stl vector并发读取线程安全吗?

我正在开发一个应用程序,其中大量的线程需要迭代一组字符串值,并尝试将其自己的数据与列表中的可用数据进行匹配.

我正在寻找以下用例:

  1. Vector初始化为几个std :: string类型的元素.(可以说对象名是strList).strList将在应用程序启动时初始化.
  2. 所有线程都将遍历strList以查看它的值是否与strList的至少一个元素匹配.
  3. 没有线程会尝试修改strList,它将严格用作只读对象.

那么请告诉我并发读取是否对矢量对象是线程安全的.我使用的是RHEL 6,gcc版本是4.5.x

c++ concurrency stl thread-safety language-lawyer

23
推荐指数
2
解决办法
2万
查看次数

在unordered_map(C++)中并发写入不同的桶?

C++新手在这里.我正在尝试在unordered_map中同时写入不同的存储桶.从我通过搜索可以看出,我的理解是这应该是一个线程安全的操作.我(也许是不正确的)理解是基于这里这里的答案,以及C++ 11标准的引用部分(特别是第2项 - 强调我的):

23.2.2集装箱数据竞赛[container.requirements.dataraces]

1为避免数据争用(17.6.5.9),实现应考虑以下函数为const:begin,end,rbegin,rend,front,back,data,find,lower_bound,upper_bound,equal_range,at和,除了关联或无序关联容器,operator [].

2尽管如此(17.6.5.9),除了vector<bool>同时修改同一序列中不同元素中包含对象的内容时,还需要实现以避免数据争用.

3 [注意:对于大小大于1的向量x,x [1] = 5和*x.begin()= 10可以在没有数据争用的情况下同时执行,但是x [0] = 5和*x.并发执行的begin()= 10可能导致数据竞争.作为一般规则的例外,对于向量<bool> y,y [0] = true可以与y [1] = true竞争. - 尾注]

在任何情况下,使用标准容器写入不同的桶似乎不是线程安全的,如下面的代码所示.您会看到我在写入之前启用了与正在修改的存储区相对应的锁定,但有时无法正确记录对.对于它的价值,如果我使用单个锁 - 例如,只需更改auto bkt = mm->bucket(key);auto bkt=0;,有效地锁定整个unordered_map容器 - 一切都按预期工作.

#include <iostream>
#include <unordered_map>
#include <atomic>
#include <vector>
#include <thread>

#define NUM_LOCKS 409
#define N 100
#define NUM_THREADS 2

using namespace std;


class SpinLock
{
    public:
        void lock()
        {
            while(lck.test_and_set(memory_order_acquire)){} …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency multithreading c++11

20
推荐指数
1
解决办法
1137
查看次数

std :: map的线程安全性,用于只读操作

我有一个std :: map,用于将值(字段ID)映射到人类可读的字符串.当我的程序在任何其他线程启动之前启动时,此映射会初始化一次,之后永远不会再次修改.现在,我给每个线程自己的这个(相当大的)地图的副本,但这显然是低效的内存使用,它减慢了程序启动速度.所以我想给每个线程一个指向地图的指针,但这会引发一个线程安全问题.

如果我正在做的就是使用以下代码从地图中读取:

std::string name;
//here N is the field id for which I want the human readable name
unsigned field_id = N; 
std::map<unsigned,std::string>::const_iterator map_it;

// fields_p is a const std::map<unsigned, std::string>* to the map concerned.
// multiple threads will share this.
map_it = fields_p->find(field_id);
if (map_it != fields_p->end())
{
    name = map_it->second;
}
else
{
    name = "";
}
Run Code Online (Sandbox Code Playgroud)

这是否有效或者从多个线程读取std :: map有问题吗?

注意:我目前正在使用visual studio 2008,但我希望这可以在大多数主要的STL实现中使用acros.

更新:已编辑的代码示例,用于const正确性.

c++ multithreading stl stdmap thread-safety

18
推荐指数
2
解决办法
2万
查看次数

将结果从线程转储到向量中是否安全?

我正在学习C++ 11的功能,并按照以下几行编写了一些代码

#include <vector>
#include <thread>
using std::thread;
using std::vector;

double Computation(int arg)
{
    // some long-running computation
    return 42.0;
}

double ConcurrentComputations()
{
    const int N = 8; // number of threads
    vector<thread> thr;
    vector<double> res(N);
    const int arg = 123456; // something or other
    // Kick off threads which dump their results into res
    for(int i=0; i<N; ++i)
        thr.push_back(thread ([&res, i, arg]()
                {  res[i] =  Computation(arg); } ));
    // Wait for them to finish and get results
    double …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

0
推荐指数
1
解决办法
130
查看次数