PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)
来源:互联网 发布:联发科cpu全开软件 编辑:程序博客网 时间:2024/06/05 11:10
http://stackoverflow.com/questions/14320041/pthread-mutex-initializer-vs-pthread-mutex-init-mutex-param
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.
42down vote
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.
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.
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.
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.
" plus you can only use the dynamic method if you're adding a bunch of mutexes at run time. "if your mutex is allocated by malloc()
(or belongs to an object that is allocated).
f mutex variable "lock" is part of a structure , then we can not follow 1st approach. we have to use pthread_init().
In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes.
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_INITIALIZER vs pthread_mutex_init ( &mutex, param)
- 多个用 PTHREAD_MUTEX_INITIALIZER 初始化的 pthread_mutex_t 不是同一个 mutex
- pthread_mutex_init
- pthread_mutex_init
- Semaphore VS Mutex
- Mutex VS spin lock
- Mutex vs. Semaphore
- pthread mutex vs spinlock
- spinlock vs. mutex
- Mutex VS Semaphore VS Spinlock
- Pthreads mutex vs Pthreads spinlock
- boost::mutex vs boost::recursive_mutex
- Pthreads mutex vs Pthreads spinlock
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- Pthreads mutex VS Pthreads spinlock
- Pthreads mutex vs Pthreads spinlock
- Pthreads mutex vs Pthreads spinlock
- Spring Web MVC学习笔记目录
- C++中的struct也能定义类!!!
- HDU 1425 sort 快速排序
- dfasdfasdfasdf
- struts2的初步使用
- PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)
- Android Intent Action 大全
- Mybatis 批量操作
- maven + tomcat7实现热部署
- jquery banner 轮播配置方法
- JavaScript for Kids 学习笔记:准备
- Google Chrome 浏览器 开发者工具 使用教程
- TCP/IP 协议三次握手与四次挥手
- 解决asp.net mvc的跨域请求问题