为每个结构使用多个Mutex One的最佳方法? (例如,由4个线程操纵的2个结构)

发布于 2025-01-22 23:07:45 字数 1483 浏览 2 评论 0原文

我是第一次在C中使用线程的“大”刻度上工作,我不确定如何与每个usex一起正确使用多个静音,以保护结构,并且结构可能被多个线程操纵,

请考虑以下代码


#include <pthread.h>
#include <stdlib.h>

struct example{
    int parameter;
};

void *do_something(void *argument);

int main(){
    struct example *ex1 = (struct example *) malloc(sizeof(struct example));
    struct example *ex2 = (struct example *) malloc(sizeof(struct example));
    
    ex1->parameter = 5;
    ex2->parameter = 4;

    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;

    pthread_create(&thread1,NULL, do_something, ex1);
    pthread_create(&thread2,NULL, do_something, ex1);
    pthread_create(&thread3,NULL, do_something, ex2);
    pthread_create(&thread4,NULL, do_something, ex2);

    return 0;
}

void *do_something(void *argument){
    struct example *something = (struct example *) argument;
    something->parameter = something->parameter +1; 
    return NULL;
}

。在上面代码中为EX1分配两个Mutex的最佳方法?我有几个想法,但我不确定哪一个会起作用,哪个会造成灾难 例如


struct parameters{
    struct example *example;
    pthread_mutex_t lock;
};

struct parameters *param
pthread_create(&thread1,NULL, do_something, param);

甚至可能在第一个结构中包括穆特克斯

struct example{
    int parameter;
    pthread_mutex_t lock;
};

, 一种方法,事实证明是一场灾难

I am working for the first time on a "large" scale with threads in C and i am uncertain about how to properly use multiple mutex with each mutex protecting a structure and the structure possibly being manipulated by multiple threads

consider the following code


#include <pthread.h>
#include <stdlib.h>

struct example{
    int parameter;
};

void *do_something(void *argument);

int main(){
    struct example *ex1 = (struct example *) malloc(sizeof(struct example));
    struct example *ex2 = (struct example *) malloc(sizeof(struct example));
    
    ex1->parameter = 5;
    ex2->parameter = 4;

    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;

    pthread_create(&thread1,NULL, do_something, ex1);
    pthread_create(&thread2,NULL, do_something, ex1);
    pthread_create(&thread3,NULL, do_something, ex2);
    pthread_create(&thread4,NULL, do_something, ex2);

    return 0;
}

void *do_something(void *argument){
    struct example *something = (struct example *) argument;
    something->parameter = something->parameter +1; 
    return NULL;
}

what is the best way to assign two mutex one for ex1 and the other for ex2 in the code above? i have a couple of ideas in my mind but i'm not sure which one will work and which will make a catastrophe
for example


struct parameters{
    struct example *example;
    pthread_mutex_t lock;
};

struct parameters *param
pthread_create(&thread1,NULL, do_something, param);

or maybe even including the mutex in the very first structure so it's like

struct example{
    int parameter;
    pthread_mutex_t lock;
};

i'm not sure which is better or whether both are usuable at all, i'd love to hear some opinions, pointers or even readup recommendations before i commit into a method and it turns out to be a catastrophe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-29 23:07:45

在上面的代码中为EX1分配两个Mutex的最佳方法是什么?

将单独的静音与给定结构类型的每个实例相关联的最自然方法是使其成为该类型的成员。这并非总是可能的,但是当您以其他方式构建关联时,您会觉得如何从给定的结构实例中确定的问题,即与之相关的MUTEX。

也就是说,如果是struct示例您想要的sutexes然后

 结构示例{
    int参数;
    pthread_mutex_t锁;
};
 

what is the best way to assign two mutex one for ex1 and the other for ex2 in the code above?

The most natural way to associate a separate mutex with each instance of a given structure type is to make it a member of that type. That's not always possible, but when you form the association any other way, you are susceptible to the problem of how to determine from a given structure instance which is the mutex that goes with it.

That is, if it is struct example for which you want mutexes then

struct example{
    int parameter;
    pthread_mutex_t lock;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文