c ++ pthread - 如何使地图访问线程安全? [英] c++ pthread - How to make map access threadsafe?

查看:201
本文介绍了c ++ pthread - 如何使地图访问线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个地图作为成员变量和多个线程访问地图(读和写访问)。现在我必须确保只有一个线程可以访问地图。但是我怎么点呢?

$ p

解决方案

实际上,前提是只有一个线程应该访问 map 在给定的时间稍微关闭。



并发读取是好的,你想避免的是有线程修改地图而其他人正在阅读它。



根据您需要的粒度级别,您可能会考虑一个读写器锁,这将允许几个并行读取。 p>

确切的用法已演示这里使用Boost:

  boost :: shared_mutex _access; 
void reader()
{
//获取共享访问
boost :: shared_lock< boost :: shared_mutex> lock(_access);

//现在我们有共享访问
}

void writer()
{
//可升级访问
boost :: upgrade_lock< boost :: shared_mutex> lock(_access);

//获取独占访问权限
boost :: upgrade_to_unique_lock< boost :: shared_mutex> uniqueLock(lock);
//现在我们有独占访问
}

只是方便地包装地图访问的问题。例如,你可以使用通用的代理结构:

  template< typename Item,typename Mutex> 
class ReaderProxy {
public:
ReaderProxy(Item& i,Mutex& m):lock(m),item(i){}

- >(){return& item; }

private:
boost :: shared_lock< Mutex>锁;
Item&项目;
};

template< typename Item,typename Mutex>
class WriterProxy {
public:
WriterProxy(Item& i,Mutex& m):uplock(m),lock(uplock),item(i){}

Item * operator->(){return& item; }

private:
boost :: upgrade_lock< Mutex> uplock;
boost :: upgrade_to_unique_lock< Mutex>锁;
项目&项目;
};

您可以使用它们:

  class Foo {
typedef ReaderProxy< std :: map< int,int>,boost :: shared_mutex>读者;
typedef WriterProxy< std :: map< int,int>,boost :: shared_mutex>作家;

public:
int get(int k)const {
Reader r(map,m);

auto it = r-> find(k);
if(it == r-> end()){return -1; }
return it-> second;
}

void set(int k,int v){
Writer w(map,m);

w-> insert(std :: make_pair(k,v));
}
private:
boost :: shared_mutex m;
std :: map< int,int>地图;
};

注意迭代器,但是它们只能在当前线程持有mutex时被安全操作。



此外,我建议您严格控制地图,将其放入有意义的最小对象,并仅提供您需要的操作。最少的方法访问地图,您不太可能错过一个接入点。


I have a map as member variable and multiple threads that access the map (read and write access). Now I have to ensure that only ONE thread have access to the map. But how do I dot that? What is the best solution for that?

解决方案

Actually, the premise that only a single thread should access to the map at a given time is slightly off.

Concurrent reads are okay, what you want to avoid is having a thread modifying the map while others are reading it.

Depending on the level of granularity you need, you might consider a reader/writer lock, which will let several reads proceed in parallel.

The exact usage was demonstrated here using Boost:

boost::shared_mutex _access;
void reader()
{
  // get shared access
  boost::shared_lock<boost::shared_mutex> lock(_access);

  // now we have shared access
}

void writer()
{
  // get upgradable access
  boost::upgrade_lock<boost::shared_mutex> lock(_access);

  // get exclusive access
  boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
  // now we have exclusive access
}

After that, it is just a matter of conveniently wrapping the map access. For example, you could use a generic proxy structure:

template <typename Item, typename Mutex>
class ReaderProxy {
public:
  ReaderProxy(Item& i, Mutex& m): lock(m), item(i) {}

  Item* operator->() { return &item; }

private:
  boost::shared_lock<Mutex> lock;
  Item& item;
};

template <typename Item, typename Mutex>
class WriterProxy {
public:
  WriterProxy(Item& i, Mutex& m): uplock(m), lock(uplock), item(i) {}

  Item* operator->() { return &item; }

private:
  boost::upgrade_lock<Mutex> uplock;
  boost::upgrade_to_unique_lock<Mutex> lock;
  Item& item;
};

And you can use them as:

class Foo {
  typedef ReaderProxy< std::map<int, int>, boost::shared_mutex> Reader;
  typedef WriterProxy< std::map<int, int>, boost::shared_mutex> Writer;

public:
  int get(int k) const {
    Reader r(map, m);

    auto it = r->find(k);
    if (it == r->end()) { return -1; }
    return it->second;
  }

  void set(int k, int v) {
    Writer w(map, m);

    w->insert(std::make_pair(k, v));
  }
private:
  boost::shared_mutex m;
  std::map<int, int> map;
};

Beware of iterators though, they can only be safely manipulated while the mutex is hold by the current thread.

Also, I recommend that you keep the map under tight control, fitting it into the smallest object that make sense, and provide only those operations you need. The least methods have access to the map, the less likely you are to miss one access point.

这篇关于c ++ pthread - 如何使地图访问线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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