C语言 PTHREAD_MUTEX_INITIALIZER 与 pthread_mutex_init ( &mutex, param)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14320041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:02:01  来源:igfitidea点击:

PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

cubuntupthreadsmutex

提问by Kalec

Is there any difference between

有什么区别吗

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Or

或者

pthread_mutex_t lock;
pthread_mutex_init ( &lock, NULL);

Am I safe enough if I use only the first method ?

如果我只使用第一种方法,我是否足够安全?

NOTE:My question mostly refers to very small programs where at the most what I'll do is connect several clients to a server and resolve their inquiries with worker threads.

注意:我的问题主要是指非常小的程序,其中我最多要做的是将多个客户端连接到服务器并使用工作线程解决他们的查询。

采纳答案by Jens Gustedt

By older versions of the POSIX standard the first method with an initializer is only guaranteed to work with statically allocated variables, not when the variable is an autovariable that is defined in a function body. Although I have never seen a platform where this would not be allowed, even for autovariables, and this restriction has been removed in the latest version of the POSIX standard.

根据旧版本的 POSIX 标准,第一个带有初始化程序的方法只能保证使用静态分配的变量,而不是当变量是auto在函数体中定义的变量时。尽管我从未见过不允许这样做的平台,即使对于auto变量也是如此,并且在最新版本的 POSIX 标准中已删除此限制。

The staticvariant is really preferable if you may, since it allows to write bootstrap code much easier. Whenever at run time you enter into code that uses such a mutex, you can be assured that the mutex is initialized. This is a precious information in multi-threading context.

static如果可以,该变体确实更可取,因为它可以更轻松地编写引导程序代码。每当您在运行时输入使用此类互斥锁的代码时,您都可以确信该互斥锁已被初始化。这是多线程上下文中的宝贵信息。

The method using an init function is preferable when you need special properties for your mutex, such as being recursive e.g or being shareable between processes, not only between threads.

当您需要互斥锁的特殊属性时,使用 init 函数的方法更可取,例如递归或在进程之间共享,而不仅仅是在线程之间共享。

回答by Joe

You can set more attributes of the mutex with the dynamic initialisation, plus you can only use the dynamic method if you're adding a bunch of mutexes at run time.

您可以使用动态初始化设置更多互斥体的属性,此外,如果您在运行时添加一堆互斥体,则只能使用动态方法。

There's nothing wrong with the static approach though, if that fits your needs.

不过,如果符合您的需要,静态方法并没有错。

回答by Mari202

I would like to quote this from this book:

我想引用这本书中的一句话:

With POSIXthreads, there are two ways to initialize locks. One way to do this is to use PTHREAD_MUTEX_INITIALIZER, as follows: pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Doing so sets the lock to the default values and thus makes the lock usable. The dynamic way to do it (i.e., at run time) is to make a call to pthread_mutex_init()as follows: int rc = pthread_mutex_init(&lock, NULL); assert(rc == 0); // always check success!

The first argument to this routine is the address of the lock itself, whereas the second is an optional set of attributes. Read more about the attributes yourself; passing NULL in simply uses the defaults. Either way works, but we usually use the dynamic (latter) method.

对于POSIX线程,有两种方法可以初始化锁。一种方法是使用PTHREAD_MUTEX_INITIALIZER,如下所示: pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

这样做会将锁设置为默认值,从而使锁可用。这样做的动态方法(即在运行时)是调用pthread_mutex_init()如下: int rc = pthread_mutex_init(&lock, NULL); assert(rc == 0); // always check success!

此例程的第一个参数是锁本身的地址,而第二个参数是一组可选的属性。自己阅读有关属性的更多信息;传入 NULL 只是使用默认值。无论哪种方式都有效,但我们通常使用动态(后一种)方法。

回答by Ramesh Miriyala

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes.

在默认互斥体属性合适的情况下,宏 PTHREAD_MUTEX_INITIALIZER 可用于初始化互斥体。

If you want to specify attributes for mutex go with dynamic initialization ........

如果要为互斥锁指定属性,请使用动态初始化........

The effect shall be equivalent to dynamic initialization by a call to?pthread_mutex_init() with parameter?attrspecified as NULL, except that no error checks are performed.

除了不执行错误检查外,效果应等同于通过调用?pthread_mutex_init() 并将参数?attr 指定为NULL 进行动态初始化。