l6-d8 线程的取消和互斥

一、互斥锁的概念和使用

1.线程通信 – 互斥

临界资源

 一次只允许一个任务(进程、线程)访问的共享资源

临界区

访问临界资源的代码

互斥机制

mutex互斥锁

任务访问临界资源前申请锁,访问完后释放锁

2.互斥锁的创建和销毁

互斥锁初始化 – pthread_mutex_init

#include  <pthread.h>

int  pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *  attr);

成功时返回0,失败时返回错误码

mutex  指向要初始化的互斥锁对象

attr  互斥锁属性,NULL表示缺省属性

man 函数出现 No manual entry for pthread_mutex_xxx解决办法

  apt-get install manpages-posix-dev

互斥锁的创建和销毁

两种方法创建互斥锁,静态方式动态方式

动态方式:

int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);

其中mutexattr用于指定互斥锁属性,如果为NULL则使用缺省属性。

静态方式:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

锁的销毁:

int pthread_mutex_destroy(pthread_mutex_t *mutex)

在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的 pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。

申请锁 – pthread_mutex_lock

#include  <pthread.h>

int  pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex)

成功时返回0,失败时返回错误码

mutex  指向要初始化的互斥锁对象

pthread_mutex_lock 如果无法获得锁,任务阻塞

pthread_mutex_trylock 如果无法获得锁,返回EBUSY而不是挂起等待

释放锁 – pthread_mutex_unlock

#include  <pthread.h>

int  pthread_mutex_unlock(pthread_mutex_t *mutex);

成功时返回0,失败时返回错误码

mutex  指向要初始化的互斥锁对象

执行完临界区要及时释放锁

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

FILE *fp;

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_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");
}

void *func1(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_mutex_lock(&mutex);
        while(i<strlen(str))
        {
            c = str[i];
            fputc(c,fp);
            i++;
            usleep(1);
        }
        pthread_rwlock_unlock(&mutex);
        i=0;
        usleep(1);
    }
    pthread_exit("func1 exit");
}


int main(){
    pthread_t tid1,tid2;

    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);
    }
}

二、读写锁概念和使用

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");
}

    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 <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

FILE *fp;

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_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");
}

void *func1(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_mutex_lock(&mutex);
        while(i<strlen(str))
        {
            c = str[i];
            fputc(c,fp);
            i++;
            usleep(1);
        }
        pthread_rwlock_unlock(&mutex);
        i=0;
        usleep(1);
    }
    pthread_exit("func1 exit");
}


int main(){
    pthread_t tid1,tid2;

    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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值