在C ++中实现无锁地图是可能的 [英] Is it possbile to implement lock free map in C++

查看:181
本文介绍了在C ++中实现无锁地图是可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发基于C / S的网络应用程序,我们发现有很多锁添加到std :: map中,服务器的性能变差。



我想知道是否可以实现无锁地图,如果是的话,怎么办?是否有任何开源代码?



编辑:
实际上我们使用std :: map来存储套接字信息,我们根据套接字文件做了封装描述包括一些其他必要的信息,如ip地址,端口,套接字类型,tcp或udp等。



总而言之,我们有一个全球地图说它是



  map< int fileDescriptor,socketInfor *> SocketsMap,

然后用于发送数据的每个线程都需要访问SocketsMap,并且必须添加互斥体在从SocketsMap读取或写入SocketsMap之前,因此整个应用程序的并发级别将会因为SocketsMap的许多锁定而大大降低。



为了避免并发级别问题,我们有两个解决方案:1.分别存储每个socketInfor * 2.使用某种无锁地图。



我想找到一些无锁的地图,因为这个解决方案所需的代码变化远远小于解决方案1.

解决方案

其实有一种方法,虽然我还没有实现它,但是有一篇关于使用隐藏的C ++专家Andrei Alexandrescu从危险指针中锁定免费地图。


We are developing a network application based C/S, we find there are too many locks adding to std::map that the performance of server became poor.

I wonder if it is possible to implement a lock-free map, if yes, how? Is there any open source code there?

EDIT: Actually we use the std::map to store sockets information, we did encapsulation based on the socket file description to include some other necessary information such as ip address, port, socket type, tcp or udp, etc.

To summary, we have a global map say it's

map<int fileDescriptor, socketInfor*> SocketsMap, 

then every thread which is used to send data needs to access SocketsMap, and they have to add mutex before reading from SocketsMap or writing to SocketsMap, thus the concurrency level of the whole application would be greatly decreased because of so many locks addding to SocketsMap.

To avoid the concurrency level problem, we have two solutions: 1. store each socketInfor* separately 2. use some kind of lock-free map.

I would like to find some kind of lock-free map, because codes changes required by this solution are much less than that of solution 1.

解决方案

Actually there's a way, although I haven't implemented it myself there's a paper on a lock free map using hazard pointers from eminent C++ expert Andrei Alexandrescu.

这篇关于在C ++中实现无锁地图是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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