简体   繁体   English

在结构 C 中锁定 pthread_mutex_t

[英]Lock pthread_mutex_t in structure C

I have one pthread_mutex_t aka lock in a structure and I create 2 structures.我在一个结构中有一个pthread_mutex_t aka 锁,我创建了 2 个结构。

  1. are those the same lock?是同一个锁吗? Or are they a completely different lock?还是它们是完全不同的锁?
  2. I have a function that uses the lock of the structure, can the one structure detect if the other structure lock is used?我有一个使用结构锁的function,一个结构可以检测是否使用了另一个结构锁?

are those the same lock?Or are they completly different lock?是同一个锁吗?还是完全不同的锁?

It deppends, if you have a pointer you can use the same mutex in both structures, if not, they are copies, using one will not reflect on the other.它取决于,如果你有一个指针,你可以在两个结构中使用相同的互斥锁,如果没有,它们是副本,使用一个不会反映另一个。

I have a function that uses the lock of the structure, can the one structure detect if the other structure lock is used?我有一个使用结构锁的function,一个结构可以检测是否使用了另一个结构锁?

Again, if it's a pointer to the same mutex it's shared by both structures, lock/unlock is detected by both, if not, locking or unlocking one will have no effect on the other.同样,如果它是指向两个结构共享的同一个互斥锁的指针,则两者都检测到锁定/解锁,如果不是,锁定或解锁一个对另一个没有影响。

Locks are no different from int s or any other members of a structure.锁与int或结构的任何其他成员没有什么不同。

Q: I have one int in a structure and I create 2 structures.问:我在一个结构中有一个 int,我创建了 2 个结构。 Are they the same int?他们是同一个int吗?
A: No they are not.答:不,他们不是。 Each structure has its own int.每个结构都有自己的 int。

Q: I have a function that uses the int of one structure, can the one structure detect if the other structure int is used?问:我有一个 function 使用一个结构的 int,一个结构可以检测是否使用了另一个结构 int 吗?
A: No it cannot, because each structure has its own int.答:不,它不能,因为每个结构都有自己的 int。

If I wanted one int shared by all the structures, I could make it a global variable.如果我想要一个由所有结构共享的 int,我可以将其设为全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我同时访问pthread_mutex_t pthread_mutex_lock.c时出错:62:__pthread_mutex_lock:断言`mutex-> __ data .__ owner == 0'失败 - An error when i acces concurrently to pthread_mutex_t pthread_mutex_lock.c:62: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed 使用传递给线程的pthread_mutex_t锁 - Using pthread_mutex_t lock passed to thread 是否更喜欢在 C 项目中使用全局 pthread_mutex_t? - Is it preferred to use a global pthread_mutex_t in C projects? 在结构中使用指向互斥锁 (pthread_mutex_t *) 的指针,而不是全局互斥锁 - Using a pointer to a Mutex (pthread_mutex_t *) in a structure, Instead of a global Mutex 将pthread_mutex_t传递给函数 - pass pthread_mutex_t to function linux中的pthread_mutex_t是否是可重入的(如果一个线程试图获取它已经持有的锁,则请求成功) - Does pthread_mutex_t in linux are reentrancy (if a thread tries to acquire a lock that it already holds, the request succeeds) “pthread_mutex_t mutex = {0}”初始化互斥锁吗? - Does “pthread_mutex_t mutex = {0}” initialize mutex? 为什么要破坏pthread_cond_t和pthread_mutex_t? - Why destroy pthread_cond_t and pthread_mutex_t? 使用pthread_cond_t和pthread_mutex_t - using pthread_cond_t and pthread_mutex_t 初始化的pthread_mutex_t对象内核是否持久化? - Are initialized pthread_mutex_t objects kernel persistent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM