目录
临界资源
概念:
????????不能同时访问的资源,比如写文件,只能由一个线程写,同时写会写乱。
????????比如外设打印机,打印的时候只能由一个程序使用。
????????外设基本上都是不能共享的资源。
????????生活中比如卫生间,同一时间只能由一个人使用。
临界区
互斥机制
两种方法创建互斥锁,静态方式和动态方式:?
动态方式:?
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
?静态方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
?锁的销毁:
int pthread_mutex_destory(pthread_mutex_t *mutex)
在linux中,互斥锁并不占用任何资源,因此LinuxThreads中的pthread_mutex_destory()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
?示例代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //多个文件需要多个锁
void *func1(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread1\n");
char str[] = "I write func1 line\n";
char c;
int i = 0;
//pthread_mutex_t mutex1;
while (1)
{
pthread_mutex_lock(&mutex);
while(i < strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_mutex_unlock(&mutex);
i = 0;
usleep(1);
}
pthread_exit("func1 exit");
}
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread2\n");
char str[] = "You read func1 thread\n";
char c;
int i = 0;
//pthread_mutex_t mutex2;
while (1)
{
pthread_mutex_lock(&mutex);
while(i < strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_mutex_unlock(&mutex);
i = 0;
usleep(1);
}
pthread_exit("func2 exit");
}
int main()
{
pthread_t tid1,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid1,NULL,func1,NULL);
pthread_create(&tid2,NULL,func2,NULL);
while(1)
{
sleep(1);
}
}
运行结果:
?必要性:提高线程执行效率
特性:
?注意:
- ?初始化一个读写锁? ? ? ? pthread_rwlock_init
- 读锁定读写锁? ? ? ? ? ? ? ? pthread_rwlock_rdlock
- 非阻塞读锁定? ? ? ? ? ? ? ? pthread_rwlock_tryrdlock
- 写锁定读写锁? ? ? ? ? ? ? ? pthread_rwlock_wrlock
- 非阻塞写锁定? ? ? ? ? ? ? ? pthread_rwlock_trywrlock
- 解锁读写锁? ? ? ? ? ? ? ? ? ? pthread_rwlock_unlock
- 释放读写锁? ? ? ? ? ? ? ? ? ? pthread_rwlock_destroy
?示例代码:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
pthread_rwlock_t rwlock;
FILE *fp;
void * read_func(void *arg){
pthread_detach(pthread_self());
printf("read thread\n");
char buf[32]={0};
while(1){
//rewind(fp);
pthread_rwlock_rdlock(&rwlock);
while(fgets(buf,32,fp)!=NULL){
printf("%d,rd=%s\n",(int)arg,buf);
usleep(1000);
}
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
}
void *func2(void *arg){
pthread_detach(pthread_self());
printf("This func2 thread\n");
char str[]="I write func2 line\n";
char c;
int i=0;
while(1){
pthread_rwlock_wrlock(&rwlock);
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_rwlock_unlock(&rwlock);
i=0;
usleep(1);
}
pthread_exit("func2 exit");
}
void *func(void *arg){
pthread_detach(pthread_self());
printf("This is func1 thread\n");
char str[]="You read func1 thread\n";
char c;
int i=0;
while(1){
pthread_rwlock_wrlock(&rwlock);
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
i++;
usleep(1);
}
pthread_rwlock_unlock(&rwlock);
i=0;
usleep(1);
}
pthread_exit("func1 exit");
}
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,func2,NULL);
while(1){
sleep(1);
}
}
示例代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //多个文件需要多个锁
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; //多个文件需要多个锁
void *func1(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread1\n");
char str[] = "I write func1 line\n";
char c;
int i = 0;
//pthread_mutex_t mutex1;
while (1)
{
pthread_mutex_lock(&mutex2);
printf("%d,I get lock2\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I get 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
sleep(10);
}
pthread_exit("func1 exit");
}
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread2\n");
char str[] = "You read func1 thread\n";
char c;
int i = 0;
//pthread_mutex_t mutex2;
while (1)
{
pthread_mutex_lock(&mutex);
printf("%d,I get lock1\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I get 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
usleep(10);
}
pthread_exit("func2 exit");
}
int main()
{
pthread_t tid1,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid1,NULL,func1,1);
sleep(5);
pthread_create(&tid2,NULL,func2,2);
while(1)
{
sleep(1);
}
}
?运行结果: