如果我在函数内调用另一个函数,我应该解锁吗?

发布于 2024-11-26 02:02:06 字数 364 浏览 1 评论 0原文

假设我有一个像这样的简单代码,

functionA(){
     lock(lockA);

     //doing something with a shared data;
     functionB();
     unLock(lockA);
}

functionB(){
     lock(lockB);
     //doting something with another shared data
     unLock(lockB);
}

我想知道是否应该在调用 functionB 之前解锁,否则没关系。另外,如果我有 2 个共享数据(A 和 B),那么我应该有两个互斥锁变量吗?或者我可以只使用一个吗?提前致谢..

Let's say I have a simple code like this

functionA(){
     lock(lockA);

     //doing something with a shared data;
     functionB();
     unLock(lockA);
}

functionB(){
     lock(lockB);
     //doting something with another shared data
     unLock(lockB);
}

I was wondering if I should unlock before I call functionB or it doesn't matter. Also if I have 2 shared data(A and B) then should I have two mutex lock variables? or can I just use one? Thanks in advance..

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

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

发布评论

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

评论(3

东北女汉子 2024-12-03 02:02:06

如果A和B绝对不相关,并且在调用函数B后你不会对A做任何其他事情,那么你应该先解锁。如果您不再访问 A,则无需保留互斥体。

但是,如果您使用两个锁并且并不总是以相同的顺序获取它们,则应该小心死锁。就像如果您遵循未修改的示例一样,锁是按 AB 订单获取的。如果您的程序中存在这样的情况,您将按照 BA 顺序获取锁,则可能会导致死锁。

您可以只使用两种不同的锁或一种锁,具体取决于您需要的粒度级别。您介意在 A 上运行的函数也会阻止在 B 上运行的所有其他函数吗?如果您可以接受,单锁会更容易管理。

If A and B are absolutely not correlated and you won't do anything else with A after you call function B you should unlock first. There is no need to keep the mutex if you are not going to access A any more.

You should be careful of deadlocks though if you use two lock and do not always obtain them in the same order. Like if you follow your unmodified example locks are acquired on A-B order. If there is a case in your program such that you will acquire locks in B-A order it may cause a deadlock.

You can just use two different locks or a single one depending on the level of granularity you need. Do you mind functions working on A blocks all other functions working on B as well. If that is acceptable for you single lock would be much easier to manage.

多情出卖 2024-12-03 02:02:06

使用当前的设计,您可能会面临死锁 - 如果其他线程已锁定 lockB 并尝试获取锁定 lockA,则两个线程都会被捕获。另一方面,释放锁定到lockA可能会允许对共享数据进行不需要的并发访问。

所以你必须评估你的程序的需求。如果您希望两个线程同时访问多个变量(例如一致地修改这些变量),您应该为所有这些变量拥有一把锁,并保持该锁直到完成访问。

With current design you could face a deadlock - if some other thread had lockB locked and tries to acquire a lock to lockA both threads are trapped. On the other hand releasing lock to lockA could allow undesired concurrent access to the shared data.

So you have to assess the needs of your program. If you want two threads to access several variables at once (like consistently modify those variables) you should have one lock for all those variables and hold the lock until you've finished access.

梦纸 2024-12-03 02:02:06

假设 functionB() 没有从其他任何地方调用,那么只使用一把锁就足够安全了。

即使您需要使用两个锁,也请确保在它们锁定的共享变量上的工作不再被使用时立即释放它们。

在您遇到的情况下,如果 functionB() 本身尝试在 A 上加锁,则可能会陷入死锁。因此,最好在调用可能依次调用的函数之前释放锁。尝试建立一个锁。

Assuming functionB() isn't called from anywhere else, it's safe enough to use just one lock.

Even if you need to use two locks, make sure you release them as soon the work on the shared variable they lock will no longer be used.

With the scenario you have, you may end up into a dead lock if functionB() itself attempted to put a lock on A. Therefore, it's good practice to release locks prior to calling functions that may in turn try to establish a lock.

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