PTHREAD_MUTEX_INITIALIZER VS调用pthread_mutex_init(安培;互斥参数) [英] PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

查看:462
本文介绍了PTHREAD_MUTEX_INITIALIZER VS调用pthread_mutex_init(安培;互斥参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

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.

推荐答案

这是老版本的POSIX标准带有初始第一种方法只能保证与静态分配变量的工作,而不是当变量是<$ C $那是在一个函数体中定义的C>汽车变量。虽然我从来没有见过,这些都不会被允许,甚至对汽车变量的平台,而这个限制已在POSIX标准的最新版本中删除。

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 auto variable that is defined in a function body. Although I have never seen a platform where this would not be allowed, even for auto variables, and this restriction has been removed in the latest version of the POSIX standard.

静态变种确实是preferable如果你可以,因为它允许写引导code容易得多。每当在运行时会进入使用这种互斥code,你可以放心,互斥体被初始化。这是在多线程上下文中的precious信息。

The static variant 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.

使用一个初始化功能的方法,是preferable当需要为您的互斥特殊性能,如被递归例如或作为过程之间,不仅线程之间共享。

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.

这篇关于PTHREAD_MUTEX_INITIALIZER VS调用pthread_mutex_init(安培;互斥参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆