开发者社区 问答 正文

锁的粒度问题

struct mg_context {
    char        *options[NUM_OPTIONS];    /* Configured opions    */
    pthread_mutex_t    opt_mutex[NUM_OPTIONS];    /* Option protector    */
    int        max_threads;    /* Maximum number of threads    */
    int        num_threads;    /* Number of threads        */
    int        num_idle;    /* Number of idle threads    */
    pthread_mutex_t    thr_mutex;    /* Protects (max|num)_threads    */
    pthread_cond_t    thr_cond;
    pthread_mutex_t    bind_mutex;    /* Protects bind operations    */
};

以上,每一个变量都有对应的锁,假设有一个线程获得了opt_mutex[0],然后需要对
options[0]这个字符串进行修改,然后又有一个线程需要修改max_threads,
那这个线程可以成功获得thr_mutex锁吗?如果能的话,它能立即就修改max_threads吗?

展开
收起
杨冬芳 2016-05-30 19:36:18 1930 分享 版权
1 条回答
写回答
取消 提交回答
  • IT从业

    可以阿,这个锁能不能获取到是 跟这个锁有没有人在用这个锁有关系 跟其它的锁没有关系。。。
    只要你要获取的锁不依赖于其它的锁(依赖的话其它的锁可能有人在用),就没有问题。。

    2019-07-17 19:21:01
    赞同 展开评论
问答地址: