信号量
互斥量
线程通信-互斥
1.临界资源
一次只允许一个任务(线程、进程)访问共享资源
2.临界区
访问临界资源的代码
3.互斥机制
mutex互斥锁
任务访问临界资源前申请锁,访问完后释放锁
4.互斥锁:pthread_mutex_init
静态方式:pthread_mutex_t mutex
锁的销毁: int pthread_mutex_destroy
互斥锁的使用
1.int pthread_mutex_lock
申请锁,如果无法获得锁,任务阻塞
2.int pthread_mutex_unlock
释放锁
3.int pthread_mutex_trylock
申请锁,如果无法获得锁,返回EBusy而不是挂起等待
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_iNITALIZER;
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("this is child thread\n");
char str[ ]= " i am write func2 line\n";
char c;
int i = 0;
while(1){
pthread_mutex_lock(&mutex);
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_mutex_unlock
i = 0;
usleep(1);
pthread_exit("func1 exit");
}
pthread_exit("thread return ");
}
void *func1(void *arg)
{
printf("this is child thread\n");
pthread_detach(pthread_self());
char str[ ]= " i am write func2 line\n";
char c;
int i = 0;
while(1){
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
i = 0;
ulseep(1);
pthread_exit("func1 exit");
}
pthread_exit("thread return ");
}
int main(){
pthread_t tid,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid,NULL,func,NULL);
pthread_create(&tid2,NULL,func,NULL);
while(1){
sleep(1);
}
}
5.读写锁
初始化一个读写锁 pthread_rwock_init
读锁定读写锁 pthread_rwlokck_rdlock
非阻塞读锁定 pthread_rwlock_tryrdlock
写锁定读写锁 pthread_rwlock_wrlock
解锁读写锁 pthread_rwlock_unlock
释放读写锁 pthread_rwlock_destroy
读写锁:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
pthread_rwlock_t rwlock;
FILE *fp;
void *read_func(void *arg)
{
pthread_detach(pthread_self());
printf("this is child thread\n");
char buf[32]={0};
while(1){
pthread_rwlock_rdlock(&rwlock);
while(fgets(buf,32,fp)!=NULL)
{
printf("rd=%s\n",buf);
usleep(1000);
}
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
}
void *func1(void *arg)
{
printf("this is child thread\n");
pthread_detach(pthread_self());
char str[ ]= " i am write func2 line\n";
char c;
int i = 0;
while(1){
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
i = 0;
ulseep(1);
pthread_exit("func1 exit");
}
pthread_exit("thread return ");
}
int main(){
pthread_t tid1,tid2,tid3,tid4;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_rwlock_init(&rwlock,NULL);
pthread_create(&tid1,NULL,read_func,1);
pthread_create(&tid2,NULL,read_func,2);
pthread_create(&tid3,NULL,func,NULL);
pthread_create(&tid4,NULL,func,NULL);
while(1){
sleep(1);
}
}
6.死锁
多把锁才会卡死
死锁的避免
1.锁越少越好,最好使用一把锁
2.调整好锁的顺序
死锁代码:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_iNITALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_iNITALIZER;
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("this is child thread\n");
char str[ ]= " i am write func2 line\n";
char c;
int i = 0;
while(1){
pthread_mutex_lock(&mutex2);
printf("%d,I got lock\n",(int) arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
usleep(1);
pthread_exit("func1 exit");
}
pthread_exit("thread return ");
}
void *func1(void *arg)
{
printf("this is child thread\n");
pthread_detach(pthread_self());
char str[ ]= " i am write func2 line\n";
char c;
int i = 0;
while(1){
pthread_mutex_lock(&mutex2);
printf("%d,I got lock\n",(int) arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
ulseep(1);
pthread_exit("func1 exit");
}
pthread_exit("thread return ");
}
int main(){
pthread_t tid,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid,NULL,func,NULL);
pthread_create(&tid2,NULL,func,NULL);
while(1){
sleep(1);
}
}