linux编程---线程---读写锁

来源:互联网 发布:大数据编程 书籍推荐 编辑:程序博客网 时间:2024/06/07 00:43
读写锁通信机制

读写锁分为读锁和写锁,功能如下
(1)如果某线程申请了读锁,其他线程可以再申请读锁,但不能申请写锁。
(2)如果某线程申请了写锁,则其他线程不能申请读锁,也不能申请写锁。

初始化读写锁
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
            const pthread_rwlockattr_t *restrict attr);
            
销毁读写锁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

申请读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

申请写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

程序示例:
程序实现4个线程,两个读线程,两个写线程。

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

static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata[BUFSIZE];
int gtime;

void *thread_fun_read1(void*arg);
void *thread_fun_read2(void*arg);
void *thread_fun_write1(void*arg);
void *thread_fun_write2(void*arg);


int main(int argc,char* argv[])
{
    int res;
    pthread_t th1,th2,th3,th4;
    void* thret;
    
    res = pthread_rwlock_init(&rwlock,NULL);
    if(res != 0)
    {
        perror("rwlock initialization failed");
        exit(-1);
    }
    
    res = pthread_create(&th1,NULL,thread_fun_read1,NULL);
    if(res != 0)
    {
        perror("thread create failed");
        exit(-1);
    }
    
    res = pthread_create(&th2,NULL,thread_fun_read2,NULL);
    if(res != 0)
    {
        perror("thread create failed");
        exit(-1);
    }

    res = pthread_create(&th3,NULL,thread_fun_write1,NULL);
    if(res != 0)
    {
        perror("thread create failed");
        exit(-1);
    }

    res = pthread_create(&th4,NULL,thread_fun_write2,NULL);
    if(res != 0)
    {
        perror("thread create failed");
        exit(-1);
    }

    res = pthread_join(th1,&thret);
    if(res != 0)
    {
        perror("thread join failed");
        exit(-1);
    }

    res = pthread_join(th2,&thret);
    if(res != 0)
    {
        perror("thread join failed");
        exit(-1);
    }

    res = pthread_join(th3,&thret);
    if(res != 0)
    {
        perror("thread join failed");
        exit(-1);
    }

    res = pthread_join(th4,&thret);
    if(res != 0)
    {
        perror("thread join failed");
        exit(-1);
    }
    
    pthread_rwlock_destroy(&rwlock);
    exit(0);        
}

void *thread_fun_read1(void*arg)
{
    printf("thread read one try to get lock\n");
    pthread_rwlock_rdlock(&rwlock);
    while(strncmp("end",gdata,3) != 0)
    {
        printf("this is thread read one. %s \n",gdata);
        pthread_rwlock_unlock(&rwlock);
        sleep(2);
        pthread_rwlock_rdlock(&rwlock);
        while(gdata[0] == '\0')
        {
            pthread_rwlock_unlock(&rwlock);
            sleep(2);
            pthread_rwlock_rdlock(&rwlock);
        }
    }
    pthread_rwlock_unlock(&rwlock);
    gtime = 1;
    pthread_exit(0);    
}

void *thread_fun_read2(void*arg)
{
    printf("thread read two try to get lock\n");
    pthread_rwlock_rdlock(&rwlock);
    while(strncmp("end",gdata,3) != 0)
    {
        printf("this is thread read two. %s \n",gdata);
        pthread_rwlock_unlock(&rwlock);
        sleep(5);
        pthread_rwlock_rdlock(&rwlock);
        while(gdata[0] == '\0')
        {
            pthread_rwlock_unlock(&rwlock);
            sleep(5);
            pthread_rwlock_rdlock(&rwlock);
        }
    }
    pthread_rwlock_unlock(&rwlock);
    gtime = 1;
    pthread_exit(0);    
}

void *thread_fun_write1(void*arg)
{
    printf("this is write thread one try to get lock\n");
    while(!gtime)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("this is write thread one.\n input some text,enter 'end'to finish\n");
        fgets(gdata,BUFSIZE,stdin);
        pthread_rwlock_unlock(&rwlock);
        sleep(15);
    }
    pthread_rwlock_unlock(&rwlock);
    pthread_exit(0);
}

void *thread_fun_write2(void*arg)
{
    printf("this is write thread two try to get lock\n");
    while(!gtime)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("this is write thread two.\n input some text,enter 'end'to finish\n");
        fgets(gdata,BUFSIZE,stdin);
        pthread_rwlock_unlock(&rwlock);
        sleep(15);
    }
    pthread_rwlock_unlock(&rwlock);
    pthread_exit(0);
}
原创粉丝点击