pthread_mutex 应用
来源:互联网 发布:rpm qa grep mysql 编辑:程序博客网 时间:2024/05/17 04:20
一、 pthread_mutex 家庭成员:
1.int pthread_mutex_init
(pthread_mutex_t *mutex , pthread_mutexattr_t *attr );
Description
The pthread_mutex_init function initializes the given mutex with the given attributes. Ifattr is null, then the default attributes are used.
2. int pthread_mutex_destroy
(pthread_mutex_t *mutex );
Description
The pthread_mutex_destroy function destroys the given mutex. If the mutex is already destroyed, thenerrno is set to EINVAL. If the mutex is locked, thenerrno is set to EBUSY.
3.int pthread_mutex_lock
(pthread_mutex_t *mutex );
Description
The pthread_mutex_lock function locks the given mutex. If the mutex is already locked, then the calling thread blocks until the thread that currently holds the mutex unlocks it.
4.int pthread_mutex_trylock
(pthread_mutex_t *mutex );
Description
The pthread_mutex_trylock function tries to lock the given mutex. If the mutex is already locked, the function returns without waiting for the mutex to be unlocked.
Returns
The pthread_mutex_trylock function returns zero if the call is successful, otherwise it setserrno toEINVAL and returns -1.
5.int pthread_mutex_unlock
(pthread_mutex_t *mutex );
Description
The pthread_mutex_unlock function unlocks the given mutex.
二、成员作用
1. 互斥锁的初始化和销毁
.初始化有两种初始化方式:
a. 对于静态分配的互斥锁一半用宏赋值的方式初始化
eg:static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
b. 对于动态分配的互斥锁(如调用malloc)或分配在共享内存中,则必须调用
pthread_mutex_init(pthread_mutex*mutex, pthread_mutexattr_t *mutexattr)函数来进行初始化。
动态分配释放例子:
pthread_mutex_init(*mutex ,*attr);
pthread_mutex_destory(*mutex)
pthread_mutexattr_init(*attr)
pthread_mutexattr_destory(*attr)动态分配的代码:
pthread_mutex_t
lock=(pthread_mutex_t *)malloc(sizeof(szieof(pthread_mutex_t)));
pthread_mutex_init(lock ,NULL);
. 互斥锁摧毁
pthread_mutex_destory(lock);
free(lock);
销毁一个互斥锁即意味着释放它所占用的资源,且要求锁当前处于开放状态。由于在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的 pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。
2. 互斥锁上锁、尝试上锁和解锁
要对公共变量进行上锁来进行保护,必须最少要上两个锁以上,换句话说就得调用pthread_mutex_lock()函数两次以上,否则,只是上一个锁会达不到相应的效果。尝试上锁pthread_mutex_trylock()和pthread_mutex_lock()的区别是:后者会阻塞等待另外一个锁被解锁;前者尝试去加锁,如果不成功就返回非0,如果成功返回0,不会产生阻塞。
每次上锁之后,一定要解锁,否则会造成程序一直在阻塞状态。
三、代码讲解:
代码说明1:互斥锁基本应用
#include<stdio.h>
#include<pthread.h>
#include"unistd.h"
pthread_mutex_tmutex = PTHREAD_MUTEX_INITIALIZER;
intcount = 0;
void*consume( void *arg)
{
while(1 )
{
pthread_mutex_lock(&mutex );
printf("************************consumebegin lock\n");
printf("************************consumed%d\n",count );
sleep(2);
count++;
printf("************************consumed%d\n",count);
printf("************************consumeover lock\n");
pthread_mutex_unlock(&mutex );
printf("************************I'mout of pthread_mutex\n");
sleep(1);
}
}
void* produce( void * arg )
{
while(1 )
{
pthread_mutex_lock(&mutex );
printf("productbegin lock\n");
printf("Produced %d\n", count );
printf("productover lock\n");
pthread_mutex_unlock(&mutex );
printf("I'mout of pthread_mutex\n");
sleep(1);
}
}
intmain( void )
{
pthread_tthread1,thread2;
pthread_create(&thread1, NULL, &produce, NULL );
pthread_create(&thread2, NULL, &consume, NULL );
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return0;
}
结果显示:
[elbort@elborttest1]$ gcc -Wall -lpthread -o test test.c
[elbort@elborttest1]$ ./test
productbegin lock
Produced0
productover lock
I'mout of pthread_mutex
************************consumebegin lock
************************consumed0
/*中间等待了2秒但是product线程没有执行|*/
************************consumed1
************************consumeover lock
************************I'mout of pthread_mutex
productbegin lock
Produced1
productover lock
I'mout of pthread_mutex
************************consumebegin lock
************************consumed1
************************consumed2
************************consumeover lock
************************I'mout of pthread_mutex
productbegin lock
Produced2
productover lock
I'mout of pthread_mutex
/************************************************************************/
代码说明2:单一调用互斥锁没效果
void*consume( void *arg)
{
while(1 )
{
pthread_mutex_lock(&mutex );
printf("************************consumebegin lock\n");
printf("************************consumed%d\n",count );
sleep(2);
count++;
printf("************************consumed%d\n",count);
printf("************************consumeover lock\n");
pthread_mutex_unlock(&mutex );
printf("************************I'mout of pthread_mutex\n");
sleep(1);
}
}
void* produce( void * arg )
{
while(1 )
{
count=count+5;
printf("Produced %d\n", count );
sleep(1);
}
}
intmain( void )
{
pthread_tthread1,thread2;
pthread_create(&thread1, NULL, &produce, NULL );
pthread_create(&thread2, NULL, &consume, NULL );
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return0;
}
~
~
结果说明:
[elbort@elborttest1]$ ./test
Produced5
************************consumebegin lock
************************consumed5
Produced10 //线程comsume被中断,conut的值被线程product修改
************************consumed11
************************consumeover lock
************************I'mout of pthread_mutex
/********************************************************/
代码说明3:pthread_mutex_trylock作用
intcount = 0;
void*consume( void *arg)
{
while(1 )
{
pthread_mutex_lock(&mutex );
printf("************************consumebegin lock\n");
printf("************************consumed%d\n",count );
sleep(2);
count++;
printf("************************consumed%d\n",count);
printf("************************consumeover lock\n");
pthread_mutex_unlock(&mutex );
printf("************************I'mout of pthread_mutex\n");
sleep(1);
}
}
void* produce( void * arg )
{
while(1 )
{
if(pthread_mutex_trylock(&mutex ) == 0)
{
printf("productbegin lock\n");
count++;
printf("Produced %d\n", count );
printf("productover lock\n");
pthread_mutex_unlock(&mutex );
printf("I'mout of pthread_mutex\n");
sleep(1);
}
else
{
printf("Ihave try!But i can lock the mutex!\n");
sleep(1);
}
}
}
intmain( void )
{
pthread_tthread1,thread2;
pthread_create(&thread1, NULL, &produce, NULL );
pthread_create(&thread2, NULL, &consume, NULL );
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return0;
}
结果显示:
[elbort@elborttest1]$ ./test
************************consumebegin lock
************************consumed0
Ihave try!But i can lock the mutex!
Ihave try!But i can lock the mutex!
************************consumed1
************************consumeover lock
************************I'mout of pthread_mutex
productbegin lock
Produced2
productover lock
I'mout of pthread_mutex
************************consumebegin lock
************************consumed2
Ihave try!But i can lock the mutex!
Ihave try!But i can lock the mutex!
************************consumed3
************************consumeover lock
************************I'mout of pthread_mutex
productbegin lock
Produced4
productover lock
I'mout of pthread_mutex
- pthread_mutex 应用
- pthread_mutex
- about pthread_mutex
- pthread_mutex互斥锁多进程共享
- pthread_mutex一例
- pthread pthread_mutex pthread_cond相关
- pthread_mutex互斥锁多进程共享
- pthread pthread_mutex pthread_cond相关
- pthread_mutex的使用
- pthread_mutex的任务间同步
- 线程同步之 pthread_cond_wait 和pthread_mutex
- pthread-win32配置方法及pthread_mutex测试
- linux下的读写锁pthread_mutex使用方法
- linux应用程序使用到的保护机制 semaphore和pthread_mutex
- linux互斥信号量pthread_mutex的两个问题
- C++ 多线程并发控制——互斥锁 pthread_mutex
- 5进程间锁:进程间pthread_mutex,文件锁
- 详解linux互斥锁 pthread_mutex和条件变量pthread_cond
- 常用的 shall 命令 及 语法
- Java中用Calendar类计算周和周的起始日期(转)
- excel导入到access
- C#数据类型
- 数据库部分备份策略
- pthread_mutex 应用
- Enum 枚举实例
- windows查询占用端口的pid以及查询对应的过程名称
- PHP入门经典书籍
- PHP5学习笔记-注释
- reg51.h是什么意思?
- 面向对象计算器
- Hello ExtJs4.1
- Div的min-height适用IE和Firefox