相关疑难解决方法(0)

如何使用 pthread_mutex_destroy 在 Linux 中安全正确地销毁互斥锁​​?

我阅读了APUE 3rd , 11.6.1 Mutexes ,本章中有一个关于锁定和解锁互斥锁的示例:

struct foo {
    int             f_count;
    pthread_mutex_t f_lock;
    int             f_id;
    /* ... more stuff here ... */
};

struct foo *
foo_alloc(int id) /* allocate the object */
{
    struct foo *fp;

    if ((fp = malloc(sizeof(struct foo))) != NULL) {
        fp->f_count = 1;
        fp->f_id = id;
        if (pthread_mutex_init(&fp->f_lock, NULL) != 0) {
            free(fp);
            return(NULL);
        }
        /* ... continue initialization ... */
    }
    return(fp);
}

void
foo_hold(struct foo *fp) /* add a reference to the …
Run Code Online (Sandbox Code Playgroud)

c linux mutex pthreads race-condition

6
推荐指数
1
解决办法
3627
查看次数

标签 统计

c ×1

linux ×1

mutex ×1

pthreads ×1

race-condition ×1