我的服务器中的线程之间有许多共享数据。如果我使用一个 pthread_rwlock,多线程 stoppid。我使用 rwlocks 数组:
#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 或更大吗?