我们可以在socket Map上使用读写锁吗?
Can we use read-write lock on Sockets Map?
假设我们有std::map Sockets map,它是一个多线程应用程序。将有多个线程访问映射中的套接字以发送套接字数据,同时只有一个线程访问映射中的套接字以接收数据,如果远程端关闭,该线程也将删除SocketInfo*。
在上面的情况下,我们可以使用读写锁(pthread_rwlock_t)来同步线程吗?如果是,我们比pthread_mutex_t有更多的好处吗?
史蒂夫[伪代码]
class CSocketIO {
std::map<int, SocketInfo*> m_Sockets; //socket value and socket info
pthread_t m_ReadingSocketThreads; // for reading socket data
};
class CSession {
void SendOutNetworkPackets(); //access sockets for sending sock data
pthread_t m_WorkerThread; // do jobs and send sock data
};
class CSomeClass {
void SendOutNetworkPackets(); // also access sockets
pthread_t m_WorkerThread; // do jobs and send sock data
};
是的,你可以使用读/写锁来做到这一点,这实际上是明智的。
这样做的好处是,您可以使用一个读/写锁同时拥有多个读取器,而如果您使用一个简单的互斥锁,如果一个线程正在读取,其他想要读取的线程就会阻塞,从而有效地序列化读取操作。对于读/写锁,只有当写操作发生时,读操作才会阻塞。
你不允许两个线程同时写同一个套接字,对吗?你只是想保护SocketInfo*
的查找不受map
的影响吗?
读写锁很少是有益的,往往是有害的。当我的客户机在多线程程序中有一个不确定的错误时,我查找的第一个罪魁祸首是使用不当的读写锁。(你只能使用读锁来保护真正是const
的函数调用)
假设你的每个线程都在做这样的事情:
// do some work to figure out what to send
SocketInfo* my_socket_info = 0;
// critical section begins here
pthread_mutex_lock(the_map_mutex);
socket_map_t::const_iterator i = m_Sockets.find(id);
if (i != m_Sockets.end()) {
my_socket_info = i->second;
}
pthread_mutex_unlock(the_map_mutex);
// critical section is done
if (my_socket_info != 0) {
// do a bunch of work to actually send the data
// (including acquiring a mutex specific to the socket unless
// you have some other way of guaranteeing that the socket isn't
// currently being written by some other thread)
您需要问的第二个问题(在"我保护的是const
吗?"之后)是:the_map_mutex
被锁定的时间百分比是多少?如果它小于50%,那么你就没有足够的争用使读写锁获胜。您可以通过测量单个线程在临界区内花费的时间百分比,然后乘以系统上的硬件线程数,来快速估计受the_map_mutex
保护的临界区的利用率。
相关文章:
- 如何调试读写器锁的死锁?
- Linux读,写Arduino串行
- 多线程一读一写time_t
- 将Linux打开,读,写,关闭功能以在Windows上工作
- 如何使用单个解锁方法(可称为读取器或写入器)实现C++读写器锁?
- 简单的读写锁
- 快速且无锁的单写器、多读器
- CreateProcess cmd.exe读/写管道死锁
- 优化读/写锁的实现
- 单读多写器,带pthreads和锁,不带boost
- 无锁单写多读列表的实现细节
- 如何从更基本的同步原语中创建多读/单写锁
- 为银行项目计算读入/写出的问题
- Linux下c++中的串口.可以读和写串行端口同时发生
- 写文件/读文件死锁
- 在Windows+VisualC上是volatile读和写的原子
- GCC可以使用读-修改-写指令来更新易失性变量吗?
- 具有条件变量的读写器锁
- 读(和写)Office文档自定义属性而不需要自动化
- vector多线程同步,一个读一个写:只在调整大小时锁定