[英]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 个结构。
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.