我们可以在socket Map上使用读写锁吗?

Can we use read-write lock on Sockets Map?

本文关键字:读写 写锁 socket Map 我们      更新时间:2023-10-16

假设我们有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保护的临界区的利用率。