与互斥锁相关API
互斥量(mutex)从本质上来说是一把锁,在访问共享资源前对互斥量进行加锁,在访问完成后释放互斥量上的锁。对互斥量进行加锁后,任何其他试图再次对互斥量加锁的线程将会被阻塞直到当前线程释放该互斥锁。如果释放互斥锁时有多个线程阻塞,所有在该互斥锁上的阻塞线程都会变成可运行状态,第一个变为可运行状态的线程可以对互斥量加锁,其他线程将会看到互斥锁依然被锁住,只能回去等待它重新变为可用。在这种方式下,每次只有一个线程可以向前运行。
在设计时需要规定所有的线程必须遵守相同的数据访问规则。只有这样,互斥机制才能正常工作。操作系统并不会做数据访问的串行化。如果允许其中的某个线程在没有得到锁的情况下也可以访问共享资源,那么即使其它的线程在使用共享资源前都获取了锁,也还是会出现数据不一致的问题。
互斥变量用pthread_mutex_t数据类型表示。在使用互斥变量前必须对它进行初始化,可以把它置为常量PTHREAD_MUTEX_INITIALIZER(只对静态分配的互斥量),也可以通过调用pthread_mutex_init函数进行初始化。如果动态地分配互斥量(例如通过调用malloc函数),那么在释放内存前需要调用pthread_mutex_destroy。
1. 创建及销毁互斥锁
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t
*restrict mutex);
// 返回:若成功返回0,否则返回错误编号
要用默认的属性初始化互斥量,只需把attr设置为NULL。
2. 加锁及解锁
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *restrict mutex);
int pthread_mutex_trylock(pthread_mutex_t mutex);
int pthread_mutex_unlock(pthread_mutex_t *restrict mutex);
// 返回:若成功返回0,否则返回错误编号
如果线程不希望被阻塞,它可以使用pthread_mutex_trylock尝试对互斥量进行加锁。如果调用pthread_mutex_trylock时互斥量处于未锁住状态,那么pthread_mutex_trylock将锁住互斥量,不会出现阻塞并返回0,否则pthread_mutex_trylock就会失败,不能锁住互斥量,而返回EBUSY。
代码示例
#include<stdio.h>
#include<pthread.h>
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);int g_data=0;
pthread_mutex_t mutex;//定义一个互斥量也就是锁void*func1(void *arg)
{int i;pthread_mutex_lock(&mutex);//给互斥量上锁for(i=0;i<5;i++){printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));}pthread_mutex_unlock(&mutex);//给互斥量解锁}void*func2(void *arg)
{pthread_mutex_lock(&mutex);//给互斥量上锁printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));pthread_mutex_unlock(&mutex);//给互斥量解锁}int main()
{int ret;int param=100;pthread_t t1;pthread_t t2;pthread_mutex_init(&mutex,NULL);//初始化互斥量ret=pthread_create(&t1,NULL,func1,(void*)¶m);if(ret==0){printf("main:create t1 success\n");}ret=pthread_create(&t2,NULL,func2,(void*)¶m);if(ret==0){printf("main:create t2 success\n");}printf("main : %ld \n",(unsigned long)pthread_self());pthread_join(t1,NULL);//用来等待进程t1退出pthread_join(t2,NULL);//用来等待进程t2退出pthread_mutex_destroy(&mutex);//销毁这把锁return 0;
}
脚本可以这样写
CLC@Embed_Learn:~/xiancheng$ vi test.sh
CLC@Embed_Learn:~/xiancheng$ chmod +x test.sh
CLC@Embed_Learn:~/xiancheng$ ./test.sh
//其中test.sh中写入要运行的程序
实现进程t1满足条件退出代码
#include<stdio.h>
#include<pthread.h>
#include <stdlib.h>
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);int g_data=0;//pthread_mutex_t mutex;void*func1(void *arg)
{printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));pthread_mutex_lock(&mutex);while(1){printf("t1 printf is %d\n",g_data++);sleep(1);if(g_data==3){pthread_mutex_unlock(&mutex);printf("t1 over==============================\n");//pthread_exit(NULL);exit(0);}}}void*func2(void *arg)
{printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));while(1){printf("t2 printf is %d\n",g_data);pthread_mutex_lock(&mutex);g_data++; pthread_mutex_unlock(&mutex);sleep(1);}pthread_mutex_unlock(&mutex);
}int main()
{int ret;int param=100;pthread_t t1;pthread_t t2;pthread_mutex_init(&mutex,NULL);ret=pthread_create(&t1,NULL,func1,(void*)¶m);if(ret==0){printf("main:create t1 success\n");}ret=pthread_create(&t2,NULL,func2,(void*)¶m);if(ret==0){printf("main:create t2 success\n");}printf("main : %ld \n",(unsigned long)pthread_self());while(1){printf("main printf is %d\n",g_data);sleep(1);}pthread_join(t1,NULL);pthread_join(t2,NULL);pthread_mutex_destory(&mutex);return 0;
}
什么情况下造成死锁
死锁 是指两个或两个以上的进程在执行过程中,由于竞争资源或彼此通信而造成的一种阻塞现象,无外力作用,他们都将无法再推进下去。(一般需要两个mutex)
代码示例
void*func1(void *arg)
{int i;pthread_mutex_lock(&mutex);sleep(1);pthread_mutex_lock(&mutex2);for(i=0;i<5;i++){printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));}pthread_mutex_unlock(&mutex);}void*func2(void *arg)
{pthread_mutex_lock(&mutex2);sleep(1);pthread_mutex_lock(&mutex);printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));pthread_mutex_unlock(&mutex);}
参考博文:https://www.cnblogs.com/xiehongfeng100/p/4620852.html