是否可以在C ++中实现无锁映射 [英] Is it possible to implement lock free map in C++

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

问题描述

我们正在开发基于C/S的网络应用程序,我们发现std :: map中添加了太多锁,导致服务器性能变差.

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?

实际上,我们使用std :: map来存储套接字信息,我们基于套接字文件描述进行了封装,以包括其他一些必要的信息,例如ip地址,端口,套接字类型,tcp或udp等.

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, 

然后,每个用于发送数据的线程都需要访问SocketsMap,并且它们必须在从SocketsMap读取或写入SocketsMap之前添加互斥锁,因此由于添加了如此多的锁,整个应用程序的并发级别将大大降低.到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.

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

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

我想找到某种无锁映射,因为此解决方案所需的代码更改远少于解决方案1所需的代码更改.

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天全站免登陆