Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么在文件或结构中使用多个互斥体?_C_Mutex - Fatal编程技术网

C 为什么在文件或结构中使用多个互斥体?

C 为什么在文件或结构中使用多个互斥体?,c,mutex,C,Mutex,如果互斥锁可以锁定变量区域,为什么要使用多个互斥锁 EX1:(全局变量) 而不是 static DEFINE_MUTEX(lock); static LIST_HEAD(core_list); static LIST_HEAD(module_list); 例2:(用于结构) 而不是 struct core_data { struct list_head node; struct list_head module_list; char core_id[20]; s

如果互斥锁可以锁定变量区域,为什么要使用多个互斥锁

EX1:(全局变量)

而不是

static DEFINE_MUTEX(lock);
static LIST_HEAD(core_list);
static LIST_HEAD(module_list);
例2:(用于结构)

而不是

struct core_data {
    struct list_head node;
    struct list_head module_list;
    char core_id[20];
    struct device *dev;
    struct list_head atten_list[CY_ATTEN_NUM_ATTEN];
    struct list_head param_list;
    struct mutex lock;
}

我喜欢@klutt的评论:
为什么房子里的每个房间都有锁,而前门只有一把锁?

我有一个使用两个互斥来更改两个独立变量的小例子:
inta
intb

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a, b;

pthread_mutex_t m1, m2;
void * increase_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a++;
        printf("increase a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * decrease_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a--;
        printf("decrease a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * increase_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b++;
        printf("increase b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}

void * decrease_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b--;
        printf("decrease b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}


int main()
{
    pthread_t a1, a2, b1, b2;

    pthread_create(&a1, NULL, increase_a, NULL);
    pthread_create(&a2, NULL, decrease_a, NULL);
    pthread_create(&b1, NULL, increase_b, NULL); 
    pthread_create(&b2, NULL, decrease_b, NULL);

    pthread_join(a1, NULL); 
    pthread_join(a2, NULL);
    pthread_join(b1, NULL);
    pthread_join(b2, NULL);

    pthread_mutex_destroy(&m1);
    pthread_mutex_destroy(&m2);


    return 0;
}
#包括
#包括
#包括
INTA,b;
pthread_mutex_t m1,m2;
无效*增加(无效*数据){

对于(int i=0;i我喜欢@klutt的评论:
当你可以在前门上只有一把锁的时候,为什么要把锁锁锁在房子的各个房间上?

我有一个使用两个互斥来更改两个独立变量的小例子:
inta
intb

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a, b;

pthread_mutex_t m1, m2;
void * increase_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a++;
        printf("increase a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * decrease_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a--;
        printf("decrease a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * increase_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b++;
        printf("increase b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}

void * decrease_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b--;
        printf("decrease b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}


int main()
{
    pthread_t a1, a2, b1, b2;

    pthread_create(&a1, NULL, increase_a, NULL);
    pthread_create(&a2, NULL, decrease_a, NULL);
    pthread_create(&b1, NULL, increase_b, NULL); 
    pthread_create(&b2, NULL, decrease_b, NULL);

    pthread_join(a1, NULL); 
    pthread_join(a2, NULL);
    pthread_join(b1, NULL);
    pthread_join(b2, NULL);

    pthread_mutex_destroy(&m1);
    pthread_mutex_destroy(&m2);


    return 0;
}
#包括
#包括
#包括
INTA,b;
pthread_mutex_t m1,m2;
无效*增加(无效*数据){

对于(int i=0;具有多个互斥体的i),一个线程可以处理(修改)一个列表而另一个线程在另一个列表上工作-这不可能只在一个互斥锁上发生。另一方面,如果多个线程希望同时在两个列表上工作,那么拥有多个互斥锁会导致潜在的死锁情况。当你可以只在前面有一把锁时,为什么要将锁锁定到房子中的各个房间door?@500 InternalServerError:不是核心列表锁或模块列表锁被获取,然后两个列表都被锁定?为什么还有另一个线程可以在列表上工作?我们无法从您发布的内容判断互斥体是如何获取的。@500 InternalServerError它是由互斥锁获取的,一个线程可以处理多个互斥体(修改)一个列表而另一个线程在另一个列表上工作-这不可能只在一个互斥锁上发生。另一方面,如果多个线程希望同时在两个列表上工作,那么拥有多个互斥锁会导致潜在的死锁情况。当你可以只在前面有一把锁时,为什么要将锁锁定到房子中的各个房间door?@500 InternalServerError:不是核心列表锁或模块列表锁被获取了吗?然后两个列表都被锁定了?为什么还有另一个线程可以在列表上工作?我们无法从您发布的内容判断互斥锁是如何获取的。@500 InternalServerError它是由互斥锁获取的
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a, b;

pthread_mutex_t m1, m2;
void * increase_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a++;
        printf("increase a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * decrease_a(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m1);
        a--;
        printf("decrease a = %d\n", a);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

void * increase_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b++;
        printf("increase b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}

void * decrease_b(void * data) {
    for(int i = 0; i <10; i++) {
        pthread_mutex_lock(&m2);
        b--;
        printf("decrease b = %d\n", b);
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}


int main()
{
    pthread_t a1, a2, b1, b2;

    pthread_create(&a1, NULL, increase_a, NULL);
    pthread_create(&a2, NULL, decrease_a, NULL);
    pthread_create(&b1, NULL, increase_b, NULL); 
    pthread_create(&b2, NULL, decrease_b, NULL);

    pthread_join(a1, NULL); 
    pthread_join(a2, NULL);
    pthread_join(b1, NULL);
    pthread_join(b2, NULL);

    pthread_mutex_destroy(&m1);
    pthread_mutex_destroy(&m2);


    return 0;
}