6

我有一个多线程应用程序,它创建了 48 个线程,这些线程都需要访问一个公共属性(stl::map)。只有在线程启动时才会写入映射,其余时间将从中读取映射。这似乎是 pthread_rw_lock 的完美用例,而且一切似乎都运行良好。

我遇到了一个完全不相关的段错误并开始分析核心。使用 gdb,我执行了命令info threads并对结果感到非常惊讶。我观察到几个线程实际上正在按预期从映射中读取,但奇怪的是几个线程在等待 rw_lock 的 pthread_rwlock_rdlock() 中被阻塞。

这是等待锁的线程的堆栈跟踪:

#0  0xffffe430 in __kernel_vsyscall ()
#1  0xf76fe159 in __lll_lock_wait () from /lib/libpthread.so.0
#2  0xf76fab5d in pthread_rwlock_rdlock () from /lib/libpthread.so.0
#3  0x0804a81a in DiameterServiceSingleton::getDiameterService(void*) ()

有这么多线程,很难说有多少正在读取,有多少被阻塞,但我不明白为什么任何线程会被阻塞等待读取,考虑到其他线程已经在读取。

所以这是我的问题:为什么有些线程被阻塞等待读取 rw_lock,而其他线程已经在读取它?似乎可以同时读取的线程数是有限制的。

我查看了pthread_rwlock_attr_t功能,没有看到任何相关内容。

操作系统是 Linux,SUSE 11。

以下是相关代码:

{
  pthread_rwlock_init(&serviceMapRwLock_, NULL);
}

// This method is called for each request processed by the threads
Service *ServiceSingleton::getService(void *serviceId)
{
  pthread_rwlock_rdlock(&serviceMapRwLock_);
    ServiceMapType::const_iterator iter = serviceMap_.find(serviceId);
    bool notFound(iter == serviceMap_.end());
  pthread_rwlock_unlock(&serviceMapRwLock_);

  if(notFound)
  {
    return NULL;
  }

  return iter->second;
}

// This method is only called when the app is starting
void ServiceSingleton::addService(void *serviceId, Service *service)
{
  pthread_rwlock_wrlock(&serviceMapRwLock_);
    serviceMap_[serviceId] = service;
  pthread_rwlock_unlock(&serviceMapRwLock_);
}

更新:

正如 MarkB 在评论中提到的,如果我将 pthread_rwlockattr_getkind_np() 设置为优先考虑写入器,并且有写入器阻塞等待,那么观察到的行为将是有意义的。但是,我使用我认为优先考虑读者的默认值。我刚刚验证没有线程阻塞等待写入。我还按照@Shahbaz 在评论中的建议更新了代码并获得了相同的结果。

4

2 回答 2

6

您只是观察到与获取锁有关的固有性能问题。这需要一些时间,而您恰好在其中捕获了这些线程。当受锁保护的操作持续时间很短时尤其如此。

编辑:阅读源代码,glibc用于lll_lock保护其自己的 pthread 库数据结构中的关键部分。pthread_rwlock_rdlock检查几个标志并递增计数器,因此它在持有锁的同时执行这些操作。一旦这些都完成了,锁就会被释放lll_unlock

为了演示,我实现了一个简短的例程,该例程在获取rwlock. 主线程等待它们完成。但在等待之前,它会打印线程实现的并发性。

enum { CONC = 50 };

pthread_rwlock_t rwlock;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
unsigned count;

void *routine(void *arg)
{
    int *fds = static_cast<int *>(arg);
    pthread_rwlock_rdlock(&rwlock);
    pthread_mutex_lock(&mutex);
    ++count;
    if (count == CONC) pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    sleep(5);
    pthread_rwlock_unlock(&rwlock);
    pthread_t self = pthread_self();
    write(fds[1], &self, sizeof(self));
    return 0;
}

并且主线程等待计数器达到 50:

int main()
{
    int fds[2];
    pipe(fds);
    pthread_rwlock_init(&rwlock, 0);
    pthread_mutex_lock(&mutex);
    for (int i = 0; i < CONC; i++) {
        pthread_t tid;
        pthread_create(&tid, NULL, routine, fds);
    }
    while (count < CONC) pthread_cond_wait(&cond, &mutex);
    pthread_mutex_unlock(&mutex);
    std::cout << "count: " << count << std::endl;
    for (int i = 0; i < CONC; i++) {
        pthread_t tid;
        read(fds[0], &tid, sizeof(tid));
        pthread_join(tid, 0);
    }
    pthread_rwlock_destroy(&rwlock);
    pthread_exit(0);
}

编辑:使用 C++11 线程支持简化了示例:

enum { CONC = 1000 };
std::vector<std::thread> threads;

pthread_rwlock_t rwlock;
std::mutex mutex;
std::condition_variable cond;
unsigned count;

void *routine(int self)
{
    pthread_rwlock_rdlock(&rwlock);
    { std::unique_lock<std::mutex> lk(mutex);
      if (++count == CONC) cond.notify_one(); }
    sleep(5);
    pthread_rwlock_unlock(&rwlock);
    return 0;
}

int main()
{
    pthread_rwlock_init(&rwlock, 0);
    { std::unique_lock<std::mutex> lk(mutex);
      for (int i = 0; i < CONC; i++) {
          threads.push_back(std::thread(routine, i));
      }
      cond.wait(lk, [](){return count == CONC;}); }
    std::cout << "count: " << count << std::endl;
    for (int i = 0; i < CONC; i++) {
        threads[i].join();
    }
    pthread_rwlock_destroy(&rwlock);
    pthread_exit(0);
}
于 2012-08-08T15:23:42.567 回答
3

附带说明一下,上面发布的代码已损坏。您无法从 rw_lock'd 部分访问 iter->second,因为一旦您解锁 rw_lock,写入者就可以删除映射中的任何元素,从而使其上的任何迭代器无效。

我知道你没有在你的情况下这样做,因为你在程序执行开始之后没有写任何东西,但仍然值得一提。

另外,作为旁注,由于您描述的行为似乎是序列化的(作者在开始时写入地图,然后读者从现在开始读取“只读”地图),您可能应该这样写:

int writerDoneWithMap = 0;
// pthread_cond & mutex init here

// The writer write to the map here 

// Then they signal the reader that they are done with it
while (!__sync_bool_compare_and_swap(&writerDoneWithMap, 1, writerDoneWithMap));
pthread_cond_broadcast here


// The readers will then simply do this:
while (!writerDoneWithMap) 
{
     // pthread_cond_wait here
}
// Read the data without locks.

如果作者已经完成了映射,上面的代码避免了对读者的任何锁定,如果他们没有,那么你就求助于典型的 pthread_cond/mutex 技术。当且仅当您有作家 THEN 读者(但这就是您所说的)时,上面的代码才是正确的,否则它将失败。

于 2012-08-10T21:11:09.797 回答