为每个结构使用多个Mutex One的最佳方法? (例如,由4个线程操纵的2个结构)
我是第一次在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将单独的静音与给定结构类型的每个实例相关联的最自然方法是使其成为该类型的成员。这并非总是可能的,但是当您以其他方式构建关联时,您会觉得如何从给定的结构实例中确定的问题,即与之相关的MUTEX。
也就是说,如果是
struct示例
您想要的sutexes然后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