在外部模块中使用互斥体

发布于 2024-11-06 05:27:14 字数 186 浏览 6 评论 0原文

如果我有一个具有互斥锁的模块,并且我使用互斥锁上的锁定/解锁来写入 int 变量的值,那么如何在线程中运行的另一个模块中锁定/解锁相同的互斥锁?

外部模块还需要写入变量的值,这是在线程循环函数中完成的。如何使用相同的互斥锁来锁定它,或者应该使用另一个互斥锁?

如果 2 个不同的互斥体锁定相同的内存段(不一定同时)会发生什么?

If I have a module that has a mutex and I write the value of an int variable using lock/unlock on the mutex, how is the same mutex locked/unlocked in another module that is being run in a thread?

The external module also needs to write the value of the variable and this is being done in a threaded loop function. How does one lock this using the same mutex or should another mutex be used?

What happens if 2 different mutexes lock the same memory segments(not necessarily simultaneously)?

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

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

发布评论

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

评论(1

小清晰的声音 2024-11-13 05:27:14

为了使用 pthread 共享互斥体,您必须以某种方式通过指针共享互斥体的地址或使互斥体成为全局可访问的变量。

互斥体或信号量本身不会锁定给定的内存或关键代码段...相反,它们会锁定对内存中特定“标志”或位置的访问(即,像 unsigned long 或某些其他 POD 类型) )然后用于确定对关键部分或其他全局可访问内存位置的访问。换句话说,一旦一个线程“拥有”一个给定的互斥体,它就可以访问一段代码或内存部分,在拥有线程的锁期间,任何其他试图获得同一互斥体所有权的线程都会被阻止。

如果您使用两个不同的互斥锁来阻止对给定位置的访问,则不会提供对实际内存段的互斥访问...不共享互斥锁的每个线程将具有对内存段的相同访问权限,即使它们可能各自在各自的互斥锁上有所有权锁。因此,内存段并没有真正受到保护......两个线程可以同时访问内存段,从而产生并发问题。

再说一遍,如果您有不同的模块或线程,并且您想要对给定的代码或内存部分进行独占访问,则必须在这些元素之间共享相同互斥体。有很多方法可以完成此操作,如果您需要为多个单独的进程执行此操作,可以使用命名信号量之类的方法,或者通过共享内存段(即 shmget()shmat()shmdt 等)等,如果由于模块之间的地址空间存在某种分离而无法以某种方式共享全局可访问的互斥体或指针。

In order to share a mutex using pthreads, you are going to have to somehow share the address of the mutex through a pointer or make the mutex a globally accessible variable.

Mutexes or semaphores themselves do not lock a given memory or critical code section ... instead they lock access to a specific "flag" or location in memory (i.e., like an unsigned long or some other POD type) that is then used to determine access to a critical section or other globally accessible memory location. In other words once a thread "owns" a given mutex, it gets to have access to a segment of code or memory section that any other thread trying to obtain ownership of that same mutex is blocked from for the duration of the owning thread's lock.

If you use two different mutexes to block access to a given location, that does not provide mutually exclusive access to the actual memory segment ... each thread that does not share a mutex will have equal access to the memory segment even though they may each have an ownership lock on their respective mutexes. Therefore the memory segment is not really protected ... two threads could access the memory segment at the same time, creating concurrency issues.

So again, if you have different modules or threads, and you want to have exclusive access to a given section of code or memory, you're going to have to share the same mutex between those elements. There are many ways this could be done, either though something like named semaphores if you need to-do this for multiple separate processes, or through a shared memory segment (i.e., shmget(), shmat(), shmdt, etc.), etc. if you can't somehow share a globally accessible mutex or pointer because of some separation of address space between your modules.

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