我们可以使用 pthreads 互斥体锁定一个函数以供其所有其他调用吗?

发布于 2024-10-06 05:52:27 字数 146 浏览 0 评论 0原文

假设一个程序产生一个线程。该线程调用 func1()。然而,func1() 也在主应用程序的其他地方被调用。如果我仅将其包装在线程中的互斥锁中,对于整个应用程序来说是否安全?还是必须得进去锁上?如果其中有其他函数被它调用,但也在主应用程序的各个地方调用,是否必须递归并锁定它们?

Say a program spawns a thread. That thread calls func1(). However, func1() is also called in various places elsewhere in the main app. If i wrap it in a mutex lock in the thread only, will it be safe for the whole of the app? Or will one have to go in it and lock it? And if in it are other functions that are called by it but also on the main app in various places, does one have to go recursively and lock them?

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

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

发布评论

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

评论(3

栀子花开つ 2024-10-13 05:52:27

摆脱认为使用互斥体保护函数的习惯,事实并非如此。

您实际上保护了资源,例如线程之间共享的变量。

一旦您接受了这一点智慧之珠,您就会开始思考必须保护哪些数据,并且可以最大限度地减少保护的粒度。

例如,如果 func1()func2() 都访问共享变量 x,则可以调用 func2()< /code> 无论是从 func1() 还是 main(),您都必须设计一个解决方案来检测互斥体是否已经锁定,以便 func2() 可以声明/释放(当从 main 调用时)或不执行任何操作(当从 func1() 调用时)。要么使用递归互斥锁。

线程不安全的函数(例如使用静态数据区域)可以使用互斥体进行保护,但我发现重构它们通常更容易,以便它们本质上是线程安全的(使用分配的内存或线程) -本地存储)。

Get out of the habit of thinking that you protect functions with mutexes, you don't.

You actually protect resources such as variables shared amongst threads.

Once you accept that little pearl of wisdom, you start thinking in terms of what data has to be protected and can minimise the granularity of the protections.

For example, if func1() and func2() both access the shared variable x, and you can call func2() either from func1() or main(), you're going to have to engineer a solution that can detect if the mutex is already locked so that func2() can claim/release (when called from main) or do nothing (when called from func1()). Either that or use a recursive mutex.

Functions which are thread-unsafe (such as using static data areas) can be protected with mutexes but I find it's usually easier to refactor them so that they're inherently thread-safe (with allocated memory or thread-local storage).

弱骨蛰伏 2024-10-13 05:52:27

您只需要锁定共享资源或任何非线程本地的资源。您还应该考虑尽可能将函数编写为可重入的。可重入函数本质上是线程安全的,但并非所有线程安全函数都可以成为可重入的。

You only need to lock shared resources, or anything not thread-local. You should also consider writing your functions to be reentrant whenever possible. Reentrant functions are inherently thread-safe, whereas not all thread-safe functions can be made reentrant.

好倦 2024-10-13 05:52:27

只要你在函数中声明static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;并使用它,就可以完成你想要的事情。但是创建不可重入、具有全局状态等的函数通常是一件坏事。好的设计是锁定数据,而不是全局变量(或单例,这是全局变量的委婉说法)。

As long as you declare static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; in the function and use it, you can accomplish what you want. But making functions which are not reentrant, which have global state, etc. is generally a Bad Thing(tm). Good design is to lock data, and not to have globals (or singletons which is a euphemism for global variables).

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