2013-09-24 66 views
0

我的服務器中的線程之間有很多共享數據。如果我使用一個pthread_rwlock,則多線程停止。我使用rwlocks數組:如何在Linux 2.6中使用最大數量pthreads_rwlock

#define DIR_LOCK_COUNT  32 
pthread_rwlock_t dir_mutex[DIR_LOCK_COUNT]; 

... 
# into main thread initialize pthread_rwlock 
for(i=0; i < DIR_LOCK_COUNT; i++){ 
    if(pthread_rwlock_init(&dir_mutex[i], NULL) != 0) { 
     syslog(LOG_ERR, "can't initialize rwlock %i", i); 
     return ERR; 
    } 
} 

... 
# in the worker thread 

int num = user_id % DIR_LOCK_COUNT; 

pthread_rwlock_rdlock(&dir_mutex[num]); 
struct dir *dir_trash = dict_search((dict*)user->dirs, &dir_trash_id); 
pthread_rwlock_unlock(&dir_mutex[num]); 

我有35K用戶和16個線程池的數組。我可以使用pthread_rwlock的維度是1024還是更多?

回答

0

pthread_rwlock()的Linux/glibc NPTL實現不使用任何per-lock內核資源。如果首先有足夠的內存用於pthread_rwlock_t,那麼您可以創建鎖 - pthread_rwlock_init()在NPTL實現中永遠不會失敗。

相關問題